sizeof()

J

Jeanne MESTRE

Hi

I am new user. I have a question about sizeof() function.

I have a structure which contains char* as a member.

when I do the sizeof() on the structure, it does not know

what my char* is pointing to. So how can it give me correct value.
 
K

Keith Thompson

Jeanne MESTRE said:
I am new user. I have a question about sizeof() function.

sizeof is an operator, not a function.
I have a structure which contains char* as a member.

when I do the sizeof() on the structure, it does not know

what my char* is pointing to. So how can it give me correct value.

Correct value of what?

sizeof, when applied to your structure type or to an expression of
that type (including an object name) correctly gives you the size
in bytes of that structure, including the size of the char* member.
Following that pointer to determine what, if anything, it points
to isn't part of sizeof's job.

I could probably make a pretty good guess what you're trying to do,
but (a) my guess is likely to be wrong, and (b) you're likely to
understand the problem better yourself if you can state it clearly.

So what exactly are you trying to do? Can you show us some code
and explain to us just what information you're looking for?

Incidentally, the comp.lang.c FAQ, <http://www.c-faq.com/>, is
an excellent resource.
 
S

Shao Miller

Jeanne said:
Hi

I am new user. I have a question about sizeof() function.

I have a structure which contains char* as a member.

when I do the sizeof() on the structure, it does not know

what my char* is pointing to. So how can it give me correct value.
Good day Jeanne,

'sizeof' is not a function in C. It is a unary operator; taking one
argument. If the operand is a type name, it must be surrounded by
parentheses. Its operand _can_ be surrounded by parentheses regardless
of whether or not the operand is a type. This can cause readers to
believe that it's a function. :)

As an operator, it enjoys specifically well-defined semantics. Unless
the operand's type is a variable length array ("VLA"), the operand is
not evaluated.

For anything other than a VLA, the compiler knows at translation-time
what the size is for a particular type of object, so in these very
common cases, a 'sizeof' expression is an integer constant value,
determinable at translation-time.

For example, your 'char *' has a known size at translation time. 'char'
also has a known size at translation-time.

Hope that helps. :)
 
E

Eric Sosman

Hi

I am new user. I have a question about sizeof() function.

First, sizeof is not a function: It is an operator, just
like + and -> and the rest. If you think about it a bit, you
will see that sizeof cannot be a function: What kind of function
could take `int' (the type name, not a value) as an argument?
I have a structure which contains char* as a member.

when I do the sizeof() on the structure, it does not know

what my char* is pointing to. So how can it give me correct value.

`sizeof theStruct' or `sizeof(struct mytype)' will give you
the number of bytes in the struct itself, including any pointers
it contains but not including whatever they point at. So, given

struct mytype { char *string; } x, y, z;
x.string = "Hi!";
y.string = "Hello, hello, hello, and welcome!"
z.string = NULL;

.... all three of `sizeof x', `sizeof y', and `sizeof z' will give
the same result: The number of bytes in one `struct mytype'. This
is true even though `strlen(x.string) != strlen(y.string)', and even
though `strlen(z.string)' cannot be computed. The number of bytes
in the struct itself is all that sizeof reports.
 
S

Seebs

I am new user. I have a question about sizeof() function.

There is no sizeof() function. There is only an operator.
I have a structure which contains char* as a member.

when I do the sizeof() on the structure, it does not know

what my char* is pointing to. So how can it give me correct value.

Easily -- because the correct value is the size of the pointer,
not the size of the thing pointed to.

-s
 
S

Shao Miller

Shao said:
... ... ...so in these very
common cases, a 'sizeof' expression is an integer constant value,
> ... ... ...
Even better than a value, in these very common cases, it's defined to be
what's called an "integer constant expression" (which has a value).

It might be that you are under the impression that 'struct's have
different sizes, depending on what their pointer members point to. But
a pointer in C is just that; a pointer. It points somewhere. That
somewhere can be somewhere _else_, _outside_ of the 'struct'. :)

Thus 'struct's with the same type have the same size, which is known at
translation-time (compile-time).

If you'd like to know the length of a string, you can use 'strlen',
which takes a pointer as an argument, looks where that pointer points
to, then counts 'char's until it sees a termination 'char', which has
the binary representation for 0 and is called the "null character".
 
K

Keith Thompson

Kenneth Brody said:
<nit>
It points to a char, and sizeof char is defined as 1.
</nit>

True, but I don't see how it's a nit.
Unless, that is, you meant "the length of a nul-terminated string of chars
to which it points". :)

The OP asked about the "correct value", but hasn't told us what
that means. We can infer that it has something to do with what the
char* member points to, but we still don't know what the OP is really
asking about. (I can think of at least four plausible meanings:
the size of the char object to which it points, the length of the
string to which it points, the length of the string plus 1 for the
terminating '\0', and the size of the array object; in all cases,
if the member is currently NULL it messes things up.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top