Q: what is a pointer to a pointer (**) and Call By Reference

This is a discussion on Q: what is a pointer to a pointer (**) and Call By Reference within the C++ Programming forums, part of the General Programming Boards category; :? Hello, Recently I have started studying C and C++. Right now I am stuck, trying to understand call by ...

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    4

    Question Q: what is a pointer to a pointer (**) and Call By Reference

    :?
    Hello,

    Recently I have started studying C and C++. Right now I am stuck, trying to understand call by reference methodology.

    I have a small program that glues two strings to eachother.
    I am trying to reprogram it using the call by reference mechanism.

    How do I do this??
    I have especially problems understanding "pointer to a pointer" **. Can somebody explain me what exaclt ** means, why you would use it?

    Here is the program: (sorry for the Dutch function names)

    // Headerfiles
    #include <stdio.h> // prototype i/o functies
    #include <string.h> // prototype string functies
    #include <iostream.h>

    // Prototyping
    void string_invoer(char **);
    void string_voeg_toe(char ** , char );
    void string_concatenate(char *, char *, char **);

    void main()
    {
    char *p1 = new char[1];
    char *p2 = new char[1];
    char *p3 = new char[1];

    *p1 = *p2 = *p3 = 0; // init p1, p2, p3 as empty strings

    string_invoer( &p1 );
    string_invoer( &p2 );
    string_concatenate(p1, p2, &p3);

    printf("\nString1 : %s", p1);
    printf("\nString2 : %s", p2);
    printf("\nString1 + String2 : %s", p3);

    delete[] p1;
    delete[] p2;
    delete[] p3;
    getchar();
    }


    // input of characters until newline is entered
    void string_invoer(char **p)
    {
    printf(" \nGive string : " );

    char c;
    while (c != '\n') //read till "enter"
    {
    cin.get(c); // use cin.get to read space, tabs
    if (c!='\n') //don't add "enter" to the string
    string_voeg_toe(p, c);
    }
    }


    // add character c to string p
    void string_voeg_toe(char **p, char c)
    {
    char *pt = new char[strlen(*p) + 2];

    // copy original string to new memory location
    strcpy( pt, *p);
    // add new character
    pt[ strlen(*p) ] = c;
    pt[ strlen(*p) + 1 ] = 0;

    delete[] p;

    // use p as pointer for new string
    *p = pt;
    }


    // 2 strings s1 and s2 put together
    void string_concatenate( char *s1, char *s2, char **dest)
    {
    // reserve location for string s1, s2 and the end character
    char *st = new char[strlen(s1) + strlen(s2) + 1];
    strcpy(st, s1); //copy s1 in st
    strcat(st, s2); //add s2 to st, result is st

    delete[] dest

    *dest = st;
    }

  2. #2
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Code tags pal.

    You use pointer to pointer in binary trees, that's about all I use them for.
    If you ever need a hug, just ask.

  3. #3
    Registered User
    Join Date
    Apr 2003
    Posts
    2,662
    "Right now I am stuck, trying to understand call by reference methodology."

    When you pass-by-value, a copy of the variable is made for the function, so when you change the value of the variable in the function, the changes are made to the copy not the original variable, so when the function ends, the original variable remains unchanged.

    example:
    Code:
    #include <iostream>
    using namespace std;
    
    void add2(int a)
    {
    	a = a + 2;
    }
    
    int main()
    {
    	int a = 10;
    	cout<<a<<endl;
    
    	add2(a);
    	cout<<a<<endl;
    
    	return 0;
    }
    output:
    10
    10


    When you pass-by-reference, the reference isn't copied, so when you change the value of the variable in the function, it changes the original value. Pointers are kind of a hybrid. The address in a pointer variable is copied for the function, so it's passed by value, and if you try to change the value of the variable, i.e. the address, you are only changing the copy, so when the function ends the original address stored in the pointer will be the same. However, if instead of changing the address stored in the pointer, you change the value of what the pointer points to, it permanently changes the value AT the address stored in the pointer even though the address stays the same.

    examples:

    Code:
    #include <iostream>
    using namespace std;
    
    void add2(int& r)
    {
    	r = r + 2;
    }
    
    int main()
    {
    	int a = 10;
    	cout<<a<<endl;
    
    	int& r = a;
    	add2(r);
    	cout<<a<<endl;
    
    	return 0;
    }
    output:
    10
    12
    Code:
    #include <iostream>
    using namespace std;
    
    void add2(int* p)
    {
    	*p = *p + 2;
    	
    	int number;
    	p = &number; //attempt to change the original address stored in p
    }
    
    int main()
    {
    	int a = 10;
    	cout<<a<<endl;
    
    	int* p = &a; //p, a pointer to an int, equals the address of a
    	add2(p);
    	cout<<*p<<endl;
    
    	return 0;
    }
    output:
    10
    12
    Last edited by 7stud; 05-04-2003 at 03:53 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reference to a pointer to a pointer
    By Sharke in forum C++ Programming
    Replies: 18
    Last Post: 05-26-2009, 03:47 AM
  2. convert reference to pointer?
    By Marksman in forum C++ Programming
    Replies: 4
    Last Post: 10-08-2008, 06:32 AM
  3. Following CTools
    By EstateMatt in forum C Programming
    Replies: 5
    Last Post: 06-26-2008, 11:10 AM
  4. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM
  5. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21