S.i considered a variable?

C

cbowler

struct S {
int i;
};

int main() {
S myS;
myS.i; // is this a variable according to spec?
return 0;
}
 
E

E. Robert Tisdale

cat main.cc
struct S {
int i; // public data member
};

#include<iostream>

int main(int argc, char* argv[]) {
S myS;
myS.i = 33;
std::cout << myS.i << " = myS.i" << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
33 = myS.i

Yes, myS.i *is* a variable.
No, S.i is *not* a variable
because you never *declared and instance of S named S
cat main.cc
struct S {
int i; // public data member
};

#include<iostream>

int main(int argc, char* argv[]) {
S S;
S.i = 33;
std::cout << S.i << " = S.i" << std::endl;
return 0;
}
g++ -Wall -ansi -pedantic -o main main.cc
./main
33 = S.i
 
E

Eugene Alterman

Victor Bazarov said:
It is an expression of type int& (a reference to int).

Correction:
It is an lvalue of type int (see 5.2.5/4).

And in general, an object-expression evaluates to the type of the object it
designates - lvalue or rvalue.
References evaluate to lvalues (see 5/6) .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,150
Messages
2,570,853
Members
47,394
Latest member
Olekdev

Latest Threads

Top