Question about arrays and pointers 
Author Message
 Question about arrays and pointers

As we all know, array names in C are actually pointers.  So, as I understand
it, when I declare:

char string[10];

what I've actually done is allocated 10 * sizeof(char) bytes on the
application heap.  The pointer to that allocated block is then assigned to
string.  Written out longhand, the following piece of code should be
equivalent to the above declaration (except I have to free it manually):

char *string;
string = (char *)malloc(10 * sizeof(char));

Moving right along then, string is a pointer.  By definition, it contains an
address and resides in a separate part of memory from the entity which it
points to.  Therefore, if I examine the value of &string, it should be
different from the address contained IN string.  This is precisely what I
observe when I use malloc to create the array.  

On the other hand, with the simple array declaration, what I observe is that
&string and the value contained in string are EQUAL.  Therefore, string
points to itself.  I ask the following:  If string points to itself, how
can it point to the beginning of the array at the same time?  The beginning
of the array cannot occupy the same memory as string.

Clearly I'm missing something.  Can anyone explain what's going on here?
Thanks.

_____________________
Bill Svec



Mon, 12 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers

Quote:

> As we all know, array names in C are actually pointers.  So, as I understand
> it, when I declare:

False.

Quote:
> char string[10];

> what I've actually done is allocated 10 * sizeof(char) bytes on the
> application heap.  The pointer to that allocated block is then assigned to
> string.  Written out longhand, the following piece of code should be
> equivalent to the above declaration (except I have to free it manually):

> char *string;
> string = (char *)malloc(10 * sizeof(char));

That's wrong.  It's also better written as:

    string = malloc (10);

sizeof (char) == 1, and the cast could hide a {*filter*} error.  See the FAQ
for more details.

Quote:
> Moving right along then, string is a pointer.  By definition, it contains an
> address and resides in a separate part of memory from the entity which it
> points to.  Therefore, if I examine the value of &string, it should be
> different from the address contained IN string.  This is precisely what I
> observe when I use malloc to create the array.

> On the other hand, with the simple array declaration, what I observe is that
> &string and the value contained in string are EQUAL.  Therefore, string
> points to itself.  I ask the following:  If string points to itself, how
> can it point to the beginning of the array at the same time?  The beginning
> of the array cannot occupy the same memory as string.

> Clearly I'm missing something.  Can anyone explain what's going on here?
> Thanks.

Your entire premise that an array is just a pointer is wrong.  Arrays
are themselves objects.

What you are probably getting confused with is the fact that an array's
name in most contexts decays to a pointer to its first element.  There
are two cases when this doesn't happen:  when the array is the operand
of sizeof, and when it is the operand of the & address operator.[1]

"string" is an array of ten characters, ten bytes.  It is *not* a
pointer to ten characters, though its name decays to one.  That's the
behavior you're observing.

Forgot your notion that arrays and pointers are equivalent because they
are not.  The FAQ has more information on this subject.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani

[1] - "sizeof string" is not equivalent to "sizeof &string [0]".
"sizeof string" returns the size of the entire array, not the size of a
pointer to the first element.  "&string" is not equivalent to "&&string
[0]" as that's obviously an illegal expression.  "&string" evaluates to
a pointer to the entire string, which has the type "char (*) [10]", not
"char *".)



Mon, 12 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers

Quote:

> As an aside, it's a good idea to avoid defining identifiers beginning
> with str followed by a lower case letter.  They are reserved under
> some circumstances.

But at block scope they aren't reserved, so the code which I snipped is
fine.

--

I believe we can change anything.
I believe in my dream.
    - Joe Satriani



Mon, 12 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers
On Thu, 27 Aug 1998 20:15:25 -0600, "William Svec"

Quote:

>As we all know, array names in C are actually pointers.  So, as I understand
>it, when I declare:

>char string[10];

>what I've actually done is allocated 10 * sizeof(char) bytes on the
>application heap.  The pointer to that allocated block is then assigned to
>string.  Written out longhand, the following piece of code should be
>equivalent to the above declaration (except I have to free it manually):

>char *string;
>string = (char *)malloc(10 * sizeof(char));

>Moving right along then, string is a pointer.  By definition, it contains an
>address and resides in a separate part of memory from the entity which it
>points to.  Therefore, if I examine the value of &string, it should be
>different from the address contained IN string.  This is precisely what I
>observe when I use malloc to create the array.  

>On the other hand, with the simple array declaration, what I observe is that
>&string and the value contained in string are EQUAL.  Therefore, string
>points to itself.  I ask the following:  If string points to itself, how
>can it point to the beginning of the array at the same time?  The beginning
>of the array cannot occupy the same memory as string.

>Clearly I'm missing something.  Can anyone explain what's going on here?
>Thanks.

What you're missing is that strings are not pointers.

When you declare

        char string[10];

what you have actually done is allocate 10 bytes (sizeof(char) is
always 1) somewhere (where depends on where this declaration is and
the compiler).  string is the name of that array of 10 bytes.  It is
not the name of a pointer variable that contains a pointer to those
bytes.

In most expressions string is silently converted to a pointer, but
there are exceptions.  Consider

        #include <stdio.h>
        #include <stdlib.h>

        int main(void)
        {
          char a[10];
          char *b = malloc(10);

          printf("sizeof a == %lu\n", (unsigned long) sizeof a);
          printf("sizeof b == %lu\n", (unsigned long) sizeof b);
          return 0;
        }

This will print

        sizeof a == 10
        sizeof b == ???

where ??? is whatever the size of a pointer to char is; it probably
will not be 10 and, in any case, will not depend on how much space
allocated with malloc.

As an aside, it's a good idea to avoid defining identifiers beginning
with str followed by a lower case letter.  They are reserved under
some circumstances.
--
Michael M Rubenstein



Tue, 13 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers

Hi there,

Array names are not really pointers at all, athough when used in most
contexts in a C program they are virtually equivellent.

char string[10];

defines an array of 10 bytes, we all know that.

when use use the name of the array in most places in a C program
it represents a pointer to the first element of the array, in this case
a pointer to char. Hence all the following are the same
        string[4] = 0;
        *(string + 4) = 0;
        *(4 + string) = 0;
        4[string] = 0;

It also follows that if you have a pointer to char initialised as follows:
char *x = array;        then the following is also all the same
        *(x + 4) = 0;
        *(4 + x) = 0;
        x[4] = 0;
        4[x] = 0;

However, when you take the & of an array name, in ANSI C, you get a
pointer to the whole array, not just the first element. Hence you get a
pointer to an array of 10 chars. You can also explicitly define pointers
like that as in:
        char (*p)[10] = &array;

So string isnt a pointer in the real sense of the word it just evaluates in
an expression to a conceptual pointer to the first element.

When used in sizeof(array), it doesnt behave that what at all.

when used in a context where an address is the result both
array and &array evaluate to the same address, but the type of the
result is different.
        array is a pointer to a char
        &array is a pointer to an array of 10 char.

Does this help

Michael.



Tue, 13 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers


Quote:

>> As an aside, it's a good idea to avoid defining identifiers beginning
>> with str followed by a lower case letter.  They are reserved under
>> some circumstances.

>But at block scope they aren't reserved, so the code which I snipped is
>fine.

The code is OK but it is still worth avoiding. Sidestepping the issue
completely prevents mistakes.

--
-----------------------------------------


-----------------------------------------



Tue, 13 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers


Quote:
>As we all know, array names in C are actually pointers.

No, array names desingate arrays, just as integer variable names
designate integers structure variable names designate structures and so on.

Quote:
> So, as I understand
>it, when I declare:

>char string[10];

>what I've actually done is allocated 10 * sizeof(char) bytes on the
>application heap.

Space for 10 characters has been allocated somewhere, not necessarily on
any "heap".

Quote:
> The pointer to that allocated block is then assigned to
>string.

No, no pointer object is created. string represents the array of 10
characters, not a pointer variable.

Quote:
> Written out longhand, the following piece of code should be
>equivalent to the above declaration (except I have to free it manually):

>char *string;
>string = (char *)malloc(10 * sizeof(char));

This does create a separate pointer object so is not equivalent.

Quote:
>Moving right along then, string is a pointer.  By definition, it contains an
>address and resides in a separate part of memory from the entity which it
>points to.  Therefore, if I examine the value of &string, it should be
>different from the address contained IN string.  This is precisely what I
>observe when I use malloc to create the array.  

>On the other hand, with the simple array declaration, what I observe is that
>&string and the value contained in string are EQUAL.  Therefore, string
>points to itself.  I ask the following:  If string points to itself, how
>can it point to the beginning of the array at the same time?  The beginning
>of the array cannot occupy the same memory as string.

Since string designates the array and not a separate pointer the behaviour
you see is to be expected. Your confusion probably stems from the fact
that when the value of an array is taken in an expression it produces a
pointer to its first element. So

   char *p = array;

works and initialises p with a pointer to the first element of array.
Consider

   int i;
   int *ip = &i;

Here i is a simply integer variable. ip is assigned a pointer to i. That
doesn't mean that the definition of i created a pointer variable in addition,
just that i has an inherent address that the & operator gives you. Similarly
you can write

   char *p = &array[0];

which gives you a pointer to the first element of array. If you like you
can think of C having a shorthand way of writing this, i.e. just

   char *p = array;

This gives you the inherent address of a[0] just as &i gives you the
inherent address of i. There's no need for an extra variable/object to
store this address.

--
-----------------------------------------


-----------------------------------------



Tue, 13 Feb 2001 03:00:00 GMT  
 Question about arrays and pointers
On Thu, 27 Aug 1998 23:20:13 -0400, John Kugelman

Quote:


>> As an aside, it's a good idea to avoid defining identifiers beginning
>> with str followed by a lower case letter.  They are reserved under
>> some circumstances.

>But at block scope they aren't reserved, so the code which I snipped is
>fine.

Which is why I worded it that way.  William is obviously a beginner;
it didn't seem appropriate to give him the full set of rules, but I
thought it worthwhile to warn him to avoid defining such identifiers.
--
Michael M Rubenstein


Tue, 13 Feb 2001 03:00:00 GMT  
 
 [ 8 post ] 

 Relevant Pages 

1. question about array of pointers to chars

2. newbie question about array of pointers

3. Question about array of pointers...

4. Question about Arrays of Pointers?

5. Question on Array of POinter

6. Dereferencing f-pointers, arrays of f-pointers, pointers to f-pointers

7. Array of pointers, pointer to array...

8. array pointer/pointer array

9. arrays pointers array of pointers

10. Pointer of Pointers was Pointer of arrays...

11. Pointers: return of pointer to array of pointers to main

12. sizeof and arrays/pointers question

 

 
Powered by phpBB® Forum Software