tags:

views:

195

answers:

6

Just studying the C style of Jon Bentley's Programming Pearls and was wondering if someone could please explain to me the following two pieces of C syntax:

A for with no init condition (see line 2 in wordncmp in the file above):

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

and the semantic difference between

            char *word[800000];

and

            char word[800000];

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

Answer choice explanation: Ok, as the rest of the community, I was torn between accepting dmckee or CAbbott answer. They both have important pieces of knowledge that I appreciate. I've accepted CAbbott answer because it was simpler, but gave an upvote to dmckee. As fair as I could go without accepting two answers. Thanks.

+6  A: 

In the first place, p and q are already set up (q pointing to an existing string, and p to a buffer where you plan to copy the string).


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

The array of characters will take up 80000*sizeof(char) bytes (usually 80000). The array of pointers will take up 80000*sizeof(char*) which is generally 320000 bytes on a 32 bit system or 640000 bytes on a 64 bit system.

Neither array is initialized.

The pointer array is presumably intended to point to a bunch of strings.


Finally: arrays and pointers are not the same thing. I repeat: not the same thing. It is just that arrays can be freely converted to pointers whenever the compiler need them to be. SO you can use pointer arithmetic on arrays.

dmckee
A: 

A for loop with no initial condition implies that the initial condition was already set before the for loop. Perhaps some pseudocode like:

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

Second, char *word[X] means that each element in the array is a char* or a null-terminated string, more likely. char word[X] is a single "string" of length X, or a 1-dimensional array of chars, with length X.

Mark Rushakoff
+1  A: 

There is nothing special about a for loop without an initial condition. The condition will still be checked at the beginning of each loop to determine if it should continue and the last expression will still be executed at the end of each iteration.

The for loop in your question:

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

will loop as long as what p and q are pointing to are the 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.

Trent
+6  A: 

It's been a while, but here should be the differences:

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

This is simply that the developer doesnt want to do any initialization prior to the for loop. In this case the developer simply wants to iterate through the pointers.

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

The first declares an array of 800000 char*, the second is an array of 800000 chars

Hope that helps

CAbbott
+1  A: 

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

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

is equivalent to

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 those loops in general. They are much harder to understand (although not so much in that case). The underlying end condition (when *p != *q) in the middle may be confusing for C beginners as well.

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

David Cournapeau
A: 
x=a++, b++ means a++ and b++ and x = b
operator , can be used like in this example:
for (;;printf("hello!"), q++)
f0b0s
That wasn't the part of the loop that was puzzling the OP.
Jonathan Leffler
"x=a++,b++" means "x=a++" and "b++". comma binds less strong than assignment. If you are going to dare to post a puzzling post, then at least try and make it error free :D
Johannes Schaub - litb