1

I am trying to understand the concept of array of char pointers in C. In this basic example I try to get the number of strings using the x++ operator but, unfortunately I get a compiler error because maybe I try to access an extra region of memory but I shoudn't? Thanks for any help.

#include <stdio.h>

int main()
{
    char *argv[]= {"hello","world"};
    int num = 0;

    while (argv[num++] != NULL){
        printf("num value: %i\t  %c\n",num,*argv[num-1]);
    }

    printf("Final num value: %i\n",num);

    return 0;
}
3
  • 2
    you say compiler error , this is false, you have the error at the execution, please be careful when you describe your problem Commented Jan 10, 2019 at 21:27
  • pippo inzaghi "I get a compiler error " --> It is more informative to also post the exact text of that error. That raises the value of this post as it is easier for others to find the same issue and for others to solve. Commented Jan 10, 2019 at 21:42
  • Both of You are correct, sorry. The error I was getting at execution time was Cygwin Exception : open stack dump file. Commented Jan 10, 2019 at 21:53

1 Answer 1

6

You are exceeding array bounds since

 while (argv[num++] != NULL)

will stop when argv[num] is NULL, yet argv[]'s dimension is 2, and both entries are != NULL.

You could write...

 char *argv[]= {"hello","world",NULL};

and it should work with your end condition as is.

BTW: you are aware that argv[] is often used as name for the parameters to function main, i.e. int main(int argc, char* argv[]), which represent the command line arguments when your program gets called, are you? When you use the function main-parameter, I think that the last valid element of it will by NULL by definition (cf., for example, this online C11 standard draft):

5.1.2.2.1 Program startup

....

(2) If they are declared, the parameters to the main function shall obey the following constraints:

argv[argc] shall be a null pointer.

If you create your "own" local argv[]-thing, however, you have to do this explicitly on your own.

Sign up to request clarification or add additional context in comments.

7 Comments

I don't think I've ever seen anything specifying that argv is NULL terminated - hence the argc value is required. Can you cite a source for that?
@Shawn That is explicitly in the C standard, § 5.1.2.2.1 (Program Startup): — argv[argc] shall be a null pointer.
argv isn't a reserved word. It just that when you define an array like char* x[2], then x[0]="a" and x[1]="b" are OK, but x[2]="a" exceeds array bounds. char* argv[] = {"a","b"} is equivalent to an array like char* x[2]. As argv[num++] with num==1 will be != NULL, num will be increased to 2 and will access the array out of its bounds in the next round then.
I understand now thank you. The reason why I called this array argv is because I am using it as parameters vector in execv and that caused further confusion. Does the parameter vector in execv need to end with NULL just like for the main() function? According to here it does.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.