Solved

pointer arguments

Posted on 2000-03-14
6
177 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 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
Independent Software Vendors: 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

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!

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…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

695 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