C language assessment

struct 
{
    int a[2], b;
} 
arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

How to evaluate this string in C? The general declaration of the structure is different from this statement. You can also get access to an element in C, for example like [0].a, [0].b?

+4
source share
6 answers

The first lines are the definition of a new type of structure:

struct {
   int a[2], b;
}

It declares a structure with two members: an array of two intwith the name aand int b.

Further it can be decomposed as follows: first the variable:

arr[]

arr, . , ( ):

{ [0].a = ... }

C (C99, ...) : .

-, , ( ). [0] ( ), [0].a a, . { 1 }. , : 2, { 1 } 1, 0 ( ints). Etc.

:

{[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

arr :

  • 2
  • a, 1,0, b, 1
  • a 2,0, b 2

, - :

struct { ... } arr[2];
arr[0].a[0] = 1;
arr[0].a[1] = 0;
arr[0].b = 1;
arr[1].a[0] = 2;
arr[1].a[1] = 0;
arr[1].b = 2;

[0] () , , , arr[0]...

+9

, , , =, , . , , .

, . :

struct {int a[2], b;} arr[];
arr[0].a = {1};
arr[1].a = {2};
arr[0].b = 1;
arr[1].b = 2;

arr 2, . arr[0] , {{1}, 1}. arr[1] {{2}, 2}.

+3

C, , [0].a, [0].b?

TL; DR .


: .

.

designator:
[ constant-expression ]
. identifier

,

struct 
{
    int a[2], b;
} 
arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

arr ( , 1) .

, 1, arr 2 ( 0).

, [0].a = {1} a arr[0] 1. arr[0].a[0]. , .

, , a[0] a[1]. - " " 2 ( ), arr[0].a[0] 1 arr[0].a[1] 0.


1:

C11, §6.7.9/P22

, . .

2:

C11, §6.7.9/P21 ( )

, , , , , , , , .

+3

. :

// define the type
struct foo { ... };

// define a variable of that type
struct foo x;

:

// define a type and a variable of that type
struct foo { ... } x;

:

// define a variable of an unnamed struct type
struct { ... } x;

struct { int a[2], b; }, , ints, a, int b.

arr. [] , .

- :

// define an array of 2 elements
int arr[2];

:

// define and initialize an array of 2 elements
int arr[2] = { 100, 200 };

, ; :

// define and initialize an array of 2 elements
int arr[] = { 100, 200 };

, - :

struct { int a[2], b; } arr[] = { { {1}, 1 }, { {2}, 2 } };
//                                  ^a^         ^a^
//                                ^^struct^^  ^^struct^^ 

, (int a[2]), , (arr[]). , a s: , 0. , {1, 0} {2, 0}.

. , C99, " ". ; C99 "", , . ([ ]), ., . : [0].b = 42 " b 0 42".

:

struct { int a[2], b; } arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

, :

struct { int a[2], b; } arr[] = {[0].a = {1}, [0].b = 1, [1].a = {2}, [1].b = 2};

:

struct { int a[2], b; } arr[] = {[0] = { .a = {1}, .b = 1 }, [1] = {.a = {2}, .b = 2} };

, ( arr 2) .

:

struct { int a[2], b; } arr[] = { { {1}, 1 }, { {2}, 2 } };
+2

struct [0].a, [0].b C99.

[index] .fieldname = , , .

.

+1

.

gcc ubuntu linux 16.04

:

warning: missing initializer for field 'b' of 'struct <anonymous>' [-Wmissing-field-initializers]
arr[] = {[0].a = {1}, [1].a = {2}, [0].b = 1, [1].b = 2};

note: 'b' declared here
int a[2], b;

.

repost

0
source

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


All Articles