Take the 2-minute tour ×

This is a theoretical question.

I wonder how certain constructions in C are performed internally without references. For example:

struct Foo { int a; };
int main() {
  struct Foo foo;
  foo.a = 10;
  return 0;

What is the type of foo.a? It's definitely not a pointer, because we assign 10 as a value, not address. But it's not a value type as well, because it changes data of foo. In C++ I would just say that it's a reference, but in C?

share|improve this question
add comment

4 Answers

up vote 7 down vote accepted

It is an int lvalue. Same as if you had int bar, and bar = 10 changes the data of bar. Same applies to any element within an array. Basically anything you can take the address of is an lvalue.

Lvalue status is independent of type.

share|improve this answer
add comment

The only reason a reference is "needed" here in C++ is operator overloading. In C, the = sign is not a function call that needs a reference argument. It's simply an operator. It can't be redefined and made to do nonsensical stuff, so a normal lvalue is perfectly fine for it.

Note that there's no fundamental reason C++ needed references for this either. It could just as well have specified the overloaded operator= etc. functions to receive pointers rather than references.

share|improve this answer
add comment

The type of foo.a is int. Think about it this way: writing foo.a = 5 is the same as writing *((int*)(&foo + offset_of_a)) = 5 - it's just writing data to a certain memory location which is to be interpreted a a value of type int.

share|improve this answer
add comment

Structs in c are really just big primitives. if you look at the assembler generated for your foo assignment, it just acts like an int. Here's an example:

I have my struct definition:

 typedef struct Foo Foo;
 struct Foo {
     int a, b;

And I declare a Foo:

 Foo foo = {0,1};

The stack (a little machine-specific stuff is being glossed over, but in general) looks like this (assuming 4-bit ints and one-bit bytes)

%esp  %ebp[-8]  %ebp[-4]  %ebp
[....   0000      0001    ....]

you find that the instruction to assign foo.b = 1 is

mov $1, -4(%ebp)

Which is the same instruction that would assign an int on the stack a value.

share|improve this answer
I've never seen a 'one-bit byte' before. –  Jonathan Leffler Aug 17 '11 at 6:59
It'll catch on... –  Dave Aug 17 '11 at 7:00
add comment

Your Answer


