Thread: for an array a , is a same as &a

  1. #1
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33

    for an array a , is a same as &a

    Code:
    #include <stdio.h>
    void display(int* arr,int len){
        int i;
        for(i = 0; i < len; ++i){
    	printf("%d ",arr[i]);
    	printf("\n");
        }
    }
    main()
    {
        int arr[] = {1,2,3};
        printf("%d  %d\n",arr,&arr);
    }
    are the two arr and &arr same ? , can they be used interchangeably ?
    if not what is the difference ?
    gcc gives this warning:

    test23.c: In function `main':
    test23.c:13: warning: passing arg 1 of `display' from incompatible pointer type

  2. #2
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    Code:
    #include <stdio.h>
    void display(int* arr,int len){
        int i;
        for(i = 0; i < len; ++i){
    	printf("%d ",arr[i]);
    	printf("\n");
        }
    }
    main()
    {
        int arr[] = {1,2,3};
        printf("%d  %d\n",arr,&arr);
        display(&arr,3);
        display(arr,3);
    }
    This is the code i am referring to

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >are the two arr and &arr same ? , can they be used interchangeably ?
    No, and no. The fact that your compiler warns you about it should be a hint that they're not compatible types.

    >if not what is the difference ?
    A level of indirection. For the following array:
    Code:
    int a[10];
    a is almost always treated as the address of the first element (&a[0]), but &a is treated as the address of the array as a whole. The address may be the same, but the types are different. a is a pointer to int and &a is a pointer to an array of ten int.

    To use both, you need two different functions:
    Code:
    #include <stdio.h>
    void display1(int* arr,int len){
      int i;
      printf("display1: ");
      for(i = 0; i < len; ++i)
        printf("%d ",arr[i]);
      printf("\n");
    }
    void display2(int (*arr)[3],int len){
      int i;
      printf("display2: ");
      for(i = 0; i < len; ++i)
        printf("%d ",(*arr)[i]);
      printf("\n");
    }
    int main(void)
    {
      int arr[] = {1,2,3};
      printf("%d  %d\n",arr,&arr);
      display2(&arr,3);
      display1(arr,3);
      return 0;
    }
    My best code is written with the delete key.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > are the two arr and &arr same ?
    They're the same by value, but the types are different.
    Compare a pointer to a struct and a pointer to the first element of a struct - same value, different type.

    > can they be used interchangeably ?
    No.

    > if not what is the difference ?
    arr is a pointer to the first element of the array (like &arr[0] is), and &arr is a pointer to the whole array.

    void display(int (*arr)[3],int len);
    Would be the prototype of the function for passing a pointer to the whole array.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Spooky
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2005
    Location
    Hyderabad, India
    Posts
    33
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 05-29-2009, 07:25 PM
  2. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Help with an Array
    By omalleys in forum C Programming
    Replies: 1
    Last Post: 07-01-2002, 08:31 AM