C Pointer Help ....

Vu Pham vu at sivell.com
Wed Sep 23 07:32:34 PDT 2009


On 09/23/2009 08:42 AM, Ben Duncan wrote:
[...]
>
> int testpassptr (char *Buffer )
> {
>
> char *Buf_ptr ;
>
> if ((Buf_ptr = (char *) malloc(100)) == NULL)
> {
> printf("Memory allocation error! \n");
> return EXIT_FAILURE;
> }
>
> printf("Memory allocated successfully! \n");
>
> memset(Buf_ptr, '\0', 100 ) ;
>
> strcpy(Buf_ptr, "A TEST String" ) ;
> printf("TESTPASSPTR string: %s \n", Buf_ptr ) ;
> Buffer = Buf_ptr ;
>
>
> return 0 ;
> }

I think the problem is at the line Buffer = Buf_ptr;
In this case Buffer is a variable which contains the memory address of 
some buffer X.
If you write *Buffer = something, then X has something.
When you say Buffer = Buf_ptr then the var Buffer changes it content, X 
does not change.


To solve your problem, declare the function as "int testpassptr (char 
**Buffer )", i.e. pointer of pointer, pass &Buffer to it, and the last 
line of that function changes to *Buffer = Buf_ptr.


HTH,
Vu



More information about the Linux-users mailing list