pointers

C

Chathu

Help me....
I'm new to the pointers. I can't understand the meaning.
Can anybody tell me the meaning of this code fragment.

float *flp,*flq;
*flp=*flp+10;
++*flp;
(*flp)++;
flq=flp;
 
J

Jack Klein

Help me....
I'm new to the pointers. I can't understand the meaning.
Can anybody tell me the meaning of this code fragment.

float *flp,*flq;

Defined two pointers to float, named flp and flq. The rest all cause
undefined behavior, unless flp and flq are actually assigned the
addresses of float objects.

Let's assume a few more lines, to make the rest legal:

float p = 3.0;
float q;
flp = &p;
flq = &q;
*flp=*flp+10;

Adds 10.0 to the float that flp points to. With my added lines above,
p will be changed from 3.0 to 13.0.

Pre increments the float that flp points to. p will now change from
13.0 to 14.0.
(*flp)++;

Post increments the float that flp points to. p will now change from
14.0 to 15.0. The parentheses are necessary because the ++ operator
binds before the * operator. Without the parentheses, *flp++ does not
change the value of the float, it changes the value of a pointer to
point to the next float, if there is a next float.

There is no difference between pre and post increment if the value of
the expression is not used.

This changes the pointer flq to point to the same float that flp does.
So even though I added a line to make flq point to the float q, after
this it now points to the float p.
 
I

infobahn

Chathu said:
Help me....
I'm new to the pointers.

A pointer object is an object whose value can be used to locate
another object.
I can't understand the meaning.
Can anybody tell me the meaning of this code fragment.

float *flp,*flq;

flp and flq are pointer objects. Neither is given a value.
*flp=*flp+10;

From now on, your code makes no sense, because flp doesn't point
to any known float.
 
T

Taran

Recommendation: Go through the book again and again. This is amongst
the first few examples for pointers.
Help me....
I'm new to the pointers. I can't understand the meaning.
Can anybody tell me the meaning of this code fragment.

float *flp,*flq;

Delcare flp and flq as pointers to a float.
*flp=*flp+10;
ERROR: flp is used before initializing it, this leads to undefined
results.
If flp is initialized to some value like

| float temp=10.0;
| float *flp,*flq;
| flp=&temp;

flp is the address of temp and *flp is the value stored at that addess
i.e. vlaue of temp.
Therefore, *flp=*flp+10 means dereference flp (get value of temp) add
10 to it and store it at the address stored in flp, address of temp.
Since you are storing a value directly at the some address, which
happens to be that of temp, all the changes you do this way will
reflect when you access the value of temp. SO now if you see the value
of temp it'd be 20. Simple!
Dereference flp, get value 20, and increment it.
(*flp)++;

Derefernce flp and increment it.

copy value in flp to flq. so that *flp is same as *flq.

This example AFAIT is for showing that post/preincrement operators'
precendece is higher than that of dereferncing operator and that post
increment/decrement happens before dereferncing. Pre is anywayz
obvious.

| float *flp,*flq,temp=10.0;
| flp=&temp;
| *flp=*flp+10;
| ++*flp;
| (*flp)++; /* different from *flp++, try this and see for yourself*/
| flq=flp;
| printf("%f\n%f",*flp,*flq);

You can also print the values of flp and flq to verify that they are
same.

flp temp 0x0008 (Address)
________ address of temp ________
|0x0008 | ---------------->| 10 |
-------- *flp --------

Hope this poor piece of art makes some sense when I post it!

HTH

Regards,
Taran
 
C

Chathu

Thank you very much for healping me to understand the pointers.
Your answer is very good and it was very helpfull.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,159
Messages
2,570,879
Members
47,417
Latest member
DarrenGaun

Latest Threads

Top