top button
Flag Notify
    Connect to us
      Facebook Login
      Site Registration Why to Join

Facebook Login
Site Registration

What is the difference between arrayname and a pointer in C/C++?

+4 votes
620 views
What is the difference between arrayname and a pointer in C/C++?
posted Nov 15, 2013 by anonymous

Share this question
Facebook Share Button Twitter Share Button Google+ Share Button LinkedIn Share Button Multiple Social Share Button

2 Answers

0 votes

Both will holds address but pointer address can be modified where as array name can not.

answer Nov 15, 2013 by sivanraj
May be arrayname is constant so can not be modified. Is my understanding correct?
ya, that may be in build in design.
0 votes

Let me explain with the example.

int main()
{
    int arr[] = { 1, 2, 3};
    int *ptr = NULL;

    printf("addr arr : %p\naddr ptr : %p\n",  arr,  ptr);
    /*
        The output will addr arr : SOME_ADDR and addr ptr : (nil)
         So the name of array will give self address. But the name of pointer will give the address to which it
         pointing, not self address. For that you need to do " printf("ptr_addr : %p\n", &ptr);"
    */

    ptr = arr;
    printf("addr arr : %p\naddr ptr : %p\n",  arr,  ptr);  
    /* 
         This will print same address which belongs to arr. See it by your self.
    */
    return 0;
}

Some time array can be used as a pointer and vice-verse. For example.

int array[] = { 1, 2, 3}
printf(" array[0] : %d,  array[1] : %d,  array[2] : %d\n", array[0],  array[1],  array[2]);
printf(" array[0] : %d,  array[1] : %d,  array[2] : %d\n", *array,  *(array + 1),  *(array + 2));
/* Both are same. */

int *ptr = array;
printf(" ptr[0] : %d,  ptr[1] : %d,  ptr[2] : %d\n", ptr[0], ptr[1], ptr[2]);
/* It will print the same output as above. */

A pointer can point to an array, but the same for array is not ture, i.e

int array1[] = { 1, 2, 3};
int array2[] = { 4, 5, 6};
int *ptr = array1;
ptr = array2;  /* This is ok, you can change the ptr pointing to some other location */

/* But same is not possible for array */
array1 =  array2;  /* It will give to error */

Hope your doubt will be clear after this.

answer Sep 1, 2014 by Arshad Khan
Contact Us
+91 9880187415
sales@queryhome.net
support@queryhome.net
#280, 3rd floor, 5th Main
6th Sector, HSR Layout
Bangalore-560102
Karnataka INDIA.
QUERY HOME
...