What does this pointer of type structure definition mean (in C)? What does this pointer of type structure definition mean (in C)? c c

What does this pointer of type structure definition mean (in C)?


The struct keyword works sort of like an extended version of typedef, except that you create a complex custom type (called a structure) instead of aliasing an existing type. If you only have one thing that needs to use the declared type, you do not need to provide an explicit name for the type.

The first statement you are looking at declares a structure with two fields, but does not name it. This is called an anonymous structure. The declaration does, however, provide a pointer of that type.

One possible use-case for such a declaration is when you make a header for an external library, possibly one that is not even written in C. In that case, the type of the structure may be opaque or incomplete, and you just need to have a convenient reference to some parts of it. Making the structure anonymous prevents you from being able to allocate it yourself easily, but allows you to interact with it through the pointer.

More commonly, you will see this notation used in conjunction with named or at least aliased structures. The second statement could be rewritten as

struct example { ... } s1, *ptr;

In that case, struct example *ptr = &s1; would be just ptr = &s1;.

An even more common occurrence is the use of anonymous structures with typedef, create custom type names that do not include the struct keyword. Your second example could be rewritten as

typedef struct { ... } example, *pexample;example s1;pexample ptr; // alternatively example *ptr;ptr = &s1;

Note that the type of s1 is example and not struct example in this case.


For starters consider the following simple program

#include <stdlib.h>#include <stdio.h>#include <string.h> int main(void) {    struct {        int len;        char *str;    } *p;    p = malloc( sizeof( *p ) );    p->str = "Hello Nihal Jain";    p->len = strlen( p->str );    while ( *p->str ) putchar( *p->str++ );    putchar( '\n' );    free( p );    return 0;}

Its output is

Hello Nihal Jain

So in this declaration

    struct {        int len;        char *str;    } *p;

there is declared a pointer of the type of the unnamed structure. The pointer itself is not initialized. You could write for example

    struct {        int len;        char *str;    } *p = malloc( sizeof( *p ) );

For this simple program the name of the structure is not required because neither declaration of an object of the structure type is present or needed in the program.

So you can not declare an object of the structure type but it is not required in this case.

According to the C Standard structure or union are declared like

struct-or-union-specifier:    struct-or-union identifieropt { struct-declaration-list }    struct-or-union identifier

It is seen that the identifier is optional if there is a struct-declaration-list. So an unnamed structure can be used as a type specifier.

One more example with using enumerations. You can declare enumerators without declaring the enumeration type. For example

enum { EXIT_SUCCESS = 0, EXIT_FAILURE = -1 }; 

You can use the enumerators that have the type int without declaring an object of the enumeration type if there is no such a requirement in the program.


Also another use (of anonymous structures) would be to use them inside unions or other structs which basically limits the use of that struct to that particular parents union or structure not anything else, which is quite useful from programmer's point of view because looking at a code like this it provides us with the information that it is used locally on the context of the struct or union inside it - nothing more than that. (also saves us from naming).


Also if you use this anonymous structure you wont be able to allocate one instance of it other than the one which already exists.

Example:-(stark's comment: how to use it?)

struct {  int a;  int b;} p;scanf("%d",&p.a);