Pointer - output
-
Hi all, I would like to have a pointer(parameter) as a output of function. Is it possible?? here is my examle: void a(char *pcOutput){ pcOutput = new char[7];// here I will get an new address of heap strcpy(pcOutput,"Hello.\0"); } int main(int argc, char* argv[]) { char *pcOut = NULL; a(pcOut); printf("pointer: %s\n",pcOut);// I see - pointer: null return 0; } What am I doing wrong? Thank you.
-
Hi all, I would like to have a pointer(parameter) as a output of function. Is it possible?? here is my examle: void a(char *pcOutput){ pcOutput = new char[7];// here I will get an new address of heap strcpy(pcOutput,"Hello.\0"); } int main(int argc, char* argv[]) { char *pcOut = NULL; a(pcOut); printf("pointer: %s\n",pcOut);// I see - pointer: null return 0; } What am I doing wrong? Thank you.
You are only passing your pointer by value. In order for a called function to alter the passed value, it needs to be passed by reference...
void a(char ***&**pcOutput){
pcOutput = new char[7];// here I will get an new address of heap
strcpy(pcOutput,"Hello.\0");}
int main(int argc, char* argv[])
{
char *pcOut = NULL;
a(pcOut);
printf("pointer: %s\n",pcOut);
delete[] pcOut;
return 0;
}or you could return a pointer...
char *a(){
char *pcOutput = new char[7];// here I will get an new address of heap
strcpy(pcOutput,"Hello.\0");
return pcOutput;
}int main(int argc, char* argv[])
{
char *pcOut = a();
printf("pointer: %s\n",pcOut);
delete[] pcOut;
return 0;
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Hi all, I would like to have a pointer(parameter) as a output of function. Is it possible?? here is my examle: void a(char *pcOutput){ pcOutput = new char[7];// here I will get an new address of heap strcpy(pcOutput,"Hello.\0"); } int main(int argc, char* argv[]) { char *pcOut = NULL; a(pcOut); printf("pointer: %s\n",pcOut);// I see - pointer: null return 0; } What am I doing wrong? Thank you.
You need to have a pointer to a pointer. void a(char **ppcOutput) You also need to free the memory in the main function.
-
You are only passing your pointer by value. In order for a called function to alter the passed value, it needs to be passed by reference...
void a(char ***&**pcOutput){
pcOutput = new char[7];// here I will get an new address of heap
strcpy(pcOutput,"Hello.\0");}
int main(int argc, char* argv[])
{
char *pcOut = NULL;
a(pcOut);
printf("pointer: %s\n",pcOut);
delete[] pcOut;
return 0;
}or you could return a pointer...
char *a(){
char *pcOutput = new char[7];// here I will get an new address of heap
strcpy(pcOutput,"Hello.\0");
return pcOutput;
}int main(int argc, char* argv[])
{
char *pcOut = a();
printf("pointer: %s\n",pcOut);
delete[] pcOut;
return 0;
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
A reference to a char pointer. Noooooooooooooooooooooooooooooooooooooooooo! (Sorry only personal taste :rolleyes:) :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke -
A reference to a char pointer. Noooooooooooooooooooooooooooooooooooooooooo! (Sorry only personal taste :rolleyes:) :-D
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarkehehe yeah, but it's perfectly valid - It's just a reference to a pointer, and a pointer is just another data type. Maybe "char* &" is better style... ;P
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You are only passing your pointer by value. In order for a called function to alter the passed value, it needs to be passed by reference...
void a(char ***&**pcOutput){
pcOutput = new char[7];// here I will get an new address of heap
strcpy(pcOutput,"Hello.\0");}
int main(int argc, char* argv[])
{
char *pcOut = NULL;
a(pcOut);
printf("pointer: %s\n",pcOut);
delete[] pcOut;
return 0;
}or you could return a pointer...
char *a(){
char *pcOutput = new char[7];// here I will get an new address of heap
strcpy(pcOutput,"Hello.\0");
return pcOutput;
}int main(int argc, char* argv[])
{
char *pcOut = a();
printf("pointer: %s\n",pcOut);
delete[] pcOut;
return 0;
}Mark
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
You need to have a pointer to a pointer. void a(char **ppcOutput) You also need to free the memory in the main function.