Email: Password: Remember Me | Create Account (Free)

Back to Subject List

Old thread has been locked -- no new posts accepted in this thread
???
05/26/05 13:22
Read: times


 
#93928 - That's the problem
Responding to: ???'s previous message
Andy Neil said:
Donald Catto said:
"Neil's point is that, although pointers & arrays are different, they both use the same Notation."

No. He states that "pointer notation and array notation are interchangeable". He does not state that "[pointers and arrays] use the same notation", which is even less correct, if it is possible to have a state of 'less correct'.


By "Notation," I'm thinking of:

The value of an array name is the address of the start of the array; The value of a pointer is the address of the start of the pointed-to object.

The index-offset operator [] may be applied to a pointer as well as an array name.

The dereference operator * may be applied to an array name as well as a pointer.
void main( void )
{
char my_array[] = "Array";
char *my_pointer = "Pointer";

char my_char;

my_char = my_array[0];
my_char = my_pointer[0];

my_char = *my_array;
my_char = *my_pointer;

}

The differences lie in the behaviour:

Defining an array allocates storage for the array elements.

You cannot assign to an array name.



Let's expand on your example a bit:

void main( void )
{
    char  my_array[] = "Array";
    char *my_pointer = "Pointer";

    char my_char;

    my_char = my_array[0];
    my_char = my_pointer[0];

    my_char = *my_array;
    my_char = *my_pointer;

    *my_array='X'; 
    *my_pointer='X'; //Uh oh!

    my_array[0]='X';
    my_pointer[0]='X'; //Uh oh!

    my_pointer=my_array;
    *my_pointer='X';   //Ok now!
    my_pointer[0]='X'; //Ok now!

    printf("%dn",(int)sizeof(my_array));
    printf("%dn",(int)sizeof(my_pointer)); //Different!

    &my_array;   //Pointer to array of char
    &my_pointer; //Pointer to pointer to char
}


I hope this serves to illustrate why is it so dangerous to think that arrays and pointers are interchangeable, or that similarity of notation implies similarity of results.



List of 27 messages in thread
TopicAuthorDate
Unions in C            01/01/70 00:00      
   You miss the point completely...            01/01/70 00:00      
   Easy with Union            01/01/70 00:00      
      You can see from the Raghu example...            01/01/70 00:00      
         Platform-dependence            01/01/70 00:00      
            Padding in unions            01/01/70 00:00      
               portability            01/01/70 00:00      
      array=pointer...?            01/01/70 00:00      
         array != pointer            01/01/70 00:00      
         Quirk of C            01/01/70 00:00      
            Read the FAQ            01/01/70 00:00      
               Read the Comment            01/01/70 00:00      
                  Read everything            01/01/70 00:00      
                     Looks the same to me            01/01/70 00:00      
                        This One            01/01/70 00:00      
                        That's the problem            01/01/70 00:00      
                           Good example            01/01/70 00:00      
                              No fun            01/01/70 00:00      
                                 Well...            01/01/70 00:00      
                                 Of course it does!            01/01/70 00:00      
                                    Hmm            01/01/70 00:00      
                        Actually, even less.            01/01/70 00:00      
                           const pointer            01/01/70 00:00      
   O.K you win            01/01/70 00:00      
      Please conclude            01/01/70 00:00      
         Not Exactly            01/01/70 00:00      
         End of wrong stick?            01/01/70 00:00      

Back to Subject List