beterman
beterman

Reputation: 99

C++ Pointer to Pointer to Function Parameter

the following C++ code does not work as I wish.

#include <string>
#include <iostream>

int Pointer_Function(char* _output);
int Pointer_to_Pointer_Function(char** text );

int main() {

    char* input = "This";
    printf("1. %s \n", input);
    Pointer_Function(input);
    printf("5. %s \n", input);

       int test;
       std::cin >> test;
}

int Pointer_Function(char* _output) {
       _output = "Datenmanipulation 1";
       printf("2. %s \n", _output);

       Pointer_to_Pointer_Function(&_output);

       printf("4. %s \n", _output);
    return 0;
}

int Pointer_to_Pointer_Function(char** text ) {

       printf("3. %s \n", *text);
       char* input = "HalliHallo";

       text = &input;
       printf("3.1. %s \n", *text);
    return 0;
}

I wish as result for printf 5. HalliHallo instead of Datenmanipulation. Because data text must be changed due to &input.

Output:

1.This
2. Datenmanipulation 1
3. Datenmanipulation 1
3.1 HalliHallo
4. Datenmanipulation 1
5. This

Expected Result:

1.This
2. Datenmanipulation 1
3. Datenmanipulation 1
3.1 HalliHallo
4. HalliHallo
5. HalliHallo

How can I give pointer to pointer to a Function as a Parameter? Why does not work my Code?

Upvotes: 1

Views: 883

Answers (3)

Pandrei
Pandrei

Reputation: 4951

The problem is in you Pointer_to_pointer_function

int Pointer_to_Pointer_Function(char** text ) {

       printf("3. %s \n", *text);
       char* input = "HalliHallo";

       text = &input;
       printf("3.1. %s \n", *text);
    return 0;
}

You pass a pointer to pointer to char as argument to the function. The variable input is pointer to char. Because of that text = &input;is not correct. You are assigning the address of a pointer to a pointer to pointer to char. The assignment should be *text = &input;

You probably notice there is no need to use pointer to pointer here; not for what you are doing. You would need it if you were passing an in/out parameter to the function.

Upvotes: 0

user2249683
user2249683

Reputation:

You can fix the last function by altering the value of the pointer the pointer char** is pointing to:

C

int Pointer_to_Pointer_Function(char** text ) {
    *text = "HalliHallo";
    return 0;
}

To fix both function you might pass by reference:

C++

int Reference_to_Pointer_Function(char*& text ) {
    text =  "HalliHallo";
    return 0;
}

In C++11 a string literal has the (decayed) type 'char const*' (the conversion to char* is deprecated

Upvotes: 0

Barmar
Barmar

Reputation: 780974

When you assign:

text = &input;

you're just changing the local variable text, you're not changing the contents of the pointer that it pointed to. You should do:

*text = input;

This will make it print:

4. HalliHallo

You can't make it print

5. HalliHallo

because Pointer_Function just takes a pointer to the string, not a pointer to the variable, so it can't change the caller's variable.

You should also change all your declarations to specify const char* and const char**, since you're assigning pointers to literal strings. Here's the fully working code:

#include <string>
#include <iostream>

int Pointer_Function(const char* _output);
int Pointer_to_Pointer_Function(const char** text );

int main() {

    const char* input = "This";
    printf("1. %s \n", input);
    Pointer_Function(input);
    printf("5. %s \n", input);

       int test;
       std::cin >> test;
}

int Pointer_Function(const char* _output) {
       _output = "Datenmanipulation 1";
       printf("2. %s \n", _output);

       Pointer_to_Pointer_Function(&_output);

       printf("4. %s \n", _output);
    return 0;
}

int Pointer_to_Pointer_Function(const char** text ) {

       printf("3. %s \n", *text);
       const char* input = "HalliHallo";

       *text = input;
       printf("3.1. %s \n", *text);
    return 0;
}

Output:

1. This 
2. Datenmanipulation 1 
3. Datenmanipulation 1 
3.1. HalliHallo 
4. HalliHallo 
5. This 

Upvotes: 8

Related Questions