?
Solved

pointer arguments

Posted on 2000-03-14
6
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
6 Comments
 
LVL 7

Accepted Solution

by:
KangaRoo earned 120 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

764 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