By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,471 Members | 711 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,471 IT Pros & Developers. It's quick & easy.

Pointer pointing itself giving different value

P: 8
hi frnds. while studying pointers, i came across this example from a book ...
here ...at the end z is assigned address of itself by **v,
how **z can give value 3.15 if it is addressing itself ?????? another Q is if if **z
is printed in the next line after printing value of z ....it doesn't give value 3.14....
is it something related to scope??.....confused.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <conio.h>
  3. float a=3.15;
  4. float **z;
  5. float **y;
  6. float ***x;
  7. float ****v;
  8. float ****w;
  9. float **fun1(float*);
  10. float ****fun2(float***);
  11. void main()
  12. {
  13. z=fun1(&a);
  14. printf("%u , %f",z,**z);
  15.   getch();
  16. }
  17. float **fun1(float*z)
  18. {
  19.  y=&z;
  20.  v=fun2(&y);
  21.  return (**v);
  22. }
  23. float ****fun2(float***x)
  24. {
  25.  w=&x;
  26.  return(w);
  27. }
Jun 16 '07 #1
Share this Question
Share on Google+
4 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
Glad you are confused. It turns out that once you go past pointer-to-pointer
Expand|Select|Wrap|Line Numbers
  1. char** ptr;
  2.  
The mind breaks down and visualization of what happens is not possible.

If you use anything beyond pointer-to-pointer in code on a real job, you will be taken to a back room and chastized by the application of the coding standards manual to your hinder parts.

The author of your book is merely having fun with you.
Jun 17 '07 #2

P: 8
Glad you are confused. It turns out that once you go past pointer-to-pointer
Expand|Select|Wrap|Line Numbers
  1. char** ptr;
  2.  
The mind breaks down and visualization of what happens is not possible.

If you use anything beyond pointer-to-pointer in code on a real job, you will be taken to a back room and chastized by the application of the coding standards manual to your hinder parts.

The author of your book is merely having fun with you.
Thank you Weaknessforcats for the response,but I want to know does pointers behaves specifically in these kind of examples or behaves randomly that we can not imagine
.( I ran the program "stepwise " , "watching " the addresses
it is giving and it is true that I was not able to imagine how address is changing .
I know this would be ridiculous to apply to any real-job)
But does it behave specifically? (any specific pattern of change going inside memory?)
I will be vary thankful to you for your reply.....
Jun 18 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
There are no bizarre goings-on.

Expand|Select|Wrap|Line Numbers
  1. char* adr   is the address of a char
  2.        *adr    is the variable
  3. char** adr1 is the address of a char*
  4.         *adr1 is the char*
  5.        **adr1 is the variable
  6. char*** adr2  is the addres of a char**
  7.        *adr2 is the addrss of a char*
  8.        **adr2 is the address of a char
  9.        ***adr2 is the char
  10.  
If you have five layers of indirection:
Expand|Select|Wrap|Line Numbers
  1. char***** adr;
  2.  
Then you need five dereferences to arrive at the variable:
Expand|Select|Wrap|Line Numbers
  1. printf("%c", *****adr);
  2.  
Jun 18 '07 #4

P: 8
There are no bizarre goings-on.

Expand|Select|Wrap|Line Numbers
  1. char* adr   is the address of a char
  2.        *adr    is the variable
  3. char** adr1 is the address of a char*
  4.         *adr1 is the char*
  5.        **adr1 is the variable
  6. char*** adr2  is the addres of a char**
  7.        *adr2 is the addrss of a char*
  8.        **adr2 is the address of a char
  9.        ***adr2 is the char
  10.  
Thank you for response .. I just removed one block about pointers from my brain
Jun 18 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.