| Xathael (10) | |||||||
|
Hello. I've been studying pointers and wrote a code that shows an error for some reason. The target of the function is to get 2 strings (name and last name) and 2 pointers and make the pointers point to the strings. My function is:
my main code:
that doesn't work. But once I delete the function and use the code without it it works perfectly
why isn't my code working when I use a function but it does when I just use the code by itself? Thanks in advance. edit: The code compiles perfectly but the p_name and p_last are probably not set right and when I try to change them (for example *p_name = "Dave") it shows an error when I debug. | |||||||
|
Last edited on
|
|||||||
| Athar (4466) | |
|
You assign values to the local variables theName and thelast, which are destroyed when the function exits. If you want to change the original arguments that were passed, use references. | |
|
|
|
| Xathael (10) | |||||
tried changing the function to look like this:
and in the code:
but didn't see any changes. Still the same error... I can't figure out what I'm doing wrong. | |||||
|
|
|||||
| Athar (4466) | ||
This makes no sense. It's not allowed to dereference null pointers, so this is wrong:*p_name, *p_last
These are references to strings. However, p_name and p_last are not strings, they're pointers to a string (string*). | ||
|
|
||