-1

Why is the below code giving a warning?

#include <stdio.h>
int main()
{
    int arr[3] = {1,2,3};
    int *ptr, *ptr1;
    ptr = &arr;
    ptr1 = arr;
    printf("ptr is %p\t ptr1 is %p\n",ptr, ptr1);
    ptr++;
    ptr1++;
    printf("ptr is %p\t ptr1 is %p\n",ptr, ptr1);
    return 0;

}
$ gcc test.c -o test
test.c: In function ‘main’:
test.c:6:6: warning: assignment from incompatible pointer type [enabled by default]
  ptr = &arr;

I also tried changing ptr to int **ptr, but it still fails with the same warning. arr is a pointer; I am storing it in a pointer. What is the mistake here?

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
md.jamal
  • 4,067
  • 8
  • 45
  • 108

2 Answers2

2

arr is a pointer

No it is not.

MSVC (surprisingly) gives a little more detailed warning on this code:

warning C4047: '=': 'int *' differs in levels of indirection from 'int (*)[3]'

The syntax &array without a subscript includes length information. Changing the assignment to &arr[0] removes the warning and it works as intended.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
1

C Arrays are very similiar to pointers. They both refer to addresses in memory and behave the same in accessing the data. One important difference in fact is that a pointer can show to any adress in memory while an array is an address to the beginning of a block in the memory and it cannot be reassigned to another address. So "arr" is already kind of a pointer and if you want to assing it to another pointer, just use:

int* ptr = arr;

You don't need the adress operator, but you can also get a pointer to the first value by accessing the first element and using the adress operator on it.

int* ptr = &arr[0];

Please be sure to not say array = pointer. But they have things in common.

Leon0402
  • 160
  • 1
  • 9
  • 2
    No, that's definitely not the only difference. Don't say that arrays are pointers, because they are not. – klutt Jan 22 '19 at 15:00
  • Yes of course there are some more differences, "only" was probably to hard. I will edit it. I just want to keep things simple and arrays are quite similar to pointers, the reason I gave it probably the most importan one. And it helps understanding why the code has to be like this, if you compare arrays with pointers – Leon0402 Jan 22 '19 at 15:08
  • One VERY common source of problems (even in this particular) is that people doesn't understand the difference between pointers and arrays, so I think it is very important to not give the impression that they are the same. – klutt Jan 22 '19 at 15:14
  • Yes you are absolutly right! On the other hand people often also have problems with accepting that arrays behave in certain aspects like pointers, especially if they come from languages like Java. So I guess, we need to find the middle and tell people that they are not the same, but have things in common. – Leon0402 Jan 22 '19 at 15:23
  • Pointing out the similarities and differences and when arrays decays to pointers is one of the keys to understand C. – klutt Jan 22 '19 at 15:25