0

if i were to do this in a program, would it possibly overwrite data/memory that is already being using by my application?

int main()
{
char *pointer = "hi";

pointer = "weeeeeeeeeeeeeeeeeeeeeee";
return 0;
}

4
Contributors
3
Replies
5
Views
6 Years
Discussion Span
Last Post by taylorg
1

No your data is safe. When you reassign pointer, its just gets the address of your string "weeeeeeeeeeeeeeeeeeeeeee".

"hi" - has one address value.
"weeeeeeeeeeeeeeeeeeeeeee" - is another address value.

Edited by gerard4143: n/a

0

First use code tags.

int main()
{
char *pointer = "hi";

pointer = "weeeeeeeeeeeeeeeeeeeeeee";
return 0;
}

Edited by alexchen: n/a

0

Your data will not be overwritten, as gerard4143 said. If you would like to see for yourself, try this code:

int main()
{
    
    char *pointer = "hi";
    
    char *oldaddr = pointer; /* backup pointer */
    
    printf("%X%s", pointer, "\n\n");
    
    pointer = "weeeeeeeeeeeeeeeeeeeeeee";
    
    printf("%X%s", pointer, "\n\n");
    
    pointer = oldaddr; /* restore old pointer */
    
    printf("%s\n\n", pointer, "\n"); 
    
    return 0;    
}

Good luck with your program!

Edited by taylorg: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.