473,396 Members | 1,891 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,396 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 7621
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.