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

Usage of Double Pointers

P: n/a
Hi,

Can I get answers for the following:

1. Usage of Double Pointers during allocation memory.Heard that if
memory is to be allocated by someone who is working on another
module,then we should use only Double Pointers and not just ptrs.Why?

2. Usage in 2D arrays

Dec 14 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
>>Can I get answers for the following:
First think what is pointer? what is double pointer? what happens in
function call? and what is array?
Pointer is a simple variable which hold address of a memory area. For
example, on 32 bit system, char *p; means p is a pointer to character.
size of p is 4 byte (32 bit address) and it holds address (32 bit
address) of a memory area where a character may be stored.
now think what is char **p; still p has a size of 4 byte on 32 bit
system and it will store address of a memory location. But in this
case, this memory location doesn't contain data, but it contains again
a memory address, and this memory address will tell the location of
actual data (which is a character here).
Again just writing char *p; just puts a 4 byte space (on stack) for p
to store address. It doesn't allocate any memory for data, so therefore
we can allocate new memory for data via new operator(char *p; p = new
char;) or we can assign it an old memory (char c;char *p; p=&c;)
And just writing char **p; means
1. p stores address of *p. and *p stores address of **p;
2. we have 4 byte space for p to store address on stack and nothing
more. So we have only space for storing address of *p. still we have to
find memory space for *p(address of data) and **p (data).
char **p;
p = new char*; ///allocate memory of tyep char* for *p and store
address in p
*p = new char; ///since we have already space for *p, we can store here
address of data
**p='a'; ///now we can write this

this was what the pointer to pointer is.
Now we see what the function call actually is.
suppose we have a function func(int x,char y) , when we call it like
int c = 23; char a='a';
func(c,a);
Here the values of c and a are copied and 23 and 'a' are pushed on
stack and function takes its argument from stack. any changes to
arguments made within the function is local to function body and
doesn't reflect to outside of function.

now suppose we have a function func1(int *x1, char *y1), and we call
function like this
int c =23;char a='a';
func1(&c,&a);
Now here we are passing the address of variables instead of values as
arguments.
The address of c & a will be copied and pushed on stack. and function
will take these from stack.In same manner as in function func (where
parameters are passed by value and any changes made to x,y will be
local to function,), here addresses are passed and any change made to
these addresses will be local to function. (In other worlds, in this
case x and y are passed by value and any changes made to contents of x
& y wll not reflect outside). So if your function does something like
int global_int=1;
void func1(int *x1,char *y1)
{
*x1 = 22; ///change value of c of caller function
x1 = &global_int; /// locally change the contents of x1, i.e. change
the address
*x1 = 2; ///it will change the value of global_int
y1 = new char; /// change the contents of y1 (i.e. address of
data) locally,now y1 will point to new memory location but it will not
change the address of varible a of caller function

*y1 = 'c'; ///it will change the value of memory allocated just above
}
In first line ,(write *x1 =22; within the function), and since x
contains the address of c, *x1 will point to variable c of caller and
changes will made to memory location of c. thus value of c will
change.
Again take this function
void func2(int **x2,char **y2)
here content of x2 and y2 (i.e addresses to *x2 & *y2) will be passed
by value. So any changes made in contents of x2 and y2 (i.e. addresses
hold by them) will not reflect outside. But if you will change the
contents of *x2 & *y2 or change the contents of (**x2 & **y2) , it
will reflect outside (until you don't change contents of x2 & y2).
void main()
{
int i = 23; char c ='a';
int *pI = &i;
char *pc = &c;
func2(&pI,&pc);
cout<<*pI; //will print 77
}
void func2(int **ppi,char **ppc)
{
//ppi will contain the address of pI and pI contians the address
of i.
///So *ppi refers to pI and **ppi points i
*ppi = new int; //*ppi means go to pI and change the contents to
address of new memory location
*ppi = 77; ///copy 77 to memory allocated just above
//ppi =new int*; ///Don't do it. it means - change value of the
local copy of ppi.
}

Dec 14 '05 #2

P: n/a
void func2(int **ppi,char **ppc)
{
//ppi will contain the address of pI and pI contians the address
of i.
///So *ppi is pI and **ppi is i

*ppi = new int; //*ppi means go to pI and change the contents to
address of new memory location
*ppi = 77; ///copy 77 to memory allocated just above
//ppi =new int*; ///Don't do it. it means - change value of the
local copy of ppi.
}

Dec 14 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.