不知道下面的代码能不能满足楼主的要求 struct a { int i; struct b* nb; }; struct b { int x; int y; };struct a a1; struct b b1; a1.nb = new struct b*[2]; a1.nb[1] = &b1; b1.x=900; printf("%d",a1.nb[1]->x);
L-Values and R-Values Expressions in C++ can evaluate to “l-values” or “r-values.” L-values are expressions that evaluate to a type other than void and that designate a variable.L-values appear on the left side of an assignment statement (hence the “l” in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values.The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.Some examples of correct and incorrect usages are:i = 7; // Correct. A variable name, i, is an l-value. 7 = i; // Error. A constant, 7, is an r-value. j * 4 = 7; // Error. The expression j * 4 yields an r-value. *p = i; // Correct. A dereferenced pointer is an l-value. const int ci = 7; // Declare a const variable. ci = 9; // ci is a nonmodifiable l-value, so the // assignment causes an error message to // be generated. ((i < 3) ? i : j) = 7; // Correct. Conditional operator (? :) // returns an l-value.Note The examples in this section illustrate correct and incorrect usage when operators are not overloaded. By overloading operators, you can make an expression such as j * 4 an l-value.
a1.nb=&b1b不就传地址了吗?
目的是为了
a1.nb指向一个结构体数组
如何让a1.nb[1]指向结构体b1,而不是将b1赋给a1.nb[1],也就是希望指向结构体数组中某个元素的指针指向新的结构体b1 应当如何实现,得到上面的提到900的结果
struct a
{
int i;
struct b* nb;
};
struct b
{
int x;
int y;
};struct a a1;
struct b b1;
a1.nb = new struct b*[2];
a1.nb[1] = &b1;
b1.x=900;
printf("%d",a1.nb[1]->x);
Expressions in C++ can evaluate to “l-values” or “r-values.” L-values are expressions that evaluate to a type other than void and that designate a variable.L-values appear on the left side of an assignment statement (hence the “l” in l-value). Variables that would normally be l-values can be made nonmodifiable by using the const keyword; these cannot appear on the left of an assignment statement. Reference types are always l-values.The term r-value is sometimes used to describe the value of an expression and to distinguish it from an l-value. All l-values are r-values but not all r-values are l-values.Some examples of correct and incorrect usages are:i = 7; // Correct. A variable name, i, is an l-value.
7 = i; // Error. A constant, 7, is an r-value.
j * 4 = 7; // Error. The expression j * 4 yields an r-value.
*p = i; // Correct. A dereferenced pointer is an l-value.
const int ci = 7; // Declare a const variable.
ci = 9; // ci is a nonmodifiable l-value, so the
// assignment causes an error message to
// be generated.
((i < 3) ? i : j) = 7; // Correct. Conditional operator (? :)
// returns an l-value.Note The examples in this section illustrate correct and incorrect usage when operators are not overloaded. By overloading operators, you can make an expression such as j * 4 an l-value.