空值??怎么会?? [英] NULL?? How come??

查看:66
本文介绍了空值??怎么会??的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,


我有一个以下简单的代码,花了我几个小时

来尝试解决问题,但仍然无法找到什么是

错误。


无效主要(无效)

{

char (*字符串)[20]; .......................(1)


string = malloc(10 * sizeof * string); .....(2)

string [0] = NULL; .........................(3)

}


基本上,我将内存分配给20个字符串的字符串数组

max,并将数组的元素0设置为NULL值。

行(1)到( 2)工作正常,但第(3)行不起作用。我希望将元素0的字符串设置为
为NULL值,但不断出现错误

说:


左操作数必须是l值


请帮助我,我不明白什么是错的。


谢谢大家提前,

mike79

Hi all,

I have a the following simple piece of code which has taken me hours
to try and sort out the problem, but still unable to find what is
wrong.

void main( void )
{
char (*string)[20]; .......................(1)

string = malloc(10 * sizeof *string); .....(2)
string[0] = NULL; .........................(3)
}

Basically, I am allocating memory to an array of strings of 20 chars
max, and setting element 0 of the array to be a NULL value.
Lines (1) to (2) work OK, but line (3) does not work. I wish to set
the string of element 0 to be a NULL value, but keeps getting an error
saying:

"left operand must be l-value"

Please help me, I dont understand what''s wrong.

Thank you all in advance,
mike79

推荐答案

2003年11月3日03:43:45 -0800, mi **** @ iprimus.com.au (mike79)写道:
On 3 Nov 2003 03:43:45 -0800, mi****@iprimus.com.au (mike79) wrote:
大家好,

我有一个以下简单的代码,花了我几个小时来试图解决问题,但仍然无法找到错误的内容。

void main(无效)
{
char(* string)[20]; .......................(1)

string = malloc(10 * sizeof * string); .....(2)


/ *将数组的第一个元素设置为空字符串* /

if(string!= NULL) {

string [0] [0] =''\ 0'';

}}

基本上,我正在分配内存最多20个字符串的数组,并将数组的元素0设置为NULL值。
行(1)到(2)工作正常,但第(3)行不起作用。我希望将元素0的字符串设置为NULL值,但不断出现错误
说:

左操作数必须是l-value
Hi all,

I have a the following simple piece of code which has taken me hours
to try and sort out the problem, but still unable to find what is
wrong.

void main( void )
{
char (*string)[20]; .......................(1)

string = malloc(10 * sizeof *string); .....(2)
/* set the first element of the array to an empty string */
if (string != NULL) {
string[0][0] = ''\0'';
}}
Basically, I am allocating memory to an array of strings of 20 chars
max, and setting element 0 of the array to be a NULL value.
Lines (1) to (2) work OK, but line (3) does not work. I wish to set
the string of element 0 to be a NULL value, but keeps getting an error
saying:

"left operand must be l-value"




数组的每个元素都有类型(char [20]),20个字符的数组。你不能把b $ b分配给数组!请参阅上面代码的更正



Each element of the array has type (char [20]), array of 20 chars. You can''t
assign to arrays! See the corrections to your code above


>我有一个以下简单的代码,花了我几个小时
> I have a the following simple piece of code which has taken me hours
来尝试解决问题,但仍然无法找到错误的原因。 void main(void)
{char / * string] [20]; .......................(1)

string = malloc(10 * sizeof * string); .....(2)
string [0] = NULL; .........................(3)
}
to try and sort out the problem, but still unable to find what is
wrong.

void main( void )
{
char (*string)[20]; .......................(1)

string = malloc(10 * sizeof *string); .....(2)
string[0] = NULL; .........................(3)
}




试试

(* string)[0] = NULL;


-

runefv



Try
(*string)[0] = NULL;

--
runefv

< br>

2003年11月3日星期一13:03:21 +0100,Rune Flaten V?rnes

< re *********** *******@remove.kongsberg.remove.co m>写道:
On Mon, 3 Nov 2003 13:03:21 +0100, "Rune Flaten V?rnes"
<re******************@remove.kongsberg.remove.co m> wrote:
我有一个以下简单的代码片段,花了我几个小时
来尝试解决问题,但仍然无法找到错误的原因。
void main(void)
{char / * string] [20]; .......................(1)

string = malloc(10 * sizeof * string); .....(2)
string [0] = NULL; .........................(3)
}
I have a the following simple piece of code which has taken me hours
to try and sort out the problem, but still unable to find what is
wrong.

void main( void )
{
char (*string)[20]; .......................(1)

string = malloc(10 * sizeof *string); .....(2)
string[0] = NULL; .........................(3)
}



尝试
(* string)[0] = NULL;



Try
(*string)[0] = NULL;




让我们看看。 * string的类型为(char [20])。 **字符串(与

(* string)[0]和* string [0](在本例中为))的类型(char)相同。你为什么要给
给char分配NULL?


(作为旁注,这里使用NULL超过0是有利的,因为编译器

将捕获不正确的赋值(假设NULL不会扩展到0)

并且可能是你的推理中的错误。



Let''s see. *string is of type (char [20]). **string (which is the same thing as
(*string)[0] and even *string[0] (in this case)) is of type (char). Why are you
assigning NULL to a char?

(as a side note, using NULL over 0 here is advantageous because the compiler
will catch the incorrect assignment (assuming NULL does not expand to just 0)
and possibly a bug in your reasoning).


这篇关于空值??怎么会??的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆