This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 5 years ago.
I came across this code I have no clue what it is doing
// ptr is a pointer to an int
if (2[ptr] == 5){
// do something
}
I did some of my tests but no matter what value I pass into ptr, 0, negatives, positives, 2[ptr] always evaluates to 0. I am surprised it actually compiles. Can someone explain what this expression is ?
2[ptr] is the same as ptr[2] (more or less). Somebody is playing a little joke on you.
As for why it always evaluates to 0, that depends on the rest of the program, which we cannot see.
Related
This question already has answers here:
With arrays, why is it the case that a[5] == 5[a]?
(20 answers)
Closed 2 years ago.
I've seen an example showing as
int n = sizeof(0)["abcdefghij"];
cout<<n;
What does that thing in square brackets mean? I've read somewhere that (0)["abc"] is equivalent to ("abc")[0]. Meaning the above expression is simply
n = sizeof("abcdefghij")[0];
i.e. the first element.
First, sizeof is not a function but an operator
sizeof(0)["abcdefghij"] can be parsed as either
sizeof( (0)["abcdefghij"] ), or
( sizeof(0) )["abcdefghij"]
Since sizeof has lower precedence than [], the former will take place
(0)["abcdefghij"] is equivalent to "abcdefghij"[0] which is just 'a', so the whole thing is the same as sizeof('a') which is 1 in C++
Demo on GodBolt, ideone
If you replace sizeof(0) with sizeof(int) then the same thing happens, but now (int)["abcdefghij"] is invalid so it should result in a compilation fail. Most compilers report an error as expected that except ICC so it looks like that's an ICC bug which chooses (sizeof(int))["abcdefghij"] over sizeof((int)["abcdefghij"]) just because the latter is invalid
Related: Why does sizeof(my_arr)[0] compile and equal sizeof(my_arr[0])?
This question already has answers here:
How does C++ handle &&? (Short-circuit evaluation) [duplicate]
(7 answers)
Is short-circuiting logical operators mandated? And evaluation order?
(7 answers)
Closed 2 years ago.
In the below example will the comparison between a and c be executed or will it be skipped? I would assume the answer is no because the value of valid has already been determined since if any comparison is false in this expression the resulting value of valid is false, but I don't actually know if that's true.
int i = 10, b = 20, c, 30;
bool valid = (i > b && a < c);
Thanks for taking the time to answer my question!
This question already has answers here:
Undefined behavior and sequence points
(5 answers)
What's the reason for letting the semantics of a=a++ be undefined?
(8 answers)
Closed 5 years ago.
I have been running this code at different compilers. At Microsft VS. it prints 1, but at gcc, it prints 0. What is the result according to the standard c++. I don't if there is standardization for this piece of code as well.
int a=0;
a=a++;
cout << a<< endl;
a=a++ is undefined behavior. Not only is there no standard definition for what will happen, it isn't even guaranteed to always do the same thing between different runs.
It could print 0 now, 1 the next time, and crash your program on the third attempt.
This question already has answers here:
Pointer Arithmetic [closed]
(7 answers)
Closed 7 years ago.
#include<stdio.h>
#define MYSIZEOF(X) ((X*)0 +1)
int main()
{
printf("%ld", MYSIZEOF(int));
return 0;
}
Can any one please explain how it works ?
thanks in advance
The idea here is simple: arithmetic on a pointer to a type T is performed in multiples of the sizeof(T), so ((X*)0 +1) will - hopefully - be a pointer to an address sizeof(X) bytes into memory.
Unfortunately, the behaviour's undefined as (X*)0 creates a NULL pointer, and the compiler may substitute some non-zero value used as that sentinel on the system it's compiling for. Further, the code assumes %ld is the right format for a pointer, and it may not be. %p would be an improvement if the printf implementation supports it.
Of course, it's silly not to use the sizeof operator directly....
This question already has answers here:
Pointer Arithmetic In C
(2 answers)
Closed 8 years ago.
A project I did last year involved pointer arithmetic. When I did that, I was able to treat pointers like memory addresses and add or subtract from them as I wanted. For example, if int* p == array[0], then you'd know that p + sizeof(int) would find array[1]. That doesn't seem to be the case anymore, as I have a relatively well-known interview question in front of me in which I have to debug the following code:
void
ReverseTheArray( const short *pArrayStart, int nArrayByteLength )
{
short const *pArrayEnd = (pArrayStart + nArrayByteLength);
while(pArrayStart != pArrayEnd)
{
short tmp = *pArrayStart;
*pArrayStart = *pArrayEnd;
*pArrayEnd = tmp;
pArrayStart++;
pArrayEnd--;
}
}
Note the last two lines - I would have bet that these were wrong because simply adding 1 to the pointer wouldn't work, you would need to add sizeof(short). But from testing the code it would seem I'm wrong - "pArrayStart++" adds sizeof(short) to the pointer, not 1.
When did this change? Can anyone give me some insight into what I'm wrong about so that I can not look stupid if I'm asked about this?
Edit: Okay - seems like it's always been that way. My bad.
The type of pointer is merely for this purpose. "Pointer to int" means adding one would skip 4 bytes (if int is 4 bytes on that machine.
Update: (Of course, in addition to explaining what type of data it is pointing to).