0
    int *p={1,2,3,4};
    printf("%d",(*p+1));
    return 0;

While I run it, it terminates with signal 11. And if I simply make an array and assign it to the pointer it works perfectly fine, what is the difference as {1,2,3,4} is also an array.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • you code tries to create a pointer that is initialized with the value 1. On my machine it wont compile due to there being too many initializers. YOu code then dies because you dereference a pointer with a value of 1 – pm100 Jul 04 '22 at 01:43
  • *as {1,2,3,4} is also an array.* Is it? Could also be an initializer for a struct. – Gerhardh Jul 04 '22 at 08:18

2 Answers2

3

The line

int *p={1,2,3,4};

will not declare an array, because you are not declaring p as an array. You are declaring it as a single pointer. Also, you are assigning the value 1 to this pointer, which is illegal in ISO C. You cannot assign an integer to a pointer in ISO C. If you attempt to compile this in strict ISO C18 compliance mode, it will fail to compile. It will also fail due to providing too many initializers.

If you want p to be an array of 4 int elements, then you can write it like this instead:

int p[] = { 1, 2, 3, 4 };

If you want p to be a pointer that points to the first element, then you can write it like this instead:

int arr[] = { 1, 2, 3, 4 };
int *p = arr;

Due to array to pointer decay, the line

int *p = arr;

is equivalent to:

int *p = &arr[0];

Also, the line

printf("%d",(*p+1));

is probably not doing what you think it does. Due to the rules on operator precedence, this line is equivalent to the following:

printf("%d",(*p)+1);

However, you probably want the following instead:

printf("%d",*(p+1));

which is equivalent to:

printf("%d",p[1]);

Since you have defined your array contents in such a way that both are equivalent (both have the value 2), you probably won't notice this bug. However, as soon as you change the array contents so that p[1] does not have the value p[0] + 1, this bug will become visible.

Here is an example of what your entire code should probably look like:

#include <stdio.h>

int main( void )
{
    int arr[] = { 1, 2, 3, 4 };
    int *p = arr;
    printf( "%d\n", p[1] );
    return 0;
}

This program has the output 2.

Note that in this program, the line

int *p = arr;

is not necessary, because you can simply write arr[1] instead of p[1]. You don't need the variable p at all.

Andreas Wenzel
  • 22,760
  • 4
  • 24
  • 39
2

There is no array in your code.

What you posted is equivalent to

int *p = 1;

You could create an anonymous array as follows:

int *p = ( int[] ){ 1, 2, 3, 4 };

This is the same thing as

int anon[] = { 1, 2, 3, 4 };
int *p = anon;
ikegami
  • 367,544
  • 15
  • 269
  • 518
  • THANKS but if I simply assign string which is an array of char, why my code works fine? – Anup Adhikari Jul 04 '22 at 02:11
  • @AnupAdhikari: because under most circumstances the array expression is converted to a pointer to the first element of the array. Arrays are weird and don't behave like other types, and what works for arrays doesn't work for anything else. – John Bode Jul 04 '22 at 04:52
  • @ikegami I am taking about char *p – Anup Adhikari Jul 04 '22 at 05:04
  • And also tell me how can I assign 2d arrays in array of pointers in implicit declaration – Anup Adhikari Jul 04 '22 at 05:05
  • Re "*if I simply assign string which is an array of char, why my code works fine?*", Because then you do have an array. And when an array is treated as a pointer, it degenerates into a pointer to its first element. So that's perfectly fine. – ikegami Jul 04 '22 at 05:18
  • Re "*How can I assign 2d arrays in array of pointers in implicit declaration*", Are you asking how to make the array in `int a[][4] = { { 1,2,3,4 }, { 5,6,7,8 } }; int (*p)[4] = a;` anonymous? If so, `int (*p)[4] = ( int [][4] ){ { 1,2,3,4 }, { 5,6,7,8 } };` – ikegami Jul 04 '22 at 05:25
  • Yes but I want it to assign not in a pointer to whole array, instead in a array of pointer. Before you had cleared that typecasting we can assign a array in a pointer like:. int *p = ( int[] ){ 1, 2, 3, 4 }; so as 1 pointer is assigned a array I want a array of pointers to get assigned 2d array. – Anup Adhikari Jul 04 '22 at 05:27
  • But you said 2d-array, now you say array of pointers. This is going to far off-topic. If you have more questions, please post them as a Question. – ikegami Jul 04 '22 at 05:29
  • And your edit says array of pointers of 2d arrays. Once you settle on a question, ask it as a Question. I'm tired of the moving goal post. – ikegami Jul 04 '22 at 05:31
  • In any case, I've already shown how to create anonymous vars. Just remove the name from the declaration and put that in the parens, followed by the initializer. – ikegami Jul 04 '22 at 05:34
  • I am sorry for that I am posting this as a clear question , Please give me few minutes. – Anup Adhikari Jul 04 '22 at 05:34
  • #include int main() { int i; //int (*p)[]= (int [][3]) { {1,2,3},{10,20,30} }; int *p[]= { (int [][3]) { {1,2,3},{10,20,30} } }; for(int i=0;i<(2*3);i++) printf("%d ",*(*p+i)); return 0; } The actual problem is if I run code after uncommenting the initialization it gives no error and if I run like this it warns me like incompatible assignment but still provides correct output – Anup Adhikari Jul 04 '22 at 06:16
  • How is the correct code working and the incorrect code warning a problem? – ikegami Jul 04 '22 at 06:31