Structure initialization

gcc 4.4.4 c89

I will add to this list. So I am looking for NULL to finish it. However, I get a warning: "Initialization makes integer from pointer without a cast"

Why does he mention an integer when I have not used in this structure?

I thought in this case arrays and pointers, where is the same?

  struct company_prof { char name[32]; size_t age; char position[32]; } employee[] = { { "Job Bloggs", 23, "software engineer" }, { "Lisa Low" , 34, "Telecomms Technician" }, { "simon smith", 38, "personal assistist" }, { NULL , -1, NULL } }; 

Thanks so much for any suggestions,

+4
source share
3 answers

You are trying to initialize an array of characters with NULL . It makes no sense. For example, you will receive the same warning from

 char a[100] = { NULL }; 

and it doesn't make sense the exact same way.

The "integer" indicated in the diagnostic message is the first element of the character array. char is an integer type in C and when you write

 char a[100] = { NULL }; 

this is an attempt to initialize the 0th element of array a with NULL . On your platform, NULL declared as something with a pointer type, so the diagnostic message says that you are trying to make an integer ( a[0] ) from a pointer ( NULL ) without translation.

An even simpler example might look like this

 char c = NULL; 

and he will receive the same diagnostic message for the same reasons.

May I ask why you are trying to initialize char with NULL ? What were your intentions?

If you are not going to write name and position to arrays after initialization, perhaps you should use pointers instead of arrays, as in

 struct company_prof { const char *name; size_t age; const char *position; } employee[] = { { "Job Bloggs", 23, "software engineer" }, { "Lisa Low" , 34, "Telecomms Technician" }, { "simon smith", 38, "personal assistist" }, { NULL , -1, NULL } }; 

Formally, in this case NULL makes sense. But not in the case of an array.

But less formally, the purpose of this entry { NULL, -1, NULL } at the end of the array is not yet clear to me. Is any final element? Why don't you just use the exact size of the array instead of creating a trailing element?

+5
source

In this case, you statically allocate space for the rows in the structure. Space for strings is not a separate object from the structure, but simply a block of space in it. Therefore, you cannot assign a pointer to these fields because they cannot refer to anything else - these are just convenient names for referring to the beginning of these fragments inside the structure.

The reason you get the conversion bit is because the characters (for example, name[0] ) are small integers, and it tries to put a NULL pointer on the first character of each line. This first character is too small to hold the pointer, so it gives you a warning about this.

You might want to use "\0" instead of NULL here. This is an empty string or empty string, which is slightly different from the null pointer.

Edit. An alternative that you might consider to get the size of the list is not to use a watchdog value, but to define a constant that computes it from the size in bytes. A typical design for this looks like this:

 static const int num_employees = sizeof(employee) / sizeof(employee[0]); 
+5
source

You use char [32] as your structure members. You cannot convert from NULL to char [N]. So what happens is that NULL is treated as zero. The warning comes from a compiler deciding to convert NULL to 0. Since the initialization of char [N] takes place inside the array, initializing it to 0, it simply sets all the values ​​to zero. eg.

struct wrapper {char i [2]} w = {0}

set the inner self to {0,0}

This means that your memory layout is as follows.

  | =============== name ============= || - || ========== position ===== ====== |
 0123456789012345678901234567890101230123456789012345678901245678901
 Job.Bloggs0000000000000000000000 (23) software.engineer00000000000000 // first
 ... // rest
 000000000000000000000000000000000000 (-1) 0000000000000000000000000000000 // sentinel

It may or may not be what you want .. but it means that the cyclic values ​​of this value will not be what you might expect: in particular, this will not work:

 p = employee; while( p.name != NULL ) { printf("%s", p.name); ++p; }; 

It will simply disable garbage printing from memory and, ultimately, segfault.

Instead, you can try something like this

 p = employee; while( p.name[0] != 0 ) { printf("%s", p.name); ++p; }; 

But this will not work for names like "\ 0foo" (which you can consider invalid anyway ...)

+1
source

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


All Articles