Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ Function Argument Problem

Started by flery Aug 4, 2003 at 3:53 PM 16 replies 1.4k views
Original Post
flery
flery
Hi, i''ve a problem with a char function i wrote, after calling the function the argument ''input'' changes. For example input = "abc 123", the function should now set temp to "abc" and not change ''input''. The Call: temp = GetConCommand(input); The Function: char* GetConCommand(char* minput) { int round = 0; while (minput[round] != '' '') { round++; } minput[round] = ''\0''; return minput; } Need help. I made a workaround and copyed input to another var but i want to know why it works not the way i want it. greets flery
patindahat
patindahat
Your passing a pointer in, your directly changing the string. If you want to not change the passed arguement, then pass it as an array, or create a new string inside your function. This is essentially what your doing, only with a different data type.

temp = GetConCommand(input);

The Function:
int* GetConCommand(int* minput)
{
minput = 0;
return minput;
}

So your taking the pointer to some data, changing the data, then returning another pointer to the exact same data.

Pat - Ex nihilo nihilus
rypyr
rypyr
There''s no mystery here: The string only exists in one memory location, it''s not copied when you call a function. If you change it, you''re altering it forever.

Regards,
Jeff
rypyr
rypyr
quote:
Original post by Anonymous Poster
You need to make a copy of the arguement "input" because you are directly changing input with minput.

Here''s how you fix it:

char* GetConCommand(char* minput)
{
int round = 0;

char *temp;
*temp = *minput;

while (temp[round] != '' '') {
round++;
}
temp[round] = ''\0'';
return temp;
}


Ignore this. It''s wrong. It does NOT make a copy of the string...
Hyren
Hyren
Hey-
Everyone has been correct so far in telling you that you need to make a copy of your character string, otherwise when you do this you will overwrite your minput string. For instance:


char* GetConCommand(char* minput)
{
int round = 0;
char * retinput = new char[strlen(minput) + 1];
strcpy(retinput, minput);

while (retinput[round] != '' '') {
round++;
}
retinput[round] = ''\0'';
return retinput;
}


Just make sure that you delete the char * that this returns when you''re done with it, since it''s allocated via the new operator. Hope this helps.



"Back to the code mines... ka-chink... ka-chink..."
Tachyon Digital - Down for the summer, be back in the fall.
"Back to the code mines... ka-chink... ka-chink..."Tachyon Digital - Down for the summer, be back in the fall.
C-Junkie
C-Junkie
you said this was c++, use the string class.
Fruny
Fruny
quote:
Original post by C-Junkie
you said this was c++, use the string class.


ditto


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
flery
flery
okay i understand the problem now but i made some testing and came up with this

#include <iostream.h>
#include <windows.h>

char* test(char* test2);

char* d = "test";

int main()
{

char* a = "notchanged";

a = test(d);

cout << a << endl;
cout << d << endl;
return 0;
}

char* test(char* test2)
{
cout << test2 << endl;
test2 = "changed";
cout << test2 << endl;

return test2;
}

The Result is:

test
changed
changed
test

u said that its a pointer but d has not changed in the end its still ''test''
rypyr
rypyr
You need a better understanding of pointers in C/C++.

Unless you are passing references, any variable is passed by VALUE.

The pointer variable is passed by value (meaning you can change what it points to within your function and after the function it will still point to what it was pointing to before). However, the MEMORY that a pointer points to can be altered within a function by using the pointer to get to the memory:

char* d = "test"; // here, pointer d points to memory containing "test"

test(d);

// afterwards, d still points to memory containing "test"

Now within test:

char* test(char* test2) {
// at this moment, pointer test2 has the same value as d
// that is, test2 points to the same memory that d points to,
// containing "test"

test2 = "new text";
// now test2 points to new memory containing "new text",
// but d still points to the original spot of memory

return test2;
}

// this function returns a pointer to the new memory
flery
flery
now its all clear to me (at least i think so)

i pass the memory address to the function and if i use the function i created i edit the content of the memory address, with var = "new content" i would create a new memory adress and not change the old.

rypyr
rypyr
yes, but in your original function you were changing the memory that you were pointing to by doing this:

char* GetConCommand(char* minput)
{
// ...
minput[round] = ''\0'';

return minput;
}
flery
flery
a big part of understanding pointer mastered thaks to you
its weird that u can teach me better (in english) than a german book
flery
flery
hey i got another problem with the function, i want to check if the retinput is == "test" but the function wont enter the if, if i set retinput to = "test" in the function itself it works.

int GetConCommand(char* retinput)
{
int rslt = 0;
int round = 0;


while (retinput[round] != '' '')
{
round++;
}
retinput[round] = ''\0'';
if (retinput == "test")
{
MessageBox(NULL, "command was test" ,"", MB_OK|MB_ICONINFORMATION);
rslt = 1;
}
return rslt;
}
rypyr
rypyr
Use strcmp(), not ==.

== compares the values of the two pointers, not the value of the strings they point to.
Odd the Hermit
Odd the Hermit
Character arrays don''t compare the way you''d expect them to. The way you''re doing it, you''re actually comparing two memory addresses (which are never equal). You need to use strcmp (which returns 0 when the two strings are equal).

-Odd the Hermit
rypyr
rypyr
The reason that this works:

retinput = "test";
if(retinput == "test")

is because this line:

retinput = "test";

creates a constant string in memory and points retinput to it. Let''s say it does this:

Address 400 = ''t''
Address 401 = ''e''
Address 402 = ''s''
Address 403 = ''t''
Address 404 = ''\0''
retinput = Address 400

In the next line:

if(retinput == "test")

the compiler needs to create another temporary string with the same text ("test"). Your compiler is smart enough to use the same string it just created above. So the line is equivalent to this:

char* tempString = Address 400
if(retinput == tempString)

That is why the if returns true.

Always use strcmp() when comparing string values.

Regards,
Jeff
flery
flery
Thanks a lot! I thought of something like that but had not idea how to solve it.



[edited by - flery on August 5, 2003 4:50:44 PM]

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.