Solved

Char* Problem Again!

Posted on 2001-06-11
13
208 Views
Last Modified: 2010-04-02
Hi,

In my sample program, some of the code like this:

void FunctionOne()
{
    unsigned char *temp = NULL;

    temp = (usigned char*) malloc(100);

    FunctionTwo(temp);
 
    printf("temp = %s\n", temp);

}

void FunctionTwo(unsigned char* Buffer)
{
  // don't know the implementation here
}

I don't know why the value of temp can be changed after calling FunctionTwo.

Any idea in doing this?

Thanks!
0
Comment
Question by:ee_lcpaa
13 Comments
 
LVL 3

Expert Comment

by:ruff_ryder
ID: 6179776
I'm not clear on what you're trying to say. Are you asking how to change the value of temp in FunctionTwo() ?
0
 
LVL 3

Expert Comment

by:GlennDean
ID: 6179802
Are you saying the value of temp is changed or what temp points to is changing?  If it is the later (doubtful) then the following shows how:

void FunctionTwo(unsigned char* Buffer)
{
 // don't know the implementation here
 Buffer[0] = 'a';
 Buffer[1] = '\0';
}

If it is the former (i.e. temp is changed), then you should change your check to
void FunctionOne()
{
   unsigned char *temp = NULL;

   temp = (usigned char*) malloc(100);

  if (temp!=NULL)
  {
    printf("temp = %x\n",temp);
   FunctionTwo(temp);

   printf("temp is now equal to %x after calling FunctionTow\n", temp);
  }

}


0
 
LVL 2

Expert Comment

by:agriggs
ID: 6179826
Temp is changing because you are passing a pointer to the FunctionTwo.  Once FunctionTwo has the address of the data, it can stick anything it wants to into that address.  This you cannot change if you don't have access to the code of FunctionTwo.  If you absolutely need to know the value of temp before FunctionTwo, you will have to make a copy of the data in temp prior to calling FunctionTwo.
0
 
LVL 2

Expert Comment

by:MadYugoslav
ID: 6180252
If You mean why is the value of temp changed by changing the values of some elements in array, this is because FunctionTwo change that elements.
If You mean why is the value of temp changed by changing the pointer temp, that can be only if there is some error in FunctionTwo. Possible is that FunctionTwo try to access elements in array temp which is out of array upper bound.
0
 

Expert Comment

by:irisistable
ID: 6181714
in FunctionTwo you can simply write :
scanf("%s", Buffr);
or
strcpy(Buffer, "new value for temp");

or other things like that.
BUT - remember that the number of characters that temp can have (Buffer in FunctionTwo) is 100, including the '\0' at the end. if you read from the keyboard, with scanf(), beware to limit to that length (read scanf documentation on how to do that).

remark - the value of temp can never change in FunctionTwo. only the value of what temp points too (in other words, the 100 consecutive bytes). if you wish to change the value of temp, or in other words - make it point to another place in memory, than you need to pass the address of temp (&temp) and recieve a char** Buffer in FunctionTwo.

another remark - please add free(temp) at the and, or a memory leak is caused.
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6182286
It is not clear what the problem is. Why do you think there is a problem, iow, what leads you to post this question?
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:pamboo
ID: 6182698
The value of temp is altered bcos the string is passed to the function which uses call by reference...

I feel that u confuse with call by reference and call by value...

Here it is call by reference...

So any change on temp in FunctionTwo will reflect on FunctionOne...

P.S: I feel u thought that Strings are same as basic data types like int,double...But actually it is an array....So passing an array as argument to a function is nothing but passing the reference....

Sorry if more explanation(unwanted...)

Bye


0
 

Expert Comment

by:hjainu
ID: 6182959
Temp is changing because a pointer is being passed in FunctionTwo.Passing pointers as arguments in functions are the same as passing variables by reference and this causes change in values.
For eg. The value of the temp can be changed by:

1. void FunctionTwo(unsigned char* Buffer)
         Calling it in the main program by passing temp
 FunctionTwo(temp)

2. void FunctionTwo(&char)
         Calling it in the main program  
       FunctionTwo(temp) This is passing by reference and also causes change in value of temp.In both cases you are accessing same memory address and putting values of temp in it causing the change.






 
0
 
LVL 7

Expert Comment

by:KangaRoo
ID: 6183319
I don't see anything new in your contribution, hjainu, what you propose has been said earlier by others.

Passing an object by reference (or passing a pointer to an object) itself  will not change the object. The function does not neccesarily have to change anything, but it might.

Unless:
   void foo(const char*);
also accepts a pointer, but it will not change the object pointed at.

>> void FunctionTwo(&char)
A typo I suppose, should be
   void FunctionTwo(char&);
And calling it with the variable temp as it is given in the question will have the compile complaining.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6401474
This question was LOCKED with a PROPOSED ANSWER and awaits your decision today.  Once a question is LOCKED with a Proposed Answer, few new experts will step in to help on that question, since the assumption is, you've been helped.  If the Proposed Answer helped you, please accept it and award that expert.  If it did not help you, please reject it and add comments as to status and what else is needed.

If you wish to award multiple experts, just comment here with detail, I'll respond as soon as possible.  As it stands today, you asked the question, got help and not one expert was awarded for the contribution(s) made.  Your response is needed.  I'll monitor through month end, and if you've not returned to complete this, we'll need to decide.  Expert input is welcome (as always) to determine the outcome here if the Asker does not respond.

Your response in finalizing this (and ALL) your question(s) is appreciated.

Site-related HELP:  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6811481
Proposed Answer rejected, question abandoned.
------------>  EXPERTS:
 
Please leave any comments regarding this question here on closing recommendations.
Thank you everyone.
 
Moondancer
Community Support Moderator @ Experts Exchange
0
 
LVL 11

Expert Comment

by:griessh
ID: 6817151
I suggest to PAQ and refund, since the asker never clrified what his/her real problem was. There are enough valuable comments in here to justify the PAQ.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
======
Werner
0
 
LVL 1

Accepted Solution

by:
Moondancer earned 0 total points
ID: 6817179
Thank you, Werner, this has been done.
EE Moderator
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

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…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

705 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now