Two syntaxes for C: no init for and char * w [4] vs char w [4]

Just by studying Jon Bentley's "Pearl Programming" style of C and wondered if anyone could explain the following two parts of the C syntax to me.

A for without an init condition (see line 2 in wordncmp in the file above):

  for ( ; *p == *q; p++, q++) 

and semantic difference between

  char *word[800000]; 

and

  char word[800000]; 

since I thought that arrays were just pointers, in this case word[0] .

Answer by choosing an answer: Well, like the rest of the community, I was torn between accepting a dmckee or CAbbott answer. They both have important knowledge that I value. I agreed with CAbbott's answer because it was simpler but gave an advantage to dmckee. As honestly as possible, not accepting two answers. Thanks.

+4
source share
6 answers

It has been a while, but there should be differences:

 for ( ; *p == *q; p++, q++) 

It is just that the developer does not want to perform initialization before the for loop. In this case, the developer just wants to iterate through the pointers.

 char *word[800000]; and char word[800000]; 

The first declares an array of 800,000 char *, the second declares an array of 800,000 characters

Hope that helps

+6
source

First, p and q already configured ( q pointing to an existing line, and p to the buffer where you plan to copy the line).


In the second case, you have a difference between an array of characters and an array of pointers to characters.

An array of characters will occupy 80000*sizeof(char) bytes (usually 80,000). An array of pointers will take 80000*sizeof(char*) , which is usually 320,000 bytes on a 32-bit system or 640,000 bytes on a 64-bit system.

No array is initialized.

An array of pointers is supposed to point to a group of strings.


Finally: arrays and pointers are not the same thing. I repeat: not the same thing. Just arrays can be freely converted to pointers whenever they need a compiler. So you can use arithmetic of pointers on arrays.

+6
source

There is nothing special in a for loop without an initial condition. The condition will continue to be checked at the beginning of each loop to determine whether to continue, and the last expression will be executed at the end of each iteration.

The for loop in your question:

 for (; *p == *q; p++, q++) 

will loop until the points p and q are equivalent. For example, if p and q are both char * that point to strings, this will loop until the first difference is found in the strings.

+1
source

The first is not too complicated. First, an initializer is not needed because it is assumed that the pointers are already initialized.

 for(i = 0; i < 10; ++i) { } 

equivalently

 i = 0; for ( ; i < 10; ++i) { } 

Then it is a matter of understanding pointers. Although it is useful to understand this example, you should avoid these loops altogether. They are much more difficult to understand (although not so much in this case). The fundamental final condition (when * p! = * Q) in the middle can be confusing for beginners C.

The second point is simple. In the first case, you declare an array where item is char *, where the second is an array of char. You should also take care of declaring such large arrays: if you need large arrays, you must use malloc / free, otherwise you will exhaust your stack.

+1
source

And for a loop without an initial condition, it follows that the initial condition has already been set before the for loop. Maybe some kind of pseudo code like:

 int i if skipFirst then i=1 else i=0 for (; i < 10 ; i++) { ... } 

Secondly, char *word[X] means that each element in the array is a char* string or a string with a terminating zero. char word[X] is a single "string" of length X or a 1-dimensional array of characters with length X.

0
source
 x=a++, b++ means a++ and b++ and x = b operator , can be used like in this example: for (;;printf("hello!"), q++) 
0
source

All Articles