473,667 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Pointer to Pointer

16 New Member
Hi guyz,
could you please tell me what's the use of pointer to a pointer? specially in function parameters.
Regards,
Feb 25 '07 #1
7 2705
Ganon11
3,652 Recognized Expert Specialist
A pointer to a pointer is sometimes used to make dynamically-allocated 2D arrays. For instance,

Expand|Select|Wrap|Line Numbers
  1. int **array;
  2. array = new int*[10];
  3. for (int i = 0; i < 10; i++) {
  4.    array[i] = new int[10];
  5. }
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...
Feb 26 '07 #2
jeff_rowa
16 New Member
A pointer to a pointer is sometimes used to make dynamically-allocated 2D arrays. For instance,

Expand|Select|Wrap|Line Numbers
  1. int **array;
  2. array = new int*[10];
  3. for (int i = 0; i < 10; i++) {
  4.    array[i] = new int[10];
  5. }
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
Feb 26 '07 #3
Banfa
9,065 Recognized Expert Moderator Expert
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.

Expand|Select|Wrap|Line Numbers
  1. char *pp;
  2.  
  3. Function(&pp);
  4.  
  5.  
  6. void Function(char **ppp)
  7. {
  8.     (*ppp) = malloc(20);
  9.  
  10.     strcpy(*ppp, "Hellow World");
  11. }
  12.  
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.
Feb 26 '07 #4
DeMan
1,806 Top Contributor
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:

Expand|Select|Wrap|Line Numbers
  1.  
  2. int myMethod(char *inString, int size);
  3. int myOtherMethod(char **inString, int *size);
  4.  
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).
Expand|Select|Wrap|Line Numbers
  1. int myMethod(char *inString, int size)
  2. {
  3.   char* tempString = inString;
  4.   for(int i=0; i<size; i++)
  5.   {
  6.     *tempString = 'A';
  7.     tempString ++;
  8.   }
  9.   return SUCCESS;  //where SUCCESS is defined somehwer.....
  10.  
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)

Expand|Select|Wrap|Line Numbers
  1.  
  2. int myOtherMethod(char **inString, int *size)
  3. {
  4.   char *tempBuffer = NULL;
  5.   if(*size > 0)  //Notice value AT size
  6.   {
  7.     tempBuffer = (char *)malloc(*size);
  8.   }
  9.   else
  10.   {
  11.     tempBuffer = (char *)malloc(DEFAULT_SIZE);
  12.     *size = DEFAULT_SIZE;
  13.   }
  14.   //Do whatever we need to do with data
  15.  
  16.   //point callers pointer to our buffer
  17.   *inString = tempBuffer;
  18. }
  19.  
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 )



}
Feb 26 '07 #5
RedSon
5,000 Recognized Expert Expert
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.
Feb 26 '07 #6
DeMan
1,806 Top Contributor
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.......
Feb 26 '07 #7
RedSon
5,000 Recognized Expert Expert
...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.
Feb 26 '07 #8

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

Similar topics

4
2134
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 classes
110
9900
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 must be an object instead of
3
2349
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. ---------------------------------------------- //example 1: typedef int t_Array; int main(int argc, char* argv)
35
2884
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 */ memset(TextureImage,0,sizeof(void *)*1); /* Line 2*/ According to my knowledge in the first line
16
2291
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 subject pointer does not refer to an object suitably aligned in storage. It is guaranteed that a pointer to an object may be converted to a pointer to an object whose type requires less or equally strict storage alignment and back again without change;...
204
12985
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 = {0,1,2,4,9};
16
2503
by: aegis | last post by:
Given the following: int a = 10; int *p; void *p1; unsigned char *p2; p = &a;
23
7801
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 called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
69
5551
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 assigning them their maximum defined values. However, the output of printf() for the long double 'ld' and the pointer of type void 'v_p', after initialisation don't seem to be right. The compiler used was gcc (mingw) with '-Wall', '-std=c99' and
8
2230
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;
0
8458
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8366
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8888
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8790
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8650
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6206
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4372
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2017
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1779
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.