C while loop calls ++ against itself when there is a char

I found this function, and I do not understand some of its parts. I have about 3 days work experience, so bear with me. This function is used to parse command line arguments.

  • Why do they reassign *arg to *c ?

  • I do not understand why they start the while loop.

  • Secondly, why would they start a while loop against a char pointer? I understand that char is actually an array of characters, but I understand that they will run the while loop, and char will access the values โ€‹โ€‹of the characters in the array individually, and they don't do any of this.

  • How can you increase the number against char?

  • Why do we even have * c?

  • I added a check line to see if arg -styles for example, which has - , so I can parse the flag and get the value, which is the next argument in argv - is it used correctly?

As I said, I have a 3-day experience in C, so please be careful and methodical and as much as possible to help me better understand this function in general.

 void print_args(int argc,char *argv[]) { int i; if(argc > 1){ for(i=1;i<argc;i++){ char *arg = argv[i]; char *c = arg; while(*c){ if(strchr("-", *c)){ printf("arg %d: %s -> %s\n",i,arg,argv[i+1]); } c++; } } } } 
+4
source share
4 answers

1) arg is assigned to point to the head of the current char array, and c used to move the array.

2,3,5) The while loop runs until c points to (char)0 , which is also \0 . So basically they look at every character in the char array until they reach the null terminator character. It would be better conditional while (*c != '\0') , rather than relying implicitly on the fact that '\0' == 0

4) They increase the pointer, so it points to the next memory cell, that is, the next cell in the array.

6) Your add-on will work as a test for recognizing options, you still have to compare it with -styles to make sure that this is really a valid option.

This sample code could have done with a lot of corrections to make it more reliable and understandable. If this is from a book or textbook C, I suggest you look for another one.

+2
source

Most of the individual things that the code does can make sense alone, but the while over the characters in arg is fancy. This is something like "print arg and next arg for each dash in arg". But I doubt that anyone really wants this.

+1
source

I turned print_args into the main function of a program that contains nothing and ran it. This is what it does:

 $ ./a.out a ab abc abcd arg 1: a -> ab arg 2: ab -> abc arg 2: ab -> abc arg 3: abc -> abcd arg 3: abc -> abcd arg 3: abc -> abcd arg 4: abcd -> (null) arg 4: abcd -> (null) arg 4: abcd -> (null) arg 4: abcd -> (null) 

In other words, for each character of each command line argument, it prints the entire command line argument, a thin arrow, and the next command line argument. (If my C library was not very nice with printf("%s", (char *)NULL) , it would crash when it got to the end.) Listing the entire command line argument, why does it need c , arg as well, although this line read perfectly

 printf("arg %d: %s -> %s\n", i, argv[i], argv[i+1]); 

and then arg not required.

Your hunch is as good as mine why this function performs this particular thing. For me, this does not seem to be a particularly useful thing.

EDIT: If your goal is to print argv[i] and argv[i+1] when argv[i] starts with a dash and argv[i+1] not NULL (which may make sense in context) then you should write it like this:

 void print_args(int argc, const char *const *argv) { int i; for (i = 0; i < argc; i++) if (argv[i][0] == '-' && argv[i+1]) printf("arg %d: %s -> %s\n", i, argv[i], argv[i+1]); } 

If you want to print when argv[i] contains a dash, then replace

 argv[i][0] == '-' 

with

 strchr(argv[i], '-') 

in the if statement.

Appendix: I highly recommend that you read http://www.gnu.org/software/libc/manual/html_node/Argument-Syntax.html , which describes how modern UNIX command-line programs should interpret their arguments.

+1
source

When you increment the pointer, you go to the next address after that pointer. So, let's say you are at 0000, the next 0004, 0008, etc. The value at this address can be, for example, a character in a string. Therefore, if the string is "hello", then * c will be "h", and the first * C ++ will be "e".

while loops are usually not the most efficient ways to do something because you can easily get stuck in an infinite loop. It must ensure that each character in the string has been counted.

I'm not quite sure why they assign arg * c.

0
source

All Articles