Question:
ANSI C pointer/array question?
kistories
2009-10-25 22:28:22 UTC
I am so utterly confused by this one aspect of pointers.

Here's my program:
• #include
• int main() {
• float *pf;
• float m[][3]={ {0.1, 0.2, 0.3},
• {0.4, 0.5, 0.6},
• {0.7, 0.8, 0.9}};
• printf("%d \n",sizeof(m));
• pf=m[1];
• printf("%f %f %f \n",*pf, *(pf+1), *(pf+2));
• printf("%f %f %f \n",*pf, *(pf++), *(pf++));
• }

So the first print line prints out 0.4,0.5,0.6, which I'd expect. After all pf goe to the base element of [1] which is 0.4, then prints the next two elements.

But what is going on in the second line? It prints 0.6, 0.5,0.4. Why? Is it reading backwards? Can someone explain this theoretically what is going on here?
Five answers:
oops
2009-10-25 22:50:59 UTC
When you pass expressions as arguments to a function, they are evaluated in reverse order, then the function is called. So, on this line



printf("%f %f %f \n",*pf, *(pf++), *(pf++));



The first thing that happens is that last pf++, and 0.4 get's passed as the last argument, then it proceeds like that until the first argument is evaluated, then the function is called.
Pat M
2009-10-25 22:47:35 UTC
The order in which function arguements are parsed are implementation dependent. I this case it is executing the printf arguments backward. You can see this behavior if you take out the last *(pf++) in the second printf. This will yield 0.5, 0.4. So the real answer is it has nothing to do with pointers and everything to do with the implementation of C you are using.
Jim
2009-10-25 23:29:31 UTC
printf uses ... to handle arguments if I am not mistaken. so it can process the arguments in any order it likes. apparently it does this right to left. If you have the source to your implementation of printf, look in it to see.

not entirely sure.

if you experiment and change the last line to

printf("%f %f %f \n",*pf, *(pf++), *(pf));

you get

0.500000 0.400000 0.500000



and if you try

float f1=*(pf); //should be 4.0

float f2=*(pf++); //should be 4.0

float f3=*(pf++); //should 5.0

pf=m[1];

printf("%f %f %f \n",*pf, *(pf++), *(pf++));

printf("%f %f %f \n",f1, f2, f3);

you get

0.600000 0.500000 0.400000

0.400000 0.400000 0.500000



I could be wrong of course. you should have paid attention in class and read your books.



so somewhere within printf it's getting mangled. I think your instructor is letting you know "don't do that with printf."



learn to play around tinker with code, or better yet, engineer it. at least write test cases. that's what programming is about.
_TRUE_TO_ME_
2009-10-25 22:53:53 UTC
You must have heard of operator precedence. (Left to Right, Right to Left).. Similarly printf is Right to Left.



Let the pointer value pf = 20

The Printf is Right to Left Associativity.



So first, the 3rd value, pf= 20, (*pf++) = 0.4 will be executed and stored. After execution pf=24



Next, the 2nd value (*pf++) = 0.5 will be executed and stored. After execution pf=28



And then the *pf = 0.6 will be executed and stored.



While printing, it prints the stored value.
2016-10-13 15:10:50 UTC
ANSI-C++ skill C++ as standardized by skill of ANSI. it is the popular, known version of C++ and it quite is possibly the only you may desire to learn first. no count in case you land up writing video games for abode windows or intense-overall performance UNIX server purposes, you have ANSI-C++ attainable. so which you will might desire to appreciate it no count what.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...