-1

I am curious about struct that we declare in C. What does it actually do ? I know we can make nodes & pointers to those using struct but how does it function ? Like a while loop checks the condition & branches accordingly if equal or not equal. What does struct do under the hood ?

user3181231
  • 177
  • 1
  • 2
  • 8

4 Answers4

3
  • A struct type is a user-defined composite type. It is composed of fields or members that can have different types.

    From struct-wiki:

    A struct in the C programming language is a declaration that defines a list of variables to be placed under one name in a block of memory, allowing the different variables to be accessed via a single pointer.

  • For memory allocation of a struct: check out How are C struct members allocated?

  • For why to use struct, check out Why should we typedef a struct so often in C?

Community
  • 1
  • 1
herohuyongtao
  • 49,413
  • 29
  • 133
  • 174
1

Its nothing more than multiple variables treated as one entity. There is not much magic behind this, the values simply appear behind each other in the order of declaration in the memory.

Sebastian Hoffmann
  • 11,127
  • 7
  • 49
  • 77
1

A C struct simply represents data in a specified way and doesn't do anything at all. It is used to represent a more complex data-type, such as a linked-list node.

Jaffa
  • 12,442
  • 4
  • 49
  • 101
1

Using struct a user can define its own required data type to handle complex data. just like array where all elements in array are of same type but in struct each element can be defined in user's desired way.

so struct is used to define ""user-defined data types".
Prashant Anuragi
  • 390
  • 4
  • 14