473,246 Members | 1,355 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,246 software developers and data experts.

struct's first member and the NULL pointer

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.
Nov 14 '05 #1
3 7612
sathyashrayan wrote:
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

Nov 14 '05 #2
On 10 Feb 2004 21:10:49 -0800, sa************@yahoo.co.in
(sathyashrayan) wrote in comp.lang.c:
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Nov 14 '05 #3
In article <23**************************@posting.google.com >,
sa************@yahoo.co.in (sathyashrayan) wrote:
#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.
Nov 14 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: jimjim | last post by:
Hello, Any help will be much appreciatted. My problem is as follows: I declare as global variables: typedef struct _Node{ ..; ..;}Node; Node *Graph; in a function called initiallise(), I...
15
by: fix | last post by:
Hi all, I am writing a program using some structs, it is not running and I believe it is because there's some memory leak - the debugger tells me that the code causes the problem is in the malloc...
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
5
by: Johs32 | last post by:
I have a struct "my_struct" and a function that as argument takes a pointer to this struct: struct my_struct{ struct my_struct *new; }; void my_func(struct my_struct *new); I have read...
4
by: Michael Brennan | last post by:
I have a menu_item structure containing an union. func is used if the menu item should use a callback, and submenu if a popupmen should be shown. struct menu_item { enum { function, popup }...
11
by: redefined.horizons | last post by:
First, I would thank all of those that took the time to answer my question about creating an array based on a numeric value stored in a variable. I realize after reading the responses and doing...
7
by: CaptainBly | last post by:
Okay I've been spending lots of time on this and it's giving me a migraine so I go humble and ask the guru's here. I have several structs typedef struct vert { int x,y,z; struct vert *...
5
by: ssylee | last post by:
I'm not sure if I can initialize members of a struct the lazy way. For example, let's say I have a struct defined as below: typedef struct _ABC { BOOL A; BOOL B; BOOL C; } ABC;
4
by: Sheldon | last post by:
Hi, I have a unique case where I need an array of structs that grows and within this array is another struct that grows in some cases. I'm having trouble allocating memory. Since I have never...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: marcoviolo | last post by:
Dear all, I would like to implement on my worksheet an vlookup dynamic , that consider a change of pivot excel via win32com, from an external excel (without open it) and save the new file into a...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.