Link to home
Start Free TrialLog in
Avatar of ee_lcpaa
ee_lcpaa

asked on

Char* Problem Again!

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!
Avatar of ruff_ryder
ruff_ryder
Flag of United States of America image

I'm not clear on what you're trying to say. Are you asking how to change the value of temp in FunctionTwo() ?
Avatar of GlennDean
GlennDean

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);
  }

}


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.
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.
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.
It is not clear what the problem is. Why do you think there is a problem, iow, what leads you to post this question?
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


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.






 
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.
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:  https://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp
Moondancer
Community Support Moderator @ Experts Exchange
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
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
ASKER CERTIFIED SOLUTION
Avatar of Moondancer
Moondancer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial