tayash.blogg.se

Indirection operator do
Indirection operator do












T *p // p is a pointer to T, for any type T In a declaration, the declarator introduces the name of the thing being declared ( p) along with additional type information not given by the type specifier ("pointer to"): T v // v is a single object of type T, for any type T

Indirection operator do plus#

The type of p is "pointer to int" this type is fully specified by the combination of the type specifier int plus the declarator *p. The int-ness of p is specified by the type specifier int, while the pointer-ness of p is specified by the declarator *p. Only the *,, and () operators have any meaning in declarations (C++ adds &, but we won't go into that here). apply to the variable not the base type (that is by adding all the permissible parenthesis). Perhaps you can see how the parenthesis, star, etc. I mention that as another example, though, of why programmers prefer one form over the other, so consider int (*p) vs int(* p). This is the same as saying an (non-declarative) expression (i) >= (0) is the same as i >= 0, as you can always legally add ()'s. To add to that int (*p) is a legal declaration, these parens are simply grouping and in this simple case, and do nothing different than int *p. int* p and (2) declaring only a single variable per declaration, even though the language allows more. This is why experienced programmers tend to prefer (1) int *p vs. in the same declaration statement) - each variable starts over with the same base type as the (eventual) target, and then gets its own (opportunity to apply or not) pointer, array, and function operators to complete its type. Such a star is indeed part of the type of the individual variable being declared however, as other posters have noted, it does not (more precisely, it cannot) be carried over to other variables being declared at the same time (i.e. So, yes, you are right, these are indeed the same operators applied to declaration expressions, though as you've observed only some of the operators make sense (e.g. The syntax is an attempt to make the declaration and the use agree it works well for the simple cases, but it can be confusing for the harder ones, because declarations cannot be read left to right, and because parentheses are over-used. IIRC, I think that Kernigan or Ritchie called it an experiment! Here's some marginally supporting text:īack in the 70's, when Kernighan and Ritchie were writing The C Programming Language, they were pretty honest about how declarations can quickly become unreadable by the untrained eye:Ĭ is sometimes castigated for the syntax of its declarations, particularly ones that involve pointers to functions. It's just a declaration expression that reflects potential usage expressions. You make such differentiated use of lexical language element everyday when you speak English: "a handle" designates something and "to handle" means doing something, two completely different grammatical usages of the same lexical element. Remark: There is no contradiction in this. This makes indeed * part of a derived type ( * means there "pointer", but it needs always another type to say to what kind of thing it points to something). Type specified for ident is ‘‘derived-declarator-type-list If, in the declaration ‘‘T D1’’, D1 has the formĪnd the type specified for ident in the declaration ‘‘T D’’ is ‘‘derived-declarator-type-list T’’, then the The section 6.7.6.1 about pointer declarators is more precise: But * is only interpreted as operator according to the grammatical rules that make valid expressions.īut there's also a chapter 6.2 on concepts, that explains that pointers to a type are derived types. In the C11 standard chapter 6.5 on expressions, * is defined as unary operator (section 6.5.3.2, for indirection) and as multiplicative operator (section 6.5.5), exactly as you've described.

indirection operator do

Depending on context, it may specify an operation to be

indirection operator do

A punctuator can have different meanings depending on how it is used:Ī punctuator is a symbol that has independent syntactic and semantic

indirection operator do

* is a lexical element called a punctuator (as are for example ()+). The smallest pieces of the C language are lexical tokens, such as keywords (e.g.












Indirection operator do