Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a full declarator in C?

Tags:

c

syntax

Harbison/Steele's C reference says that the end of a full declarator is a sequence point. But what is a full declarator?

"A full declarator is a declarator that is not part of another declarator"

... what?

So as an example, would it be guaranteed by the C standard that int i = 0, *j = &i stores the memory address with the value of the variable i in the pointer j?

In other words, is the int i = 0 part a full declarator?

like image 382
viuser Avatar asked Mar 17 '16 20:03

viuser


People also ask

What is Declarator in C?

A declarator is the part of a declaration that specifies the name to introduce into the program. It can include modifiers such as * (pointer-to) and any of the Microsoft calling-convention keywords. Microsoft Specific. In this declarator, C Copy.

What is a declarator in c++?

A declaration means (in C) that you are telling the compiler about type, size and in case of function declaration, type and size of its parameters of any variable, or user-defined type or function in your program. No space is reserved in memory for any variable in case of the declaration.


1 Answers

No, you are mixing declarations and declarators.

Let me quote pieces of the C grammar from the standard:

declaration:
    declaration-specifiers init-declarator-list[opt] ;

init-declarator-list:
    init-declarator-list , init-declarator

init-declarator:
    declarator
    declarator = initializer

And then:

declarator:
    pointer[opt] direct-declarator

direct-declarator:
    identifier
    ( declarator )
    ....

TL;DR: int i = 0; is a declaration. The i part is a declarator.

The part about full declarators is clear if you have, for example, pointers. This line:

int *p[3] = { 0 };

is a declaration. The part *p[3] is the full declarator, but p[3] and p are also (non-full) declarators.

And asking your first question, yes, int i = 0, *j = &i; is perfectly fine, because there are two full declarators: i and *j. There is a sequence point at the end of each full declarator plus another sequence point at the end of each initializer. You could even write void *p = &p; and it would be just fine.

like image 165
rodrigo Avatar answered Oct 28 '22 15:10

rodrigo