Solved

difference in a pointer and a pointer to a pointer

Posted on 2001-07-11
1
246 Views
Last Modified: 2010-04-15
can someone explain to me what the difference is in a pointer and a pointer to a pointer? also include an example please.
0
Comment
Question by:Kitty__Kong
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
ID: 6275191
A pointer is a variable which contains the address of some other variable. The other variable is of some "final"/"usable" type. Consider the following:


int i,j,*pi;

i=1;
pi=&i;
j=2;
j=*pi;


Here i is an integer, as is j, and pi is a pointer to an integer. In the first line, i is assigned 1, pi is then assigned the address of i, j is assigned 2, then j is assigned the value of what pi points to (which is i), so j will become 1.

On the other hand the following:

int i,j,*pi,**ppi;

i=1;
pi=&i;
ppi=π
j=2;
j=**ppi;

i is an integer and is assigned 1, pi is a pointer to an integer and is assigned the address of i, ppi is a pointer to a pointer to an integer, which is assigned the address of pi, j is assigned 2. Now j is assigned **ppi. This will evaluate as follows: first ppi is dereferenced. Since ppi contains the address of pi, derefencing it will find pi, which is the address of i. Now the second "*" will cause that to be dereferenced which means that it will use the contents of pi as an address (as it should) and, since it is the address of i, j will be assigned 1.

This does not appear very useful, I realize, in the above example. But there are a number of uses for such constructs in C. For instance, just like you pass the address of a variable into a function, so that the variable can be changed, you need to pass the address of a pointer into a function, if you want the pointer to be changed.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
undefined reference to `bswap_128' 9 190
TCP/IP Socket - connection close results in data lost 14 153
valid enum? 6 75
A  &&  B  || (C||D) how it works in c 7 150
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question