Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

Char* Problem Again!

Posted on 2001-06-11
Medium Priority
257 Views
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
Question by:ee_lcpaa
[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

LVL 3

Expert Comment

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

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

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

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

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

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

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

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

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

ID: 6401474

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.

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

LVL 1

Expert Comment

ID: 6811481
------------>  EXPERTS:

Thank you everyone.

Moondancer
Community Support Moderator @ Experts Exchange
0

LVL 11

Expert Comment

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.

======
Werner
0

LVL 1

Accepted Solution

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

Featured Post

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, whâ€¦
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.htâ€¦
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 learn how to clear a vector as well as how to detect empty vectors in C++.
Suggested Courses
Course of the Month5 days, 4 hours left to enroll