www.delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp/1997/02/07/02:30:00

From: unicorn AT walrus DOT com (Bob Schultz)
Newsgroups: comp.os.msdos.djgpp
Subject: Re: Question on pointers and arrays
Date: Fri, 07 Feb 1997 00:18:54 GMT
Organization: Intellitech Corporation
Lines: 32
Message-ID: <32fa7242.998097@news.walrus.com>
References: <32f92a6c DOT 0 AT ntnews DOT compusmart DOT ab DOT ca> <32FA7146 DOT 3883 AT cam DOT org>
NNTP-Posting-Host: p36.ts1.walrus.com
Mime-Version: 1.0
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Tudor <tudor AT cam DOT org> wrote:

>Demandred wrote:
>> 
>> Probably a silly question, but...
>> 
>> If I declare an array of objects of type Foo
>> 
>> Foo FooArray[5];
>> 
>> And pass a pointer to a Foo into a function (or class constructor)
>> 
>> Bar(Foo *array) {...
>> 
>> Can I access elements in the array in the function, like so?
>> 
>> ...array[3]...}
>I guess you can.
>When you say Foo array[5] then 'array' is actually a pointer to the
>first element.
>char string[5]="abcde" and
>char *string="abcde" are equivalent.

They are not at all equivalent.  The first allocates storage for 5
chars and initializes them to the chars 'abcde'.  Note that the
contents of the memory location after the 'e' is undefined.  The
second allocates space for a pointer to a char and then initializes
the pointer to point to the null terminated string "abcde" somewhere
else in memory.  What is equivalent is the two different ways of
referencing an element in the array or string.  'string[2]' is the
same as '*(string+2)'.  And even though it looks odd '[2]string' also
references the same memory.

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019