Hi guyz,
could you please tell me what's the use of pointer to a pointer? specially in function parameters.
Regards,
7 2694
A pointer to a pointer is sometimes used to make dynamically-allocated 2D arrays. For instance, - int **array;
-
array = new int*[10];
-
for (int i = 0; i < 10; i++) {
-
array[i] = new int[10];
-
}
This code will make a 10x10 array of integers.
Functions that accept double pointers (what I call them - not an official term) can use them as 2d arrays in the above manner.
I'm not sure if there's any other widespread usage of double pointers...
A pointer to a pointer is sometimes used to make dynamically-allocated 2D arrays. For instance, - int **array;
-
array = new int*[10];
-
for (int i = 0; i < 10; i++) {
-
array[i] = new int[10];
-
}
This code will make a 10x10 array of integers.
Functions that accept double pointers (what I call them - not an official term) can use them as 2d arrays in the above manner.
I'm not sure if there's any other widespread usage of double pointers...
tnx for the reply
Banfa 9,065
Expert Mod 8TB
I'm not sure if there's any other widespread usage of double pointers...
Loads :D
You can use a pointer to pointer in a function call if you want to return a pointer via a parameter, i.e. -
char *pp;
-
-
Function(&pp);
-
-
-
void Function(char **ppp)
-
{
-
(*ppp) = malloc(20);
-
-
strcpy(*ppp, "Hellow World");
-
}
-
You need a pointer to a pointer to pass back the value of a pointer (in the same way you need a pointer to an int to pass back the value of an int), if you just had a pointer then the value of pp would not be changed.
Pointers to pointers can be useful in linked list manipulation, sometimes it is handy to have a pointer to your next pointer.
Just allocating memory is a rather trivial and boring use of a pointer to pointer that doesn't really exploit it's full potential.
Furthermore (and forgive me if i'm repeating what you saud Banfa) a double pointer allows the called function to worry about memory......
Consider two functions: -
-
int myMethod(char *inString, int size);
-
int myOtherMethod(char **inString, int *size);
-
myMethod can manipulate the data starting at pointer inString, but has to assume that memory has been allocated (generally you would expect that this is why size has been passed also). -
int myMethod(char *inString, int size)
-
{
-
char* tempString = inString;
-
for(int i=0; i<size; i++)
-
{
-
*tempString = 'A';
-
tempString ++;
-
}
-
return SUCCESS; //where SUCCESS is defined somehwer.....
-
}
-
myOtherMethod can allocate memory to a pointer of whatever size it wants and the pass the buffer back (and you'll often find that size is a pointer also as above, so that we can update that. The idea is that we can either default to a particular value if size is not pased in OR the caller passes the size of data he expects, and we return how much he gets (fairly simple check to see that data is reasonable) -
-
int myOtherMethod(char **inString, int *size)
-
{
-
char *tempBuffer = NULL;
-
if(*size > 0) //Notice value AT size
-
{
-
tempBuffer = (char *)malloc(*size);
-
}
-
else
-
{
-
tempBuffer = (char *)malloc(DEFAULT_SIZE);
-
*size = DEFAULT_SIZE;
-
}
-
//Do whatever we need to do with data
-
-
//point callers pointer to our buffer
-
*inString = tempBuffer;
-
}
-
NOTICE: we say *inString = tempBuffer ("the value AT inString = tempBuffer", since tempBuffer is a ptr and inString is a ptr to a ptr we update the (ptr) value AT ptr, ptr )
}
However, be warned that the procedure that DeMan is talking about goes against good coding practices. When you write a routine it should focus on changing one thing, and returning a value. Routines that side effect multiple arguments are harder to debug and more difficult to follow when troubleshooting. Routines that operate like this are usually indicative of a poor design.
I'm not sure that's necessarily true,
As an example....
If you want to read from storage, there is little point being passed a buffer of some arbitrary length, when you don't know how long the data will be. If the methos is creating a buffer, it should also return the length of said buffer so that the caller knows how much data they're dealing with.
Some people would suggest using another method and calling that "size" to acheive this, so that you first ask how big the data will be, then allocate a buffer that size, and pass it on to the read function. This idea has merit because the person allocating the buffer will be the person freeing it, however, by returning buffer and length together, we guarantee not (necessarily) that we return the length of data that should have been read, but the length of the ACTUAL buffer we created....that is, the caller does not need to make assume that we used their buffer as they intended.
Of course alternatives such as creating a structure that stores the size and a pointer to the buffer may be a different solution.......
...Of course alternatives such as creating a structure that stores the size and a pointer to the buffer may be a different solution.......
Yes this is an important point. It highlights the difference between programming in your language of choice and programming into you language of choice.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Carsten Spieß |
last post by:
Hello all,
i have a problem with a template constructor
I reduced my code to the following (compiled with gcc 2.7.2) to show
my problem:
// a base class
class Base{};
// two derived...
|
by: Mr A |
last post by:
Hi!
I've been thinking about passing parameteras using references instead
of pointers in order to emphasize that the parameter must be an
object.
Exemple:
void func(Objec& object); //object...
|
by: Bruno van Dooren |
last post by:
Hi All,
i have some (3) different weird pointer problems that have me stumped. i
suspect that the compiler behavior is correct because gcc shows the same
results.
...
|
by: tuko |
last post by:
Hello kind people.
Can someone explain please the following code?
/* Create Storage Space For The Texture */
AUX_RGBImageRec *TextureImage; /* Line 1*/
/* Set The Pointer To NULL...
|
by: junky_fellow |
last post by:
According to Section A6.6 Pointers and Integers (k & R)
" A pointer to one type may be converted to a pointer to another type.
The resulting pointer may cause
addressing exceptions if the...
|
by: Alexei A. Frounze |
last post by:
Hi all,
I have a question regarding the gcc behavior (gcc version 3.3.4).
On the following test program it emits a warning:
#include <stdio.h>
int aInt2 = {0,1,2,4,9,16};
int aInt3 =...
|
by: aegis |
last post by:
Given the following:
int a = 10;
int *p;
void *p1;
unsigned char *p2;
p = &a;
|
by: bluejack |
last post by:
Ahoy... before I go off scouring particular platforms for specialized
answers, I thought I would see if there is a portable C answer to this
question:
I want a function pointer that, when...
|
by: fieldfallow |
last post by:
Hello all,
Before stating my question, I should mention that I'm fairly new to C.
Now, I attempted a small demo that prints out the values of C's numeric
types, both uninitialised and after...
|
by: Martin Jørgensen |
last post by:
Hi,
"C primer plus" p.382:
Suppose we have this declaration:
int (*pa);
int ar1;
int ar2;
int **p2;
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |