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;
}

Recommended Answers

All 3 Replies

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.

First use code tags.

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

pointer = "weeeeeeeeeeeeeeeeeeeeeee";
return 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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.