Hi all,
Sorry if this is a stupid question.

Assume I have a code like this.

char *temp = "poiuytrewq";
std::cout<<*temp<<std::endl;  //should print p
function(temp);
std::cout<<*temp<<std::endl;  //should print i

Basically my function() in the above snippet increments the pointer twice so that temp point to 'i' now. I need to know the prototype of function() which can accept a pointer to a character array and change where it points to (something like pass by reference).

something like pass by reference

I can only show you actual pass by reference, not something like pass by reference.

void function(char*&);

Edited 5 Years Ago by Moschops: n/a

Hi all,

char *temp = "poiuytrewq";

You need to occupy memory before you do the initialisation:

char *temp = new char(255);
temp = "poiuytrewq";

new uses brackets, not parenthesis. Furthermore, it's called allocating memory, not occupying it.

char* temp = new char[255];
temp = "poiuytrewq";

Edited 5 Years Ago by Tumlee: n/a

new uses brackets, not parenthesis. Furthermore, it's called allocating memory, not occupying it.

char* temp = new char[255];
temp = "poiuytrewq";

Yes, sorry, I'm used to use new operator with object constructors. The code I wrote wasn't incorrect it just wouldn't do what it is supposed to. new char(10) initialises a character with the ASCII value 10. Furthermore, please be kind to inform me in advance the next time I'll have to sit an English exam and I'll try to make some more sense in my writings just so that your eyeballs won't have to suffer by any mistaken syntax or grammar that cruel posters like me use.

In this case it's not necessary. Anything in quotes, for example

"some text"

is a string literal. Those characters exist in that arrangement throughout the entire life of the program. They cannot be changed, but you do not need to allocate memory yourself for them. As such,

char *temp = "poiuytrewq";

does not need any use of new.

I'll try to make some more sense in my writings just so that your eyeballs won't have to suffer by any mistaken syntax or grammar that cruel posters like me use.

Don't forget to also work on your C++ so that you don't hand out incorrect information. :)

Edited 5 Years Ago by Moschops: n/a

thanks for all your replies.
I did some more googling. And i got the solution

char *temp = "poiuytrewq";
std::cout<<*temp<<std::endl;  //should print p
function(&temp);
std::cout<<*temp<<std::endl;  //should print i

And the function prototype will be

void function(char **temp);

You've come up with a solution that works, but is significantly less efficient than simply passing by reference. To anyone in the future reading this thread; please consider going the simple elegant route of passing by reference, as in my first post above.

Edited 5 Years Ago by Moschops: n/a

Thank you Moschops. Just out of curiosity I am asking, how is it more efficient? Your answer might be useful for others too.

This question has already been answered. Start a new discussion instead.