Here is the code, it does not work, what other options are there?

int a[3] = {1, 2, 3}, i; int *f = a; for (i = 0; i < 3; i++) { printf(" %d", *f[i]); } 

And is it possible to explicitly assign values ​​to an array after its declaration? Those.

 int a[3]; a[3] = {1, 2, 3}; 
  • @Alexandr To format the code, you need to select it with the mouse and press the button 101010. - Nicolas Chabanovsky
  • @Alexandr You need to accept the answer to the question that best solves the problem. - Nicolas Chabanovsky
  • first, the pointer stores the address of the first element of the array int * f = & a [0]; secondly printf ("% d", f [i]); - Sergey
  • @ Sergey, you cleverly noticed. It remains only to clarify that & a [0] == a always and everywhere. - kirelagin
  • @Alexandr by the way, pay attention to the fact that you could not explicitly specify the size of the array. int a [] = {1,2,3} also works. - kirelagin

4 answers 4

Can. Also

 int a[3] 

and

 int * f; 

Same. An error with the pointer. I think it's easier to imagine two kinds of correct code and you will understand where the truth is hiding.

 int a[3] = {1, 2, 3}, i; int *f = a; for (i = 0; i < 3; i++) { printf(" %d", f[i]); } 

Second option

 int a[3] = {1, 2, 3}, i; int *f = a; for (i = 0; i < 3; i++) { printf(" %d", *(f+i)); } 
  • don't forget about i [f] - saigono

This is how it works.

 int a[3] = {1,2,3}; int *f = a; for(int i = 0; i <3; i++) printf("%d\n",f[i]); return 0; 

The second way can not be done.

      int a[3] = {1, 2, 3}, i; int *f = a; for (i = 0; i < 3; i++) { printf("%d \n", *f++); } 

      Here's another option:

       int a[3] = {1, 2, 3}, i; int (*f)[] = &a; for (i = 0; i < 3; i++) printf("%d ", (*f)[i]); 

      And is it possible to explicitly assign values ​​to an array after its declaration? Those.

       int a[3]; a[3] = {1, 2, 3}; 

      It is impossible for two reasons:

      1. a[3] indicates the 4th element of the array (numbering from zero), and there are only three declared ( int a[3] ).
      2. int a[3] declares an array of numbers of type int , and in the record a[3] = {1, 2, 3}; an attempt is made to assign a value not of type int .