Re: malloc modifying a passed string
- From: "Sarin" <suseelan@xxxxxxxxx>
- Date: 28 Jul 2005 05:26:30 -0700
Hi,
If you want to modify the pointer in your function, u need to pass the
pointer to that pointer. I tried the program below and it worked for
me.
Regards,
Sarin
#include <stdio.h>
#define STRM "My Main String"
#define STRF "My Function String"
void change_ptr(char **ptr);
int main()
{
char *ptr1;
char *ptr2;
ptr1=malloc(sizeof(STRM)+1);
strcpy(ptr1,STRM);
ptr2=ptr1;
printf("ptr1: %s ptr2: %s \n",ptr1,ptr2);
change_ptr(&ptr1);
printf("ptr1: %s ptr2: %s \n",ptr1,ptr2);
return 0;
}
void change_ptr(char **ptr)
{
*ptr=malloc(sizeof(STRF)+1);
strcpy(*ptr,STRF);
}
.
- References:
- malloc modifying a passed string
- From: Scott Taylor
- Re: malloc modifying a passed string
- From: Chris Hulbert
- malloc modifying a passed string
- Prev by Date: Re: Copying a struct to a larger struct?
- Next by Date: Re: Should function argument be changed in function body?
- Previous by thread: Re: malloc modifying a passed string
- Next by thread: Re: malloc modifying a passed string
- Index(es):
Relevant Pages
|