简体   繁体   English

在同一行中进行数学运算时是否可以获取变量的地址

[英]Is it possible to get the address of a variable while doing mathematical operations with it in the same line

I was implementing a C function to do manipulations of strings.我正在实现 C function 来操作字符串。

At the end I had to return pointer to static string but before that the first element of the string had to be set to '\0'最后,我必须返回指向 static 字符串的指针,但在此之前必须将字符串的第一个元素设置为 '\0'

char *funct(...)
{
    static char string[30];
    .
    . //do some manipulations and possibly return here
    .
    string[0] = '\0';
    return string;
}

Everything works absolutely fine to this point, but then I wanted to merge the last two lines:到目前为止一切正常,但后来我想合并最后两行:

char *funct(...)
{
    static char string[30];
    .
    . //do some manipulations and possibly return here
    .
    return &(string[0] = '\0');
}

Then I got the error "lvalue required as unary '&' operand".然后我得到错误“需要左值作为一元'&'操作数”。 I presume that's so because the compiler threats everithing within the brackets as a number, not a variable and as we all know you can't have address of just a number ( "return &('\0');" is wrong).我认为是这样,因为编译器将括号内的所有内容威胁为数字,而不是变量,而且我们都知道你不能只拥有数字的地址(“return &('\0');”是错误的)。

My question is absolutely inessential but still I'm eager to know if you can trick the compiler(gcc in my case) to treat the content in the brackets as a variable and not a number我的问题绝对无关紧要,但我仍然很想知道您是否可以欺骗编译器(在我的情况下为 gcc)将括号中的内容视为变量而不是数字

Thanks in advance:)提前致谢:)

Everything works absolutely fine to this point到目前为止一切正常

Yep, code looks fine so far.是的,到目前为止,代码看起来还不错。

but then I wanted to merge the last two lines但后来我想合并最后两行

Why?为什么? That's pointless.那是没有意义的。

Then I got the error "lvalue required as unary '&' operand".然后我得到错误“需要左值作为一元'&'操作数”。 I presume that's so because the compiler threats everithing within the brackets as a number我认为是这样,因为编译器威胁括号内的所有内容作为数字

No, it is because the compiler treates everything within the brackets as the result of the = operator.不,这是因为编译器将括号内的所有内容视为=运算符的结果。 In order to take the address of something, it must be a lvalue - an addressable memory location where a variable is allocated.为了获取某物的地址,它必须是一个左值- 一个分配变量的可寻址 memory 位置。

Some operators in C return a result which is a lvalue, some don't. C 中的一些运算符返回一个左值结果,有些则不返回。 The = operator does not. =运算符没有。 From the standard:从标准:

An assignment operator stores a value in the object designated by the left operand.赋值运算符将值存储在左操作数指定的 object 中。 An assignment expression has the value of the left operand after the assignment, but is not an lvalue.赋值表达式在赋值后具有左操作数的值,但不是左值。

So you shouldn't do this.所以你不应该这样做。 Keep the code as it was and don't change anything:保持代码不变,不要更改任何内容:

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

...but still I'm eager to know if you can trick the compiler ...但我仍然很想知道你是否可以欺骗编译器

Eh, well C allows all manner of crazy stuff.嗯,C 允许各种疯狂的东西。 But that doesn't mean that you should do it.但这并不意味着你应该这样做。 Some examples of code you should not write:您不应该编写的一些代码示例:

// BAD, OBFUSCATED CODE, DON'T USE THESE

return *string = '\0', string;

return &string[*string = '\0'];

return &(0<:string:>=0)<:string:>;

If you want such a compound return statement then just use the comma operator如果您想要这样的复合返回语句,那么只需使用逗号运算符

return string[0] = '\0', &string[0];

Or (that is the same)或者(也一样)

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

Or you can enclose the expression in parentheses.或者,您可以将表达式括在括号中。

return ( string[0] = '\0', string );

If you want to merge those two lines, consider writing a function with a meaningful name, instead of obfuscating your code (or try to somehow "trick" the compiler)如果您想合并这两行,请考虑使用有意义的名称编写 function,而不是混淆您的代码(或尝试以某种方式“欺骗”编译器)

char *cleared(char *str)
{
    *str = '\0';
    return str;
}

So that you could use it like这样你就可以像使用它一样

char *funct(/*...*/)
{
    static char string[30];
    /*
        do some manipulations and possibly return here
    */
    return cleared(string);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM