Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unusual struct in c

Tags:

c

struct

how should I understand the array of two strings?

static struct S1 {
    char c[3], *s;
} s1 = {"abc", "def" };

Probably the question is not correct but I have difficulties to understand how it works

like image 250
cellka Avatar asked Jan 27 '23 22:01

cellka


1 Answers

S1.c has space for 3 bytes and S1.s is a pointer to a string.

the first part defines the structure:

struct S1 {
    char c[3], *s;
};

The next part creates an instance of this type and initializes it with a few values:

static struct S1 s1 = {"abc", "def" };

static is not part of the struct definition. It refers to the visibility of the instance variable.

like image 117
Jörg Beyer Avatar answered Feb 08 '23 21:02

Jörg Beyer