lvalue

printing a member of a returned struct

走远了吗. 提交于 2019-11-29 06:19:44
问题 I'm having trouble printing a member of a struct that is returned from a function: #include <stdio.h> struct hex_string { char a[9]; }; struct hex_string to_hex_string_(unsigned x) { static const char hex_digits[] = "0123456789ABCDEF"; struct hex_string result; char * p = result.a; int i; for (i = 28; i >= 0; i -= 4) { *p++ = hex_digits[(x >> i) & 15]; } *p = 0; printf("%s\n", result.a); /* works */ return result; } void test_hex(void) { printf("%s\n", to_hex_string_(12345).a); /* crashes */

Is a dereferenced pointer a valid lvalue?

浪尽此生 提交于 2019-11-29 06:07:07
问题 Assuming the definition: int i = 10; int *p = &i; Why is *p a valid lvalue here: *p+=10; Shouldn't *p evaluate to the value of the int stored at &i, ie. 10, and hence generate a "Not an lvalue" error? 回答1: An lvalue is an expression that refers to a region of storage that can be manipulated. *p is such an expression that refers to a region of storage. This is different than say 10+=10; because 10 doesn't refer to a region of storage like a variable would. 回答2: I believe you are confused with

how to assign to the names() attribute of the value of a variable in R

生来就可爱ヽ(ⅴ<●) 提交于 2019-11-29 05:11:29
In R, "assign('x',v)" sets the object whose name is 'x' to v. Replace 'x' by the result of applying a text function to a variable x. Then "assign" shows its worth. Unfortunately, "assign(paste('names(','x',')',sep=''),v)" fails. So if 'x' is a variable x, I can set its value, but I can't give it names for its elements. Can one work around this? a parse-eval trick maybe? Thanks. In the form you ask question there is no need to assign names. If you x exists then you do names(x) <- v . This is right way to do this. If your variable name is unknown (i.e. dynamically created) then you could use

What is dynamic type of object

僤鯓⒐⒋嵵緔 提交于 2019-11-28 23:42:56
What i think is that dynamic type means dynamically allocated object using new . In the following case, do you say p points to dynamic type or static type of object? In standard, it doesn't say about dynamic type being dynamic object. 1.3.3 - The type of the most derived object (1.8) to which the lvalue denoted by an lvalue expression refers. [Example: if a pointer (8.3.1) p whose static type is "pointer to class B" is pointing to an object of class D, derived from B (clause 10), the dynamic type of the expression *p is "D." References (8.3.2) are treated similarly. ] Also what does it the

Passing rvalue reference to const lvalue reference paremeter

限于喜欢 提交于 2019-11-28 21:42:43
问题 I am trying to understand C++11 rvalue references and how to use them for optimal performance in my code. Let's say we have a class A that has a member pointer to a large amount of dynamically allocated data. Furthermore, a method foo(const A& a) that does something with an object of class A . I want to prevent the copy constructor of A from being called when an object of A is passed to the function foo , since in that case it will perform a deep copy of the underlying heap data. I tested

Why pre-increment operator gives rvalue in C?

删除回忆录丶 提交于 2019-11-28 21:09:56
In C++, pre-increment operator gives lvalue because incremented object itself is returned, not a copy. But in C, it gives rvalue. Why? C doesn't have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented). C99 6.5.3.1/2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation . The expression ++Eis equivalent to (E+=1). ‘‘value of an expression’’ <=> rvalue However for historical reasons I think "references not being part of C" could be a possible reason. Nawaz C99 says in the

Expression must be a modifiable L-value

不想你离开。 提交于 2019-11-28 21:01:56
问题 I have here char text[60]; Then I do in an if : if(number == 2) text = "awesome"; else text = "you fail"; and it always said expression must be a modifiable L-value. 回答1: You cannot change the value of text since it is an array, not a pointer. Either declare it as char pointer (in this case it's better to declare it as const char* ): const char *text; if(number == 2) text = "awesome"; else text = "you fail"; Or use strcpy: char text[60]; if(number == 2) strcpy(text, "awesome"); else strcpy

pointer increment and dereference (lvalue required error)

孤者浪人 提交于 2019-11-28 14:18:20
I am trying to understand how pointer incrementing and dereferencing go together, and I did this to try it out: #include <stdio.h> int main(int argc, char *argv[]) { char *words[] = {"word1","word2"}; printf("%p\n",words); printf("%s\n",*words++); printf("%p\n",words); return 0; } I expected this code to do one of these: First dereference then increase the pointer (printing word1) First dereference then increase the value (printing ord1) Dereference pointer + 1 (printing word2) But compiler won't even compile this, and gives this error: lvalue required as increment operand am I doing something

Regarding lvalue-to-rvalue conversion, when is it required?

£可爱£侵袭症+ 提交于 2019-11-28 12:18:58
I've been reading quite many on the Internet and it seems that many people mentioned the following rules (but i couldn't find it in the standard), The addition operator + (and all other binary operators) requires both operands to be rvalue, and the result is rvalue. And so on.. I checked the C++ standard, and it clearly states that (clause 3.10/2), Whenever a glvalue appears in a context where a prvalue is expected, the glvalue is converted to a prvalue (clause 5/9), Whenever a glvalue expression appears as an operand of an operator that expects a prvalue for that operand, the lvalue-to-rvalue

Is it possible something like lvalue of perl or setf of lisp in python?

主宰稳场 提交于 2019-11-28 10:50:46
问题 In lisp you can say: (setf (aref a 1) 5) In perl you can say: substr( $string, $start, $stop ) =~ s/a/b/g Is it possible something like this in python? I mean is it possible to use function result as a lvalue (as a target for assignment operation)? 回答1: No. Assigning to the result of a function call is specifically prohibited at the compiler level: >>> foo() = 3 File "<stdin>", line 1 SyntaxError: can't assign to function call There are however two special cases in the Python syntax: # Slice