Hi
I am new to this and I don't understand how I can pass a memory location from my source function to my destination. Here is what I mean. I have a function "test ( char * p)". When I call this function from main () I pass in the "p" and test () should modify the contents.
void test ( char*p )
{
cout << "p=" << p;
p = "test";
cout << "p=" << p;
}
int main (void)
{
char * p = "aa";
cout << "p=" << p;
test (p);
cout << "p=" << p;
return 0;
}
Here is the output i get:
p=aa
p=aa
p=test
p=aa
But I want to know why the last "p" print out is not "test"?
Can anybody help me figure out what I'm missing?
2 replies to this topic
#1
Posted 18 September 2011 - 10:56 AM
|
|
|
#2
Posted 19 September 2011 - 05:35 AM
You've changed the value of the pointer in the test() function call rather than the data that the pointer was pointing at.
Remember that a quoted string in C is nothing more than a pointer to constant data available in the DATA segment of the resultant executable, and that p is just a pointer, there is no smart built-in technology to copy the contents from one string to another. What's more, p is local in scope for both main() and test(), which means a modification to p in test() will not change the value of p in main().
What you're trying to do is copy the string "test" into the buffer pointed at by p, and right now you're just assigning a local pointer. Remember that the passed buffer has to be big enough for the new characters, which means that "tt" isn't going to work!
Now let's change test() to actually modify the buffer being pointed to by p instead of p itself.
That should work. I should also note that you should NOT use strcpy() unless you absolutely know the size of the buffer and the size of the text you're placing in that buffer! If you don't know either of these, you should use strncpy() instead. Look up strncpy() on Google to find more.
Remember that a quoted string in C is nothing more than a pointer to constant data available in the DATA segment of the resultant executable, and that p is just a pointer, there is no smart built-in technology to copy the contents from one string to another. What's more, p is local in scope for both main() and test(), which means a modification to p in test() will not change the value of p in main().
What you're trying to do is copy the string "test" into the buffer pointed at by p, and right now you're just assigning a local pointer. Remember that the passed buffer has to be big enough for the new characters, which means that "tt" isn't going to work!
int main(void)
{
char p[5]; // Give it enough space for "test"
// PLUS THE ENDING \0 CHARACTER!
strcpy(p, "tt"); // strcpy performs a copy. You can
// find strcpy in string.h, or in C++, cstring.
std::cout << "p = " << p << std::endl;
test(p);
std::cout << "p = " << p << std::endl;
}
Now let's change test() to actually modify the buffer being pointed to by p instead of p itself.
void test(char *p)
{
std::cout << "p = " << p << std::endl;
strcpy(p, "test");
std::cout << "p = " << p << std::endl;
}
That should work. I should also note that you should NOT use strcpy() unless you absolutely know the size of the buffer and the size of the text you're placing in that buffer! If you don't know either of these, you should use strncpy() instead. Look up strncpy() on Google to find more.
Wow I changed my sig!
#3
Posted 20 September 2011 - 06:34 AM
I've noticed that this confuses a lot of people mainly because of the same variable naming. Try to avoid naming all variables in same pattern, you can try with hungarian notation. I'm not saying you must use this style as there are many too choose from or even come up with your own, but it might help you understand global/local scope and things like that.
Also, since you're coding in C++ why not use std::string?
Also, since you're coding in C++ why not use std::string?
A conclusion is where you got tired of thinking.
#define class struct // All is public.
1 user(s) are reading this topic
0 members, 1 guests, 0 anonymous users


Sign In
Create Account


Back to top









