Thread: Setting character in malloc'ed char array

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    3

    Setting character in malloc'ed char array

    I am attempting to change a character in a character array.

    In the code below, there are three attempts to do this. Only the first one will succeed. The last two both segfaults. If I understand correctly, str_one is declared in the heap, and could therefore be manipulated; and in contrast, str_two is declared in the stack and is therefore immutable, thus the segfault, when update it is attempted. However, I understand that using malloc, one is able to assign a pointer and allocate space in heap memory. Thus, I should be able to manipulate the assigned variable str_three. Doing so, however, results in a segfault.

    I hope someone can shed light on this. Thanks!


    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main (int argc, char const* argv[])
    {
      char str_one[4092] = "This is string number one";
      char * str_two = "This is string number two";
    
      char * str_three;
      str_three = (char *) malloc(4092);
      str_three = "This is the third string";
    
      str_one[5] = 'X';
      printf("%s\n", str_one);
    
      str_two++;
      *str_two = 'X'; // segfaults here
      str_two--;
      printf("%s\n", str_two);
    
      str_three++;
      *str_three = 'X'; // segfaults here
      str_three--;
      printf("%s\n", str_three);
    
    
      return 0;
    }

  2. #2
    11DE784A SirPrattlepod's Avatar
    Join Date
    Aug 2013
    Posts
    485
    Code:
       /* In this line you malloc some memory
         */
      str_three = (char *) malloc(4092);  
    
       /* 
        In this line, recognise that "This is the third string" is a string 
        literal. It evaluates to a pointer, so you're *overwriting* the pointer 
        to the memory you allocated (that you stored in str_three; you've given 
        str_three a new value). String literals are constants, so when you try 
        and write to them you will/should get a segfault 
       */
      str_three = "This is the third string";
    Edit: to set str_three to contain a string (after you allocated memory for it), use the strcpy function
    Last edited by SirPrattlepod; 10-16-2013 at 04:35 AM.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    3
    Quote Originally Posted by SirPrattlepod View Post
    ... String literals are constants, so when you try
    and write to them you will/should get a segfault


    Edit: to set str_three to contain a string (after you allocated memory for it), use the strcpy function
    That's it! Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. using malloc for an array of character pointers
    By dtkokovoko in forum C Programming
    Replies: 9
    Last Post: 03-30-2013, 05:19 PM
  2. Replies: 9
    Last Post: 11-18-2008, 02:59 AM
  3. Replies: 3
    Last Post: 11-17-2008, 12:36 PM
  4. Setting String equal to char array?
    By Stunner in forum C++ Programming
    Replies: 16
    Last Post: 07-24-2007, 02:41 AM
  5. Problem with setting a character array in a class member.
    By swbluto in forum C++ Programming
    Replies: 4
    Last Post: 12-15-2006, 02:41 AM

Tags for this Thread