1

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?

3 Answers 3

8

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 
Sign up to request clarification or add additional context in comments.

6 Comments

I just tried to run your code and it doesnt work with that change
Your output gives number 5 as This....the OP wants it to say HalliHallo, sorry if I am missing something
I explained why that isn't possible with a Pointer function, it needs to be Pointer to Pointer.
ah my bad, I was too busy testing to read the explanation :) Thanks @Barmar
Maybe it gets easier to understand if you give a short explaination to call-by-value and why pointer-to-pointer is necessary
|
0

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

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.