struct's first member and the NULL pointer

S

sathyashrayan

The standard confirms that the following initialization of a struct

struct node
{
---
---
}


struct node var[3] = {NULL};
My doubts are followed:
1) When I set the high warning level of the compiler (-Wall) that
gives some warnings. What are those warnings?

2) Is there any difference in the above initialization if I declare
the struct's first element as a value or a pointer? Does this have any
impact on the usage of NULL pointer in
the above context of initialization?
#include <stdio.h>

struct node
{
int a;
char *b;
};

int main(void)
{
struct node var[3] = {NULL} ; /*a is not a pointer.So is this
initialization correct*/
printf("%s %d\n",var[0].b,var[0].a);
var[0].b = "sathya";
var[0].a = 10;
printf("%s %d\n",var[0].b,var[0].a);
return 0;
}

My understanding of the var variable is this
"var is a array of 4 elements in which all the 4 elements are set
to NULL"
So with a single initialization all the array's value is set to be
NULL?
Is there any wrong understanding?


3)I searched the above questions in the faq but I don't get the
exacat one.Is it in the faq?

The above questions is based on a thread named "NULL pointer and zero
value"
in clc.
 
E

E. Robert Tisdale

sathyashrayan said:
The standard confirms that the following initialization of a struct

struct node {
---
---
}

struct node var[3] = {NULL};
My doubts are followed:
1) When I set the high warning level of the compiler (-Wall) that
gives some warnings. What are those warnings?

2) Is there any difference in the above initialization if I declare
the struct's first element as a value or a pointer? Does this have any
impact on the usage of NULL pointer in
the above context of initialization?
#include <stdio.h>

struct node {
int a;
char *b;
};

int main(void) {
struct node var[3] = {NULL} ; /*a is not a pointer.
So is this initialization correct*/
printf("%s %d\n",var[0].b,var[0].a);
var[0].b = "sathya";
var[0].a = 10;
printf("%s %d\n",var[0].b,var[0].a);
return 0;
}

My understanding of the var variable is this
"var is a array of 4 elements in which all the 4 elements are set
to NULL"
So with a single initialization all the array's value is set to be
NULL?
Is there any wrong understanding?

When I compile your code

> gcc -Wall -std=c99 -pedantic -o main main.c
main.c: In function `main':
main.c:9: warning: missing braces around initializer
main.c:9: warning: (near initialization for `var[0]')
main.c:9: warning: initialization makes integer \
from pointer without a cast

Try this:
> cat main.c
#include <stdio.h>

typedef struct node {
int a;
char *b;
} node;

int main(int argc, char* argv[]) {
//node var[3] = {NULL}; // a is not a pointer.
// So is this initialization correct?
node var[3] = {{0, NULL}};
fprintf(stdout, "%s %d\n", var[0].b, var[0].a);
var[0].b = "sathya";
var[0].a = 10;
fprintf(stdout, "%s %d\n", var[0].b, var[0].a);
return 0;
}
> gcc -Wall -std=c99 -pedantic -o main main.c
> ./main
(null) 0
sathya 10
 
J

Jack Klein

The standard confirms that the following initialization of a struct

Chapter and verse, please. The C standard says no such thing.
struct node
{
---
---
}


struct node var[3] = {NULL};

This might work or it might not, depending on two things, the actual
definition of struct node, and the choice the compiler makes for the
definition of the macro NULL.
My doubts are followed:
1) When I set the high warning level of the compiler (-Wall) that
gives some warnings. What are those warnings?

I don't know, what are they? Had you copied them and pasted the text
into your message, I might be able to tell you.
2) Is there any difference in the above initialization if I declare
the struct's first element as a value or a pointer? Does this have any
impact on the usage of NULL pointer in
the above context of initialization?

There is no such thing as "NULL pointer". There is the macro NULL,
which the C language standard allows a compiler to define in one of
two ways (not the exact text you understand, but equivalent to the two
possible allowed definitions):

#define NULL 0; /* or some other integer expression equal to 0 */

#define NULL (void *)0;

But the macro NULL is not a "NULL pointer", of which there is no such
thing, or even a "null pointer". It is a "null pointer constant", a
value, not a pointer at all. If the value of the macro NULL is
assigned to any pointer type, that pointer will then be a null
pointer.
#include <stdio.h>

struct node
{
int a;
char *b;
};

int main(void)
{
struct node var[3] = {NULL} ; /*a is not a pointer.So is this
initialization correct*/

The initialization above is equivalent to writing:
{ { NULL, 0 }, { 0, 0 }, { 0, 0 } };

....that is, you are trying to initialize var[0].a to NULL, and provide
default initialization for the rest of var[0] and all the other
members of the array.

That will work if your compiler defines NULL as just plain 0, and
require a diagnostic if your compiler defines it as (void *)0, because
you are trying to initialize an int with a pointer value, and that is
invalid without a cast. And questionable with a cast.
printf("%s %d\n",var[0].b,var[0].a);
var[0].b = "sathya";
var[0].a = 10;
printf("%s %d\n",var[0].b,var[0].a);
return 0;
}

My understanding of the var variable is this
"var is a array of 4 elements in which all the 4 elements are set
to NULL"

Conceptually, you can't initialize objects to NULL unless they are
pointers. There is no such thing as a null int, or a null double, or
a null struct. Whether your compiler will actually accept it or not
depends, as I have said, on which of the two allowed forms for the
macro NULL it decided to provide.
So with a single initialization all the array's value is set to be
NULL?

No, you can't conceptually set a struct to NULL.
Is there any wrong understanding?

Yes, if you want proper default initialization for any object you
define in a C program, whether an arithmetic type like int or double,
or an array, struct, or union, or an array of structs, or anything at
all, do this:

= { 0 };

Note 0, not NULL. NULL will work on some compilers and break on
others. = { 0 }; can be used on any data type and will initialize all
integer types to 0, floating point types to 0.0, and pointers to NULL.
On every single C compiler everywhere.
3)I searched the above questions in the faq but I don't get the
exacat one.Is it in the faq?

The above questions is based on a thread named "NULL pointer and zero
value"
in clc.

You may always use plain old ordinary 0 in place of the macro NULL.
You may not always use the macro NULL in place of plain old 0.
 
C

Christian Bau

#include <stdio.h>

struct node
{
int a;
char *b;
};

int main(void)
{
struct node var[3] = {NULL} ; /*a is not a pointer.So is this
initialization correct*/
printf("%s %d\n",var[0].b,var[0].a);
var[0].b = "sathya";
var[0].a = 10;
printf("%s %d\n",var[0].b,var[0].a);
return 0;
}

NULL can be defined in different ways, depending on your implementation:
NULL is either defined as an integer constant with a value of 0, or such
a constant cast to (void *). So you could have

#define NULL 0
#define NULL 0ul
#define NULL (45-42-3) // Bit weird, but legal
#define NULL ((void *) 0)

An initialisation

char* p = NULL;

is correct on every compiler. But an initialisation

int x = NULL;

will only compile if NULL happens to be defined as an integer, and is
absolutely not portable. It looks to me as if your example may compiler
on some compilers, but not on others. If it compiles, then var[0].a is
explicitely set to 0 because NULL was a zero, the rest is initialised to
zeroes. Lets say you wrote

struct node var[3] = { 999 };

then var[0].a would be 999, the rest would be zeroes.


My understanding of the var variable is this
"var is a array of 4 elements in which all the 4 elements are set
to NULL"
So with a single initialization all the array's value is set to be
NULL?
Is there any wrong understanding?

No, the single initialisation value is _not_ repeated. Everything that
doesn't have an initialiser is set to zero, no matter what values you
include in the list.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top