C/char pointer
Expert: Narendra - 7/14/2006
QuestionHi Narendra,
I'm getting bit confused with the character pointer that I used.
The program is like this, from 'main', function caller is called with an argument char pointer (init to NULL).
In the caller function, it again call another function caller2 which also passing the char pointer passed from main.
In caller2, this function perform a malloc to allocate 5 bytes of memory and putting value "helo" in to that memory locaion pointed by pointer from main.
When the function call returns, my char pointer from main did not get modified. I want the char pointer from main get modified. Right now it seem like a scoping problem where the char pointer from main still having NULL after the function call returned.
Here is my program. Can you let me know where is my mistake ?
thanks
Chi Tsung.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void caller(char *input);
void caller2(char *a);
int main() {
char *testing=NULL;
printf("%10s(%d): testing ptr addr [%p]\n",__FUNCTION__,__LINE__,testing);
caller(testing);
printf("%10s(%d): testing is [%s]\n",__FUNCTION__,__LINE__,testing);
printf("%10s(%d): leaving testing ptr addr [%p]\n",__FUNCTION__,__LINE__,testing);
system("PAUSE");
return 0;
}
void caller(char *input) {
printf("%10s(%d): input address is [%p]\n",__FUNCTION__,__LINE__,input);
caller2(input);
printf("%10s(%d): input is [%s]\n",__FUNCTION__,__LINE__,input);
printf("%10s(%d): leaving input address is [%p]\n",__FUNCTION__,__LINE__,input);
}
void caller2(char *a) {
char *temp=NULL;
printf("%10s(%d): a address is [%p]\n",__FUNCTION__,__LINE__,a);
temp = (char *)malloc(5);
memset(temp,0x00,sizeof(a));
sprintf(temp,"test");
printf("%10s(%d): before temp addr [%p]\n",__FUNCTION__,__LINE__,temp);
printf("%10s(%d): before a addr [%p]\n",__FUNCTION__,__LINE__,a);
a=temp;
printf("%10s(%d): after temp addr [%p]\n",__FUNCTION__,__LINE__,temp);
printf("%10s(%d): after a addr [%p]\n",__FUNCTION__,__LINE__,a);
printf("%10s(%d): a is [%s]\n",__FUNCTION__,__LINE__,a);
}
AnswerTry this corrected code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void caller(char **input);
void caller2(char ***a);
int main() {
char *testing=NULL;
printf("%10s(%d): testing ptr addr [%p]\n",__FUNCTION__,__LINE__,testing);
caller(&testing);
printf("%10s(%d): testing is [%s]\n",__FUNCTION__,__LINE__,testing);
printf("%10s(%d): leaving testing ptr addr [%p]\n",__FUNCTION__,__LINE__,testing);
// system("PAUSE");
return 0;
}
void caller(char **input) {
printf("%10s(%d): input address is [%p]\n",__FUNCTION__,__LINE__,*input);
caller2(&input);
printf("%10s(%d): input is [%s]\n",__FUNCTION__,__LINE__,*input);
printf("%10s(%d): leaving input address is [%p]\n",__FUNCTION__,__LINE__,*input);
}
void caller2(char ***a) {
char *temp=NULL;
printf("%10s(%d): a address is [%p]\n",__FUNCTION__,__LINE__,*a);
temp = (char *)malloc(5);
memset(temp,0x00,sizeof(**a));
sprintf(temp,"test");
printf("%10s(%d): before temp addr [%p]\n",__FUNCTION__,__LINE__,temp);
printf("%10s(%d): before a addr [%p]\n",__FUNCTION__,__LINE__,**a);
**a=temp;
printf("%10s(%d): after temp addr [%p]\n",__FUNCTION__,__LINE__,temp);
printf("%10s(%d): after a addr [%p]\n",__FUNCTION__,__LINE__,**a);
printf("%10s(%d): a is [%s]\n",__FUNCTION__,__LINE__,**a);
}