• C

C Puzzle (for fun only!)

     register int x[2];
is a valid declaration.  Only one operation on x is
legal, but what is it?

LVL 2
JYoungmanAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Answers2000Connect With a Mentor Commented:
Lock for PAQ:
sizeof is the only operation that works of the whole of x

e.g.
int y = sizeof(x) ;

0
 
Answers2000Commented:
Not sure the question is very clear - you mean the whole of x

Problem is you can't do address of - &

sizeof ??
0
 
ecwCommented:
sizeof(x)
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
JYoungmanAuthor Commented:
Yep.  Maybe that one was too easy :-)

0
 
alexoCommented:
Keep em comming...
0
 
newexpertCommented:
But remeber register is only a request.  So if somehow computer decide not to grand what's requested then ...
0
 
newexpertCommented:
The following 4 operations are also valid: x[0], x[1], 1[x], 0[x]
although you can argue they are too similar to be 4.
0
 
newexpertCommented:
yuk, they still made implicit reference to the address.
0
 
paynCommented:
Isn't *x valid?
0
 
newexpertCommented:
Nope.  Remember you can't directly or indirectly refer to x's address, so how can you dereference x's address?

The only other pseudo-operation is #x, which gets interpreted as "x" by compiler.
0
 
alexoCommented:
#x is textual substitution done py the preprocessor befor the compiler gets to look at the code.  It has no relation to the x variable.
0
 
JYoungmanAuthor Commented:
Anybody want to answer this so we can convert it to a PAQ?

0
 
Answers2000Commented:
"But remeber register is only a request.  So if somehow computer decide not to grand [grant ?] what's requested then ..."
It can but the syntax rules for register variables still apply AFAIK
0
 
alexoCommented:
>> It can but the syntax rules for register variables still apply AFAIK
Correct.  Same as the C++ "inline" request.
0
 
pagladasuCommented:
sizeof is OK.
What if you are using a C++ compiler? Won't the address-of(&) work?
0
 
Answers2000Commented:
pagladasu - not on a register variable
0
 
pagladasuCommented:
Answers2000-The address-of operator works on register variable in C++ - try it out with the VC++ compiler yourself and see. Also please check out the latest book on the C++ programming language by Bjarne Stroustrup.
0
 
alexoCommented:
Hmmm...  Just checked the ARM.  He's right, it is permissible in C++.
Live and learn...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.