I'm solving C programming quiz.
The quiz problem was "what is the output of the following code snnipet?"
uint32_t v = 0xdeadbeef;
printf("%02x", (char *) v[0]);
or uint64_t?
Honestly I didn't understand the problem, so I tested on my local machine.
#include<stdio.h>
#include<stdint.h>
int main() {
uint32_t v = 0xdeadbeef;
printf("%02x", (char *) v[0]); /* (1) */
int64_t w = 0xdeadbeef;
printf("%02x", (char *) w[0]); /* (2) */
}
I'm getting compile error on (1) and (2).
Here is the error message
num1.c: In function ‘main’: error: subscripted value is neither array nor pointer nor vector
So for the question on this post, How can I test this code without compile error?
Expected output : de, ad, be, ef, or 00
v, notv[0]. That is,((char*)v)[0]. That might also give you an idea as to the problem.((char *) &v)[0]. Then it would output either de or ef depending on endianness.(char *)v[0]to((char *)&v)[0]-- or, more likely,((unsigned char *)&v)[0]v) which is neither array nor pointer nor vector. The subscript operator requires one of those things as operand.