Solved

Char* Problem Again!

Posted on 2001-06-11
13
227 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
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
 
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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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 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.

815 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

11 Experts available now in Live!

Get 1:1 Help Now