Thread: Reallocate CString on Heap

  1. #1
    Registered User
    Join Date
    Feb 2019
    Posts
    69

    Reallocate CString on Heap

    Hi,

    I have created a CString as:

    Code:
    char* p = malloc(3);
    p[0] = '1'
    p[1] = '1'
    p[2] = '1'
    Now, I want to change the string to: "He"

    Code:
    char* p = "He"
    But this reallocates the pointer adress. How to go around this?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do this.

    strcpy(p,"He");
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    69
    #I am stupid
    Thank you!

  4. #4
    Registered User
    Join Date
    May 2016
    Posts
    104
    I think it'd be more illustrating to change the string manually at this stage.
    Code:
    #define NULL_TERMINATION 1
    #define STR_SIZE 3 + NULL_TERMINATION
    
    char *p = malloc(STR_SIZE * sizeof(*p));
    p[0] = '1';
    p[1] = '1';
    p[2] = '1';
    P[3] = 0;
    //How do we change the string to "he"?
    p[0] = 'h';
    p[1] = 'e'
    p[2] = 0; //Otherwise we end up with "he1"
    Note that by convention, all strings in c should end with a 0 (technically a '\0' or null character which is normally 0, but it might not be in some obscure platform) which marks the end of the string. Library functions like strlen expect that, and won't work well without it.
    Last edited by Dren; 08-11-2019 at 04:56 PM. Reason: Dereference p during sizeof operation
    printf("I'm a %s.\n", strrev("Dren"));

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > char *p = malloc(STR_SIZE * sizeof(*p));
    Oooh, watch out for that operator precedence.

    Always put ( ) around all your #define expressions.

    > (technically a '\0' or null character which is normally 0,
    The ISO standard says it's always 0 (see 5.2.1 Character sets)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reallocate memory for array
    By MMG in forum C Programming
    Replies: 5
    Last Post: 05-19-2018, 10:04 PM
  2. heap short and heap tree about help me pls
    By webwebcpp in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2014, 12:30 PM
  3. getting sub CString from a bigger CString
    By hanhao in forum C++ Programming
    Replies: 1
    Last Post: 06-29-2007, 11:53 AM
  4. cstring
    By zoobiskuit in forum C++ Programming
    Replies: 5
    Last Post: 01-22-2007, 05:13 PM
  5. Replies: 2
    Last Post: 11-08-2002, 03:22 AM

Tags for this Thread