Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the use of Struct Tag name in C programming?

Tags:

c

struct

I want to know the actual use of struct tag_name in C programming. Without using the tag_name also i was getting output as with use of tag_name. I want the exact reason behind this process.

For ex:

//With tag_name st1
struct st1 { int x; char c;}x={100,'a'},y={70,'e'};

//Without any tag_name
struct { int x; char c;}x={100,'a'},y={70,'e'};

printf("x.x= %d \t x.c= %c \n",x.x,x.c);   //Output: x.x=100    x.c=a
printf("y.x= %d \t y.c= %c \n",y.x,y.c);   //Output: y.x=70     y.c=e
like image 232
user6677473 Avatar asked May 25 '17 12:05

user6677473


People also ask

Why we use struct keyword?

The struct keyword defines a structure type and/or a variable of a structure type.

What is the use of struct data type?

A struct data type represents a collection of elements of different data types. A struct data type has an associated schema that defines the structure of the data. To pass, generate, or process struct data, assign struct data type to ports.

How does a struct work in C?

A struct in the C programming language (and many derivatives) is a composite data type (or record) declaration that defines a physically grouped list of variables under one name in a block of memory, allowing the different variables to be accessed via a single pointer or by the struct declared name which returns the ...

What is the benefit of struct?

Advantages of structure Structures gather more than one piece of data about the same subject together in the same place. It is helpful when you want to gather the data of similar data types and parameters like first name, last name, etc.


1 Answers

A struct declaration tells the compiler how a struct looks like and optionally gives that struct a name. If you want to use a struct as a "type" in your code, it requires a name:

struct coordinate {
  int x;
  int y;
};

// Can now be used as a return type:
struct coordinate getCoordinate ( ) { ... }

// Can now be used as an argument:
void drawAtPoint ( struct coordinate point ) { ... }

// Can be used to create new constants or variables:
const struct coordinate kOrigin = { 0, 0 };
struct coordinate lastViewPosition;

Alternatively you can create a nameless struct and explicitly define it to be a new type using a type definition:

typedef struct {
  int x;
  int y;
} coordinate;

// Can now be used as a return type:
coordinate getCoordinate ( ) { ... }

// Can now be used as an argument:
void drawAtPoint ( coordinate point ) { ... }

// Can be used to create new constants or variables:
const coordinate kOrigin = { 0, 0 };
coordinate lastViewPosition;

But if you don't do either one, you cannot use that struct as a type since in C, the type of a struct is defined by its name and not by its data layout.

The following is valid C code:

struct coordinate {
  int x;
  int y;
};

struct coordinate startCoord = { 20, 80 };
struct coordinate endCoord = startCoord;

Yet the following is not:

struct {
  int x;
  int y;
} startCoord = { 20,  80 };

struct {
  int x;
  int y;
} endCoord = startCoord;

Compiler will fail with an error. It seems as if startCoord and endCoord have the same type in both examples, but that's not true. Two structs don't denote the same type just because they have an equal memory layout or equal field names. For a C compiler, startCoord and endCoord have different types in the later example and thus you cannot assign them as shown above as this requires that both of them have the same type.

So the only situation where you can skip naming your structs is when you directly declare variables of that struct type and there is no requirement to ever refer to that struct again as a data type throughout your code.

like image 111
Mecki Avatar answered Oct 01 '22 16:10

Mecki