sizeof

we know that sizeof never returns zero when used with structure

then why in this case it is returning zero

struct foo
{
char c[];
};

void main()
{
struct foo f;
cout<<sizeof(f);
}

i am working on solaris 5.8
isn't the above function should return the size of empty structure

How is your program even compiling? I tried to complie the following program on HP-UX 11.11 and SunOS 5.8:

#include<stdio.h>
#include<unistd.h>

struct s {
        char sarr[];
};

int main() {
        int a;
        char c;
        struct s b;

        fprintf(stdout,"size of int: %d\n",sizeof(a));
        fprintf(stdout,"size of char: %d\n",sizeof(c));
        fprintf(stdout,"size of char arr: %d\n",sizeof(b));
}

And I am getting the following errors:
On HP:
cc: "t3.c", line 5: error 1578: Size of struct or union member is unknown.
cc: "t3.c", line 4: error 1613: Zero-sized struct.
cc: "t3.c", line 15: warning 504: The sizeof operator applied to a zero-sized object.
Note the errors in lines 4 and 5 above.

On Sun:
t3.c:5: error: flexible array member in otherwise empty struct

but i am getting the result as zero

my program is like this

struct foo
{
char c[];

};

void main()
{
struct foo s;
cout<<sizeof(s);
}

when i compile this and run on solaris 5.8 it gives the output as 0
i dont know why

there is no error in my compilation

I think this has to do with the fact that you are using C++. Cant test that now. Sorry.

All C and C++ compilers reject this code. Even g++ would not compile it until I added:
#include <iostream>
at that point I had to run it with:
./bogus ; sleep 10
to see the output. To fix that, I made this change:
cout << sizeof(s) << "\n" ;
Now that I can easily view the output, yes it is zero. I am a liitle surprised that it compiles at all. Using:
g++ -ansi -pedantic bogus.C -o bogus
I get a warning:
bogus.C:4 warning: ANSI C++ forbids zero-size array `c'

How many characters did you expect c[] to be sized for? 5? 17? 80? Is zero really that unexpected? I really cannot imagine what result you were hoping for... :confused:

man u havent under stand my problem

as you said that it return zero
the same result i too am getting

but i have read that sizeof can never ever return zero even if the class or structure is empty

if you remove the char from the structure the value of sizeof is 1
but when include the char[] then its value is zero
why?

thats was my question

My guess is that sizeof cannot work out the size of the struture and is passing back 0, representing a null or undetermined value or an error condition.

What does your documentation for sizeof say about error conditions/values from this function?

c[] is not legal. Reasonable compilers refuse to produce an executable from your source code. That is the behavior I would prefer to see and I think it's unfortunate that g++ compiles it. But the standard does not require g++ to reject illegal code. So g++ is free to do anything it wants with your code. The resulting program can output 1 or 0. Or 8,732. Or it could whistle dixie. Or it could dump core. No behavior is specified for a non-c++ program by the c++ standard.

As for what you call "empty" structures, "Objects with an incomplete structure or union type may be mentioned in contexts where their size is not needed". And the sizeof operator needs the size. That too is illegal. gcc allows sizeof to operate on a incomplete structure and for some odd reason decided it should return 1.

Microsoft allows a single unsized array as the last member of a structure. gcc's behavior with unsized arrays is a superset of Microsoft's extention. The allows gcc to compile some Microsoft code. Maybe that is why gcc behaves as it does with unsized arrays. Microsoft says sizeof return the size of the structure minus the size of the array. The array has a non-zero size but the size is not known at compile time. Apparently, you obtain the size of the array somehow and add it in later. Miicrosoft unsized arrays in structures Note that you are abusing Microsoft's screwy extention. You are not supposed to allocate it as you have done.

Incomplete structures are in the same boat. The members are unknown, not absent. gcc is out of its mind in allowing you allocate such a thing. Each object is supposed to have a unique address, so if you allocate two of them, I guess that they consume a byte.

So nonstandard extentions to the language by two different organizations have clashed with one another. Righteous karma, if you ask me. Try writing in C or C++ for a change. Then none of this will affect you.