Ask your own question, for FREE!
Computer Science 60 Online
mhchen:

unsigned long long int a[4][10] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,37,38,39,40}; printf("%lu",a); //5120 this is given printf("%lu",a+2); //5280 since a is 80 bytes, and 2*80 = 160, and 5120 + 160 = 5280 printf("%lu",a[2]); //also 5280 printf("%lu",*(a+2)+9); //answer is 5352 I don't understand how the answer is 5352 @photonics

photonics:

took me a while but i figured it out

photonics:

essentially the issue at play is that you're doing pointer arithmetic (something we never learned), but you have an array which is a pointer of pointers (in a sense) and you've dereferenced the pointer once

photonics:

printf("%lu",*(a+2)+9); //answer is 5352 so *(a+2) would yield address 5280, but as an unsigned long long is 8 bytes having already dereferenced your pointer you get 5280+8*9=5352

photonics:

admittedly I probably would not get this question correct on an exam, but hey, i already made it through the exams that would ask :D

mhchen:

and if I dereference that entire thing printf("*lu*, *(*a+2)+9); that would be *(5352) which is the content at memory location 5352. right?

mhchen:

Huh, it was 30 on the exam.

photonics:

doing math is hard

mhchen:

okay well yeah I get the idea at least.

photonics:

i mean you get the idea

photonics:

the issue is that on an exam you have to do it exactly and not just the idea

photonics:

but that's not my problem *shrug*

mhchen:

Have you done R-values and L-values before?

photonics:

yeah but i don't remember them

photonics:

but hey i never even learned pointer arithmetic sooo

mhchen:

okay xd. I'll keep that in mind.

mhchen:

I actually didn't know that an array in C was an array of pointers.

photonics:

it's not, but you can treat it as such

photonics:

it's really a single pointer wherein data is assumed to be stored in row major order

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Latest Questions
Aliciaa: Is anyone elses QC going wack? Mine keeps freezing and stuff
12 minutes ago 1 Reply 0 Medals
autumnp: does anyone have bandlab?
20 minutes ago 1 Reply 0 Medals
Jeromeccv: Art for @vanessad123
11 minutes ago 7 Replies 1 Medal
ohhhhhhhhhhhhhh: how do i slim my waist fast
1 hour ago 2 Replies 1 Medal
uknownprttyfacekayla: I drew this not sure what i was aiming for but i just drew what my heart guided m
2 hours ago 8 Replies 3 Medals
alphaXtiger: what is the meaning of math meaning who made it.
4 hours ago 3 Replies 3 Medals
axie: Poem I think
2 hours ago 14 Replies 4 Medals
Fz150: what is the definition of art?
2 hours ago 4 Replies 3 Medals
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!