Why is ptr = a; shows error? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is ptr = a; shows error?

int a[2][3] = { }; int *ptr; int a; ptr = a; printf("%d",*(ptr[2]+3));

11th Apr 2019, 4:43 PM
Unemployed Visionary.
5 Answers
+ 7
The Pointer to an Array is always the Memory Adress of the first Element. https://www.sololearn.com/learn/C/2962/
11th Apr 2019, 6:49 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 5
You must assign the memory address to ptr (= &a). https://www.sololearn.com/learn/C/2933/
11th Apr 2019, 6:28 PM
Sebastian Keßler
Sebastian Keßler - avatar
+ 5
You have two variables named <a>, one is an int array ... int a[2][3] = {}; and another one below an int int a; I'm not sure which of these you're pointing to with <ptr>, but from the printf call it seems you want to use the array, so remove that `int a;` from your code, and test again.
11th Apr 2019, 6:51 PM
Ipang
0
But array has its own pointer so why mention & ?
11th Apr 2019, 6:30 PM
Unemployed Visionary.
0
cause is that pointer must be following way *ptr=a or else int *ptr=a
5th Nov 2020, 7:53 AM
Shaik Fardeen Sami
Shaik Fardeen Sami - avatar