Solved

pointer arguments

Posted on 2000-03-14
6
174 Views
Last Modified: 2010-04-02
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..
0
Comment
Question by:anemos
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 30 total points
ID: 2615550
I just answered that in the C section. hold on.
0
 

Author Comment

by:anemos
ID: 2615558
ooops! sorry for moving!
I just though that this is a more active area...
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 2615561
>> 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
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 7

Expert Comment

by:KangaRoo
ID: 2615562
Never mind, such a thing rarely happens.
0
 

Author Comment

by:anemos
ID: 2615568
give me a minute to try it out..
0
 

Author Comment

by:anemos
ID: 2615576
Perfect! Quick and precise!
May god bless you and never let your machine crash!
 Thanx a lot,

 Nick
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question