3

Consider a typical environment, why is the following code illegal in C?

{
int x;
&x = (int*) malloc(3*sizeof(int));
...
}
Coral Doe
  • 1,925
  • 3
  • 19
  • 36
  • Why was this tagged as "homework"? I don't see anything to indicate that it's homework. – Chris Lutz Mar 13 '09 at 01:31
  • Most of these answers are the same. Why not vote up answers you agree on instead of reanswering? – Tim Matthews Mar 13 '09 at 01:34
  • @Ctrl Alt D-1337: I respectfully disagree. Several answers are subtly different, and/or explain the problem differently. For someone not clear on the issue, the varying viewpoints may lead to a more complete understanding. – mechanical_meat Mar 13 '09 at 05:14

7 Answers7

31

You can't assign something to the address of x because he address of x is not an lvalue

(An lvalue is "something that can be assigned to", i.e. it cannot be on the Left side of an equals sign)

Try

int* x;
x = (int*) malloc(3*sizeof(int)); // TODO: Call free(x) 

Now x points to your allocated memory and you can do things like

int foo = *x;
int bar = x[0];

You can assign the address of x to something else, by using the & operator this way:

int x = 1;
int* y = &x;  // y now holds the address of x
*y = 2;       // Now x = 2
Daniel LeCheminant
  • 50,583
  • 16
  • 120
  • 115
11

Becauase the address of x is not an lvalue - it is not something that can be modified. C allows you to change things that addresses point to - it does not allow you to change the addreses themselves.

  • So addresses aren't l-values, because you can't assign to them? – KingNestor Mar 13 '09 at 00:59
  • An lvalue is defined as something you can assign to. So if you can't assign to it, then it's not an lvalue by definition. – Chris Lutz Mar 13 '09 at 01:01
  • 1
    KingNestor, it's not an lvalue because it doesn't need to be stored somewhere. lvalue means "has a location". historically, lvalue means left-value and is the left side of an assignment - but that's history. examples of lvalues that can't be assigned to are arrays and constants (e.g const int). – Johannes Schaub - litb Mar 13 '09 at 12:23
  • on the other side, you can't assign to rvalues. so neils answer is still correct i think. but sadly, there *are* people where all hope is lost, like for this guy: http://stackoverflow.com/questions/502059/passing-pointers-of-arrays-in-c/502072#502072 :) – Johannes Schaub - litb Mar 13 '09 at 12:31
3

Everyone is correct. The address of X at the time your code executes is a CONSTANT. In other words it says "Hey you can CHANGE where I, the compiler, store the 'x' variable."

you could do this

int main()
{
    int* x;
    *(&x) = malloc(3*sizeof(int));

}
Jeff G
  • 41
  • 1
  • 4
2

The address of a variable cannot be changed. Instead you most likely want something like this:

int *x = (int *)malloc(3 * sizeof(int));
...
free(x);
Ecton
  • 10,702
  • 2
  • 35
  • 44
1

&x returns pointer to x. You can't use it in the left part of an assignment, only on the right.

If you want to assign something to a pointer you have to declare it as a pointer, just like int *x;

Andrea Ambu
  • 38,188
  • 14
  • 54
  • 77
1

Not that this is entirely valuable, but since no one else brought it up (all the above answers are correct, btw, and redundant, I agree that people should vote the right answer up, rather than saying the same thing over and over).

In your example, x is a stack variable. malloc gives you heap memory. That's probably not anything you need to think about too much in today's programming, but if you ever work in an environment where memory is at a premium, you'll want to save your stack as much as possible.

It's also worth noting that for some reason you're allocating 3*sizeof(int). Even if you COULD allocate memory to the stack, in your example, since you're only trying to get 1 int you'd only need 1* sizeof(int), the rest would be wasted.

Yevgeny Simkin
  • 27,946
  • 39
  • 137
  • 236
0

That's the way the language is designed. To do what you want, use a pointer.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622