Novice question.

Bryan Morse morse at currituck.cs.unc.edu
Sun Nov 4 02:26:01 AEST 1990


In article <336 at brat.UUCP> donn at brat.UUCP (Donn Pedro) writes:
>A question for you that know, because I dont.
>
>
>I understand that the following is a pointer.
>
>> 	char *s;
>
>But what is this doing?
>Why the two "*". 
>
>> 	STRING **s_array;
>
>Please e-mail.

I'm posting my reply since the mail I sent you bounced.  Besides, maybe
someone else wants to know (and if someone doesn't, my apologies).

First, the real way to read C declarations.

Any statement of the form

        type <list of expressions>

says that all expressions in the list are of the specified type.

Thus, the statement

        char *s;

does not (repeat, does *not*) say that s is a pointer to a char.  What
it says is that *s is a char.  It may seem the same (and in reality is)
but it's important to make the distinction.  That's why you can say

        char c, *s;

and declare both a char and a pointer to one.  A very common mistake is to
say "hmm, I want to declare a few char pointers" and type

        char *  s,t;
        ------  ---
        type    list of variables

and this is WRONG!  Be very careful to think in terms of this C style
instead of the Pascal style where you specify the type (including pointers)
and a list of variables.  Does this make sense?

Having said this, let's get to your question:

        STRING **s;

says that **s is of type STRING.  Since *s is what s points to and **s is
what *s points to (you can chain dereferences in C like what--similar to
p^^ in Pascal if you've every seen it) that means that s is a pointer to
a pointer to a STRING.  Make sense?

Seriously, once you learn this simple rule of how to read C declarations,
you can understand even the most convoluted declarations.

Hope this helps..

Bryan Morse                University of North Carolina at Chapel Hill
morse at cs.unc.edu           Department of Computer Science



More information about the Comp.lang.c mailing list