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
13
Medium Priority
?
257 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
[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
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Industry Leaders: 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

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

670 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