473,387 Members | 3,787 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,387 software developers and data experts.

doubts related to structure

Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70

Regards,
Somenath

Feb 16 '07 #1
4 1244
somenath wrote:
Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
Probably at least 3 compiler warnings.

The output is undefined.

--
Ian Collins.
Feb 16 '07 #2
On 15 Feb 2007 18:45:42 -0800, "somenath" <so*********@gmail.com>
wrote in comp.lang.c:
Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
There is no output defined by the C standard for the turdlet below.
There is no defined output because of the undefined behavior.
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
If your compiler does not emit a diagnostic for the line above, it is
not a C compiler.
++dp ; ++dp ;
Each of the two statements above is undefined behavior.
printf("%x\n", dp->day);
The statement above has undefined behavior.
}
Out put is 70
Maybe the output is 70, maybe it reformats your hard drive, maybe it
makes the Sony battery in your laptop explode into flames. This is
invalid, and very stupid, code, and what it does or does not do has
nothing at all to do with the C language.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
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
Feb 16 '07 #3
On 15 Feb 2007 18:45:42 -0800, "somenath" <so*********@gmail.com>
wrote:
>Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
here the output of "the modificated version" was something like

-----------------------------------
70
1
1
girono=1
mese =1
anno =2006
-----------------------------------
girono dp1=1
mese dp1=1
anno dp1=2006

because ++dp; jump sizeof(MYDATE)==here to 3 ints
but all this is UB (the output dipends from how compiler dispose the
data)
thats UB so if someone run it he/she shold know all can happen
>typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70

Regards,
Somenath

#include <stdio.h>

typedef struct
{ int day;
int month;
int year;
} mydate;

int x = 0x10;
int y = 0x20;
int z = 0x30;

int a = 0x40;
int b = 0x50;
int c = 0x60;

int d = 0x70;

mydate abc = { 1, 1, 2006 };

int main(void)
{mydate *dp = &x, *dp1=&y;

if(sizeof(mydate)!= 3*sizeof(int) || &x+7!=&abc)
{printf("I don't like padding\n");
return 0;
}
++dp; ++dp;
++dp1; ++dp1;

printf("%x\n", dp->day);
printf("%x\n", dp->month);
printf("%x\n", dp->year);

/* here "dp" point to &d */
/* here ((int*)dp + 1) point to &abc */

printf("girono=%d \n", ((mydate *)( (int*)dp + 1))->day);
printf("mese =%d \n", ((mydate *)( (int*)dp + 1))->month);
printf("anno =%d \n", ((mydate *)( (int*)dp + 1))->year);

printf("girono dp1=%d \n", dp1->day);
printf("mese dp1=%d \n", dp1->month);
printf("anno dp1=%d \n", dp1->year);

return 0;
}
Feb 16 '07 #4
somenath wrote:
Hi all,

Can any one let me know what will be the out put of the bellow
mentioned program and why it is so .
typedef struct
{
int day;
int month;
int year;
} MYDATE;
int x = 0x10;
int y = 0x20;
int z = 0x30;
int a = 0x40;
int b = 0x50;
int c = 0x60;
int d = 0x70;
MYDATE abc = { 1, 1, 2006 };

main()
{
MYDATE *dp = &x;
++dp ; ++dp ;
printf("%x\n", dp->day);
}
Out put is 70
Here're the diagnostics my compiler, (gcc), emits when I compile your
code above, with a minor addition, (#include <stdio.h>).

gcc -Wall -Wextra -ansi -pedantic -O4 -o 02 02.c
02.c:21: warning: return type defaults to int
02.c: In function main:
02.c:22: warning: initialization from incompatible pointer type
02.c:25: warning: control reaches end of non-void function

The third warning indicates the first place where you invoke undefined
behaviour by initialising dp, which is a pointer to type MYDATE with a
pointer value of a different type, specifically int. Once you invoke
undefined behaviour, as defined by the C standard, the subsequent
behaviour of the program can be anything.

The two increments of the pointer dp also result in undefined
behaviour, as does the next printf() statement.

Just to illustrate that invoking or relying on undefined behaviour is
foolish, the output I get when I run this program is:
0

You cannot make any assumptions about the physical layout of objects
in C. The standard specifies some of their properties like type, size,
scope and linkage, but not their relative placements.

Feb 16 '07 #5

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

Similar topics

0
by: QWERTY | last post by:
--------------Boundary-00=_O5I3QL80000000000000 Content-Type: Multipart/Alternative; boundary="------------Boundary-00=_O5I3LVC0000000000000" --------------Boundary-00=_O5I3LVC0000000000000...
0
by: abbas reji | last post by:
--0-599929911-1059996886=:4358 Content-Type: text/plain; charset=iso-8859-1 Content-Transfer-Encoding: 8bit Content-Id: Content-Disposition: inline ...
17
by: Steve Jorgensen | last post by:
Terminology question: Is there a term for a set of records related directly or indirectly by key value in several tables? For example, a single invoice record and its line item records -or- a...
7
by: Susan Bricker | last post by:
Greetings. As a relative newcomer to Access, I am having trouble deciding on how to design the form flow for updating and creating related records. I'm looking for a variety of suggestions so...
3
by: Ameya | last post by:
hello, i have a very specific doubt related to file handling. My objective is to make some modifications in the output data file of one software which would be the input data file for another...
1
by: NagaKiran | last post by:
Hi I want to post VBA related doubts. Where can I post my doubts in VBA? thanks bye
1
by: archana | last post by:
Hi all, I want to clear one doubt which i am facing regarding timer. Suppose i have one timer in which i am generating some files. and interval of my timer is say 5 min. Say suppose at...
14
by: ToddLMorgan | last post by:
Summary: How should multiple (related) projects be arranged (structured) and configured so that the following is possible: o Sharing common code (one of the projects would be a "common" project...
4
by: HLCruz via AccessMonster.com | last post by:
I am working with a database that has client information separated in to 4 related tables - tFolder, tAddress, tEmail, tPhone number. In addition there are related tables tGifts and tCalls. The...
24
by: temper3243 | last post by:
Hi, Many people have used this code in my project. It works because b is using the extra memory for other 4 variables.I access obj max. well here are a few questions 1) Where does it fail. I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.