Mail Archives: djgpp/1997/02/07/02:30:00
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 -