printing a pointer to function & changing return value of the function

Hello. I'm studying the topic const. Then i immediately found myself in the topic of pointer to function. I need a help.

I want to define a pointer to function and then print this function in the terminal.

Unfortunately , my code is not qualified and as below.
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>


char *function1()
{ return "Some text";}

int main()
{
	
	std::cout << *function1 <<std::endl; //print Some text.

return 0;
}


After succeeding , i want to check and understand ""changing the return value of function".

i read it in the tutorial site http://duramecho.com/ComputerInformation/WhyHowCppConst.html . part of the tutorial i am studying on is below.

If a function which returns a fixed ‘Some text’ string is written like
1
2
    char *Function1()
    { return “Some text”;}

then the program could crash if it accidentally tried to alter the value doing
 
    Function1()[1]=’a’;

whereas the compiler would have spotted the error if the original function had been written
1
2
    const char *Function1()
    { return "Some text";}

Thanks in advance
Last edited on
closed account (jvqpDjzh)
char *function1()This is not a pointer to a function, this is a function that returns a char pointer

char (*function1)()This is a pointer to a function that returns a char
Last edited on
oh excuse me for calling it wrong. But i hope i could tell what i want to do. I want to print this function that returns a char pointer and also i want to change the returning value of that function, writing Function1()[1]=’a’;
Now i can print the function which returns char pointer. The code is below. HOwever changing return value code doesn't work. I learn it from the tutorial site , but why doesnt it work?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string.h>
#include <sstream>

char *function1()
{ return "Some text";}
using namespace std;
int main()
{
	std::stringstream ss;
	ss << function1();
 
	string s2 = ss.str();

	//function1()[1]="a";  // not working
	
	std::cout << s2 <<std::endl;

return 0;
}


error is :
test100.cpp:18:2: error: stray ‘\342’ in program
test100.cpp:18:2: error: stray ‘\200’ in program
test100.cpp:18:2: error: stray ‘\231’ in program
test100.cpp:18:2: error: stray ‘\342’ in program
test100.cpp:18:2: error: stray ‘\200’ in program
test100.cpp:18:2: error: stray ‘\231’ in program
Last edited on
closed account (jvqpDjzh)
function1()[1]="a"; // not working
This is not java, actually I don't know what you are talking about. I suggest you to use strcpy() function and copy the const char* pointer return by the function to a char* and then change the value that you want.

If you want just to print what the function returns, you can make it return a const char*, which is a C-string, which means that at the end of the string there's a '\0' character.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
const char* getConstCharPtr()
{
    return "Hello World";
}

const char* a = getConstCharPtr();
int a_length = strlen(a);//calculates the length of the string
//strlen() defined in <cstring>

char* b = new char[a_length + 1];
strcpy(b, a);//defined in <cstring>
b[3] = 'K';

std::cout << a <<'\n';//printing a const char* normally: using <<
std::cout << b <<'\n';


You don't need the stringstream class to print a const char*!

Last edited on
Good suggestion,thanks.

However , i'm studying const topic and it is explained that why we should make the return value of a function "const". The sole purpose of making return value of a function const is that it prevents any change of return value of function. I understood so far. What i actually wonder that how is it possible? We are so frustrated a change of return value of function, so we define the return value const.

Suppose that the return value of the function is not const. It technically means that the return value can be changed. Then how can we change it?
Why you would even think about doing this, I have no clue.
But here's a way:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

char* func()
{
    static char str[] = "Hello World!";
    return str;
}

int main()
{
    std::cout << "Original text: " << func();
    func()[0] = 'J'; // Changing it up a bit
    std::cout << "\nAfter changing a letter: " << func() << std::endl;
}

Yes this perfectly is what i want. The only main difference of your code and mine is "static". Actually, deleting static from the code breaks the code and causes segmentation faults. I should learn about "static".

Thanks for your help , zwilu and long double main .
Topic archived. No new replies allowed.