Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c

syntax

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.

like image 242
Dervin Thunk Avatar asked Dec 09 '22 20:12

Dervin Thunk


1 Answers

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

like image 195
CAbbott Avatar answered Jan 05 '23 12:01

CAbbott