Sponsored Content
Top Forums Programming C- static initialization of structures Post 302744951 by Don Cragun on Sunday 16th of December 2012 01:23:58 AM
Old 12-16-2012
Quote:
Originally Posted by garysk
As I pointed out in my reply to @jimmacnamara, there was no overflow. By means of logic that is not obvious to me, the compiler made certain decisions, which resulted in two things:

1. The data aligned, one comma-separated datum per named member of each instance of the structure within the array of structures. The values expected, except for the NULL-initialized char [5], appeared in their expected places in the array and within every element of the array. If they didn't align one to one, that would have been the time to break out the additional curly braces.

2. The "NULL" initializer of the member array was interpreted as a signal to initialize the member array with null bytes. Somehow, no doubt through application of fundamental rules of the language (which is what is not clear to me), the compiler made the decision to convert the *((void *)0) to (char)0, essentially converting a declared pointer into a declared integer.

To my knowledge there is not a simpler explanation of the decision (the need for that decision that I created by expecting the compiler to resolve ambiguous code notwithstanding) than to ascribe it to datatype promotion/demotion. Typically, though, the compiler balks at pointer-integer conversion, and I would have expected something like that here, rather than silently do as it thought I said.

I think there should have been a warning somewhere, or an error, applied to the construct by the compiler. But alas, none was indicated.

I am aware that the compiler "tries" to do what it asked to do, and to try to make things fit, but if it's not clear, I'd rather have errors and warnings out the kazoo than to have it guess as to what is intended. And I know that the compiler is neither intelligent nor animate, and can only obey the rules given; I am at this point really only interested in understanding the rules that came into play here.
I agree with what Jim McNamara has already said. I hope this may provide a little more that will help explain what you're seeing.

Depending on the programming environment you're using, NULL is likely to be defined to be 0, 0L, or (void *)0. (All three of these are used for one or more of the multiple programming environments provided by OS X and all three of these are legitimate values for NULL according to the C Standard depending on the relative sizes of pointers and integers in your programming environment.)

If NULL expands to 0 in your programming environment, there would be absolutely no difference to the compiler between the initializer:
Code:
NULL, '\0', '\0'

and the initializer:
Code:
'\0', '\0', '\0'

If NULL expands to 0 or 0L, no pointer to integer conversion is needed to turn NULL into an integer constant used to initialize the array of characters in your structure.

I agree that if your compilation environment has NULL defined to be (void *)0 it would have been nice for the compiler to produce a warning, but I don't see anything in the C Standard that would require that to happen since the conversion from a NULL pointer to an integer constant expression is a well defined operation with no loss of data involved.
 

10 More Discussions You Might Find Interesting

1. Programming

Struct Initialization

Hi We are using a code generator for initializing structures with the #define macro. Compiling it with the GCC 2.8.1 (with -ansi) it OK. But when we are using the SUN C 5.0 compiler it screams. Following is a code sample: #include <stdlib.h> #include <stdio.h> typedef struct TEST3 {... (4 Replies)
Discussion started by: amatsaka
4 Replies

2. UNIX for Dummies Questions & Answers

Shell initialization files

As you know, when a user logs in, the shell reads the initialization files in an order something like below... ################### Bourne Shell /etc/profile > $HOME/.profile Bash Shell /etc/profile > $HOME/.bash_profile > $HOME/.bash_login > $HOME/.profile > $HOME/.bashrc C Shell... (3 Replies)
Discussion started by: SeanWuzHere
3 Replies

3. Programming

Char initialization

Hi All, char a="\0"; a) a contains \0 a contains garbage value b) a contains \ a contains 0 a contains garbage value Pls, let me know correct result is a or b. I guess a. Thanks, Naga:cool: (2 Replies)
Discussion started by: Nagapandi
2 Replies

4. IP Networking

I need HELP to Set up Coyote Linux router with 1 static IP & 64 internal static IP

hello, i need help on setting my coyote linux, i've working on this for last 5 days, can't get it to work. I've been posting this message to coyote forum, and other linux forum, but haven't get any answer yet. Hope someone here can help me...... please see my attached picture first. ... (0 Replies)
Discussion started by: dlwoaud
0 Replies

5. Shell Programming and Scripting

little confusion about variable initialization.

Whenever i execute the below scriptlet with out proper file name it deletes /tmp directory . I guess this is because value of variable a didnt get initialized and there for rm -rf /tmp/ get executed and entire /tmp directory get deleted. How would i avoid any empty variables to be used in... (9 Replies)
Discussion started by: pinga123
9 Replies

6. Programming

Class Pointer initialization C++

Hello everyone, I have a question, that are the following ways of pointer intialization same ? ClassA *point; point = 0; point = new ClassA; Thanks a load in advance!! Regards, (10 Replies)
Discussion started by: mind@work
10 Replies

7. Programming

Even the Static cURL Library Isn't Static

I'm writing a program which uses curl to be run on Linux PCs which will be used by a number of different users. I cannot make the users all install curl on their individual machines, so I have tried to link curl in statically, rather than using libcurl.so. I downloaded the source and created a... (8 Replies)
Discussion started by: BrandonShw
8 Replies

8. Shell Programming and Scripting

Initialization error

new to shell scripting. below line is showing error in script. ${parameter:=word} in the o/p first it shows the below error. word: not found. and then in next line print "word" ---------------- p2: word: not found. word --------------------------- OS is AIX and shell is... (12 Replies)
Discussion started by: scriptor
12 Replies

9. Shell Programming and Scripting

Variable initialization

Hallo Team, I have a simple request and i would appreciate your help. I would like to use two dates in my script lets: A=$(date +"%d %B %Y") echo $A 23 June 2014 That's awesome now we cooking. Now i want B to be on the previous month eg: echo $B Should give me 23 May 2014 I would... (9 Replies)
Discussion started by: kekanap
9 Replies

10. Shell Programming and Scripting

Error occurred during initialization of VM

Hi , I was invoking a sh file using the nohup command. But while invoking, I received a below error. Error occurred during initialization of VM Unable to load native library: /u01/libjava.so: cannot open shared object file: No such file or directory . Could you please help out. Regards,... (2 Replies)
Discussion started by: Kamal1108
2 Replies
All times are GMT -4. The time now is 11:07 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy