473,387 Members | 1,844 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.

sizeof(struct emp) = 0

Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };

int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

puts ("");
return (EXIT_SUCCESS);
}

i have seen this same code giving o/p as 1 in other compilers
Feb 10 '08 #1
7 3171
On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };
This isn't valid C. C requires at least one member in every structure.
That said, ...
i have seen this same code giving o/p as 1 in other compilers
....why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?
Feb 10 '08 #2
so**********@gmail.com wrote:
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>
Some white space might improve readability
>
struct emp
{ };
Gives me an error "expected a declaration"
int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));
sizeof() returns size_t, %d expects int.
>
puts ("");
Not needed if the printf string would end with a \n
return (EXIT_SUCCESS);
() not needed, return is not a function
}

i have seen this same code giving o/p as 1 in other compilers
Which compiler and where they called in conforming mode? Conforming to which
version of the standard?

Bye, Jojo
Feb 10 '08 #3

<so**********@gmail.comschreef in bericht
news:bb**********************************@s13g2000 prd.googlegroups.com...
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };

int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

puts ("");
return (EXIT_SUCCESS);
}

i have seen this same code giving o/p as 1 in other compilers
empty structs is illegal C
It is legal C++ though, there the size of an empty struct is always 1. Maybe
you were confused with that?

Feb 10 '08 #4
Harald van Dijk <tr*****@gmail.comwrites:
On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
>Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };

This isn't valid C. C requires at least one member in every structure.
That said, ...
>i have seen this same code giving o/p as 1 in other compilers

...why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?
Padding.

--
Keith Thompson (The_Other_Keith) <ks***@mib.org>
Nokia
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
Feb 10 '08 #5
On Sun, 10 Feb 2008 12:36:05 -0800, Keith Thompson wrote:
Harald van Dijk <tr*****@gmail.comwrites:
>On Sun, 10 Feb 2008 01:30:37 -0800, sophia.agnes wrote:
>>Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };

This isn't valid C. C requires at least one member in every structure.
That said, ...
>>i have seen this same code giving o/p as 1 in other compilers

...why would you expect any bytes to be required to store nothing?
Shouldn't you be asking why other compilers make sizeof(struct emp)
anything other than zero? Or do you already know why?

Padding.
In C, padding is not allowed at the immediate start of a structure.

More seriously, padding is used to ensure the structure's members are
properly aligned, and there's no problem with alignment of an empty
structure's members. There's also no problem with an array of zero-byte
objects. They all have the same address, so they are all identically
aligned.

Of course, the fact that they all have the same address is why a certain
other language places extra requirements on empty structures, but
extensions by any C compiler don't have to follow rules for other
languages.
Feb 10 '08 #6
On Sun, 10 Feb 2008 11:53:57 +0100, "Serve Laurijssen" <ni@hao.com>
wrote in comp.lang.c:
>
<so**********@gmail.comschreef in bericht
news:bb**********************************@s13g2000 prd.googlegroups.com...
Dear all,

why the following program is giving o/p as

sizeof(struct emp) = 0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };

int main(void)
{
printf("\n sizeof(struct emp) = %d", sizeof(struct emp));

puts ("");
return (EXIT_SUCCESS);
}

i have seen this same code giving o/p as 1 in other compilers

empty structs is illegal C
It is legal C++ though, there the size of an empty struct is always 1. Maybe
you were confused with that?
<off-topic>

The size of an empty struct in C++ is greater than 0. There is
neither requirement nor guarantee that it be exactly equal to 1.

</off-topic>

--
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.club.cc.cmu.edu/~ajo/docs/FAQ-acllc.html
Feb 11 '08 #7
On Feb 10, 2:30*pm, sophia.ag...@gmail.com wrote:
Dear all,

why the following program is giving o/p as

*sizeof(struct emp) = *0

#include<stdio.h>
#include<stdlib.h>

struct emp
{ };
Not allowed in C .
There may be unnamed padding within a structure object, but not at its
beginning.
There may be unnamed padding at the end of a structure or union.
Refer -http://c0x.coding-guidelines.com/6.7.2.1.html
>
int main(void)
{
* printf("\n sizeof(struct emp) = *%d", sizeof(struct emp));

* puts ("");
* return (EXIT_SUCCESS);

}

i have seen this same code giving o/p as 1 in other compilers
Feb 17 '08 #8

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

Similar topics

19
by: Martin Pohlack | last post by:
Hi, I have a funtion which shall compute the amount for a later malloc. In this function I need the sizes of some struct members without having an instance or pointer of the struct. As...
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
7
by: Zero | last post by:
If we have a structure like: struct something{ int *a; int b; }; We allocate mempry for a using malloc or calloc. The question is when we want to know the size of the structure,...
15
by: ak | last post by:
struct xy{ int x; int y; } _xy; size_of_xy(struct xy * a) { int len = sizeof a; printf( "\sizeof xy: %i\n", len ); }
10
by: Sean | last post by:
I have a struct that I wrote to test a protocol. The idea I had was to just declare the elements of the struct in the order in which they are sent and received as defined by the protocol. ...
10
by: Mark A. Odell | last post by:
Is there a way to obtain the size of a struct element based only upon its offset within the struct? I seem unable to figure out a way to do this (short of comparing every element's offset with...
8
by: Tony Houghton | last post by:
I'm writing a python program which reads input device events so it needs to know sizeof(struct timeval). By using the struct module I should be able to work out sizeof(long) from python, but I...
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
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
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...

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.