Clear, simple explanation of what structure is in C

I already have some idea, but I thought it would be nice to get some impressions from the wonderful people here in sof.

Please let me know if my question is too broad or vague.

+3
source share
4 answers

The question is a bit broad, but ...

A structure is an aggregate or composite data type used to represent objects that are described by several attributes of potentially different types. Some examples:

  • A point in three-dimensional space represented by 3 real coordinates x, y and z;
  • Postal address indicated by street name, house or apartment number, city, state, postal code;
  • -, , , ;
  • A node , , , ;

.. .. ..

. :

struct Address {
  char *streetName; 
  int buildingNumber;  // House, apt building, office building, etc.    
  char *aptNumber;     // Handles apt and suite #s like K103, B-2, etc.
  char *city;
  char state[3];
  int zip;
};

:

struct Address newAddress;

:

struct Address *addrPtr = &newAddress;

. -> , struct struct:

newAddress.streetName = strdup("Elm");
addrPtr->buildingNumber = 100;
...

- , .

+8

.

+6

, , , (C99 6.2.5 §20):

A structure type describes a sequentially distributed non-empty set of member objects (and, in certain circumstances, an incomplete array), each of which has a specified name and, possibly, a different type.

+2
source

From MSDN -

The structure type is a custom composite type. It consists of fields or members that can be of different types.

0
source

Source: https://habr.com/ru/post/1790434/


All Articles