pointer arguments

I have declared the following variable:

char * previous;

 I have allocated space for one chat to it:

previous = malloc(1);

I have initialized it as:

previous="\0";

I want to pass it to a function that will change its size using malloc and change its contents by copying another string to it:
                   strcpy(previous,curr);

 Now when I've returned from the function previous should be containing the string.

How am I going to pass the pointer to this function in order to achieve this???

I hope you all understand what I mean..
Thanx in advance..
anemosAsked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
KangaRooConnect With a Mentor Commented:
I just answered that in the C section. hold on.
0
 
anemosAuthor Commented:
ooops! sorry for moving!
I just though that this is a more active area...
0
 
KangaRooCommented:
>> previous = malloc(1);
Better use new:
prev = new char[1];

>> previous="\0";

You've changed the pointer, not the contents of the previously allocated memory.
Might have wanted to do:

*previous = '\0';

You need to pass the pointer in a way that allows the function to change it, so pass the pointer by reference:

void change(char*& p)
{
   char* str = "hello";
   delete [] p;
   p = new char[strlen(str) + 1];
   strcpy(p, str);
}




0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
KangaRooCommented:
Never mind, such a thing rarely happens.
0
 
anemosAuthor Commented:
give me a minute to try it out..
0
 
anemosAuthor Commented:
Perfect! Quick and precise!
May god bless you and never let your machine crash!
 Thanx a lot,

 Nick
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.