Search code examples
cpointersc-preprocessorstring-literals

Does data under address assigned using #define can be overwritten?


I have a hard time uderestanding how #define works when combined with pointers.

Here is my pseudocode:

#define ID "28"      // I need to keep it as string

int main()
{
    char * my_id = ID;
    ...
    ...
}

Now what is actually my_id pointing to ? I did not call alloc nor statically allocated memory for my variable so can data under address my_id be overwritten?


Solution

  • A #define just does text substitution. So what you have is equivalent to:

    char *my_id = "28";
    

    What this means is that my_id points to the string constant "28". String constants are typically stored in a read-only data section, so there's nothing to allocate/deallocate.