Print values ​​in an array pointer in C

I can't seem to wrap my head around the problem. What am I missing?

Consider the following

int main(int argc, char *argv[]) { while ( *argv ) { printf("argv[] is: %s\n", *argv); ++argv; } return 0; } 

This prints out each argv value. Thus, on a command line such as ./example arg1 arg2 , the following is displayed:

 `argv[] is: ./example` `argv[] is: arg1` `argv[] is: arg2` 

Now consider the following (which I'm having problems with):

 int main(void) { char *days[] = { "Sunday", "Monday", "Tuesday" }; while ( *days ) { printf("day is %s\n", *days); *days++; } return 0; } 

If I try to compile, I get an error message cannot increment value of type 'char *[3]'

If I change *days++ to (*days)++ , it compiles. If I run it, it will work forever and eventually end with a bus error .

However, it does not iterate over each value of days[] . I even tried putting a Null pointer in the form of '\0' and "\0" in the days array with no effect.

What am I missing?

+7
c arrays pointers
source share
4 answers

There are several errors in the code:

  • There is a difference between the argv variable and the days constant. The variable can be changed, the constant label of the array cannot.

  • In your array, the NULL terminator is missing at the end of the array.

  • *days++; in this case is pointless. This is a dummy element and returns the value of days , then the days increment. Simply use days++ .

So your code should look like this:

 #include <stdio.h> int main(void) { char *days[] = { "Sunday", "Monday", "Tuesday", NULL }; char **d = days; while ( *d ) { printf("day is %s\n", *d); d++; } return 0; 
+7
source share

The argv [] structure contains a null pointer as the last entry, so your code works. You can add a null pointer to your data structure. You also need to understand that argv is a parameter (like a local variable) that points to an internal data structure containing command line arguments. You need to create a similar local variable that can go through the array.

Here is the solution:

 #include <stdio.h> int main(void) { char *days[] = {"Sunday", "Monday", "Tuesday", 0 }; char **v = days; while (v) { printf("day is %s\n", *v); v++; } return 0; } 
+5
source share
  • days is a two-dimensional array of characters. Although the array name points to the first address, it is not a pointer. You cannot do pointer arithmetic by array name.
  • while ( *days ) will never become false. You have to change the logic.

In the code below, I have saved most of your code.

  #include <stdio.h> int main(void) { char *days[] = { "Sunday", "Monday", "Tuesday" }; for(int i=0; i < 3; i++) { printf("day is %s\n", (days[i])); } return 0; 
+1
source share

Instead of incrementing a pointer to a character, you should probably skip every element in the character array.

For example:

 char *days[] = { "Sunday", "Monday", "Tuesday" }; int i; for (i = 0; i < 3; i++) printf("day is %s\n", days[i]); 
0
source share

All Articles