为什么下面的C代码是非法的?

时间:2021-01-20 05:26:53

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

考虑一个典型的环境,为什么下面的代码在C中是非法的?

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

7 个解决方案

#1


31  

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

不能给x的地址赋值因为x的地址不是lvalue

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

(lvalue是“可以被赋值的东西”,即它不能位于等号的左边)

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

现在x指向你分配的内存,你可以这样做

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

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

你可以将x的地址分配给其他的东西,使用&操作符:

int x = 1;
int* y = &x;  // y now holds the address of x
*y = 2;       // Now x = 2

#2


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.

因为x的地址不是lvalue——它不是可以修改的东西。C允许你改变地址指向的东西——它不允许你改变地址本身。

#3


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."

每个人都是正确的。代码执行时的X地址是常量。换句话说,它说"嘿,你可以改变我,编译器,存放'x'变量的地方"

you could do this

你可以这样做

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

}

#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);

#5


1  

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

&x返回指向x的指针。你不能在赋值的左边使用它,只能在右边。

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

如果你想给一个指针赋值你必须声明它为一个指针,就像int *x;

#6


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.

在您的示例中,x是一个堆栈变量。malloc提供堆内存。这在今天的编程中可能不需要考虑太多,但是如果您曾在内存非常重要的环境中工作,那么您将希望尽可能地保存堆栈。

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.

同样值得注意的是,由于某些原因,您正在分配3*sizeof(int)。即使您可以将内存分配给堆栈,在您的示例中,因为您只尝试得到1 int,您只需要1* sizeof(int),其余的就会被浪费掉。

#7


0  

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

语言就是这样设计的。要做你想做的事,使用一个指针。

#1


31  

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

不能给x的地址赋值因为x的地址不是lvalue

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

(lvalue是“可以被赋值的东西”,即它不能位于等号的左边)

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

现在x指向你分配的内存,你可以这样做

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

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

你可以将x的地址分配给其他的东西,使用&操作符:

int x = 1;
int* y = &x;  // y now holds the address of x
*y = 2;       // Now x = 2

#2


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.

因为x的地址不是lvalue——它不是可以修改的东西。C允许你改变地址指向的东西——它不允许你改变地址本身。

#3


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."

每个人都是正确的。代码执行时的X地址是常量。换句话说,它说"嘿,你可以改变我,编译器,存放'x'变量的地方"

you could do this

你可以这样做

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

}

#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);

#5


1  

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

&x返回指向x的指针。你不能在赋值的左边使用它,只能在右边。

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

如果你想给一个指针赋值你必须声明它为一个指针,就像int *x;

#6


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.

在您的示例中,x是一个堆栈变量。malloc提供堆内存。这在今天的编程中可能不需要考虑太多,但是如果您曾在内存非常重要的环境中工作,那么您将希望尽可能地保存堆栈。

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.

同样值得注意的是,由于某些原因,您正在分配3*sizeof(int)。即使您可以将内存分配给堆栈,在您的示例中,因为您只尝试得到1 int,您只需要1* sizeof(int),其余的就会被浪费掉。

#7


0  

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

语言就是这样设计的。要做你想做的事,使用一个指针。