1

I have a question about strings that hopefully someone can help with. I realise that doing something like:

char* word = malloc(4);
word = "THE";

is not really correct (as word is really a pointer to the first element as I understand it) and so we are trying to assign a character string to a pointer. However, in code in partially works and I would like to know what is going on. Below is some code to illustrate what I mean (with the standard way of writing one character at a time to the array as a comparison).

#include <stdio.h>
#include <stdlib.h>


int main(void)
{  
    char* word = malloc(4);
    word[0] = 'T';
    word[1] = 'H';
    word[2] = 'E';
    word[3] = '\0';

    // printing out where string is stored and the string
    printf("POINTER: %p\n", word);
    printf("STRING: %s\n", word);

    /* 
     * altering an element and reprinting. The string prints out
     * altered and memory location prints out unaltered as expected 
     * in my output 
     */ 
    word[0] = '!';
    printf("POINTER: %p\n", word);
    printf("STRING: %s\n", word);

    /*
     * trying the dodgy way. In my output the pointer is altered and
     * the string prints out fine. Why?
     */
    word = "CAT";
    printf("POINTER: %p\n", word);
    printf("STRING: %s\n", word);

    /*
     * it only partially works though as altering a single element
     * leads to a segmentation fault
     */
    word[0] = '!';
    printf("POINTER: %p\n", word);
    printf("STRING: %s\n", word);

    free(word);


}

So clearly a string is being stored somewhere but in a different location from what malloc() gave us and then after we cannot alter elements (we get a segmentation fault) only read them. If anyone can explain to me what exactly is happening I would be very appreciative!

1 Answer 1

2

I'll let you visualize this for yourself. Go here http://www.pythontutor.com/c.html#mode=edit and paste your code, then hit "visualize execution".

You'll see graphically what's happening in memory.

EDIT: I'm sorry, the server seems to be down, so I'll try to use Python to show you what the site would have shown:

visualization 1 visualization 2

And finally I'll direct you to a question regarding strings stored in read-only section in memory, like char* a = "AAAAAA":
https://stackoverflow.com/questions/164194/why-do-i-get-a-segmentation-fault-when-writing-to-a-string-initialized-with-cha

3
  • Ok, thanks Yuri. Have tried but am getting Server Error. Have tried running even more simple code and still server error so maybe "server has crashed due to too many people using it at once." Will try again bit later Oct 12, 2016 at 23:27
  • Sorry about that, I hadn't noticed the site was down for simulations in C (because in Python it's working fine now). I edited my answer. Oct 13, 2016 at 2:39
  • 1
    Thanks Yuri. Your explanation and the link to the Stack Overflow question explained it all! Oct 16, 2016 at 21:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .