Thread: appending extra byte to a char *

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    3

    appending extra byte to a char *

    Hi,

    I trying to append something in a char pointer array but coudn't able.. can anyone please help me?

    Example:

    char* a = "World";

    now i want to add "hello" before the "World" in the variable a.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You can't, without invoking undefined behaviour. In your example, "World" is a constant string, so cannot be modified.

    Declare an array long enough to contain all the data you need, and modify it accordingly.

    Code:
    #include <string.h>
     
    int main()
    {
         char a[12] = "World";       /*  12 = strlen("Hello World") + 1  */
         strcpy(a, "Hello World");   /*  Copy "Hello World" into the array a */
    }
    This is one case where a pointer does not behave like an array.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Location
    INDIA
    Posts
    64
    If you want to append character to a char * you need to allocate memory for that .

    Code:
    #include<stdio.h>
    #include<string.h>
    #include<malloc.h>
    main()
    {
    char *a="Hello ";
    char *b="World";
    char *c = malloc(strlen(a)+strlen(b)+1);
    strcat(c,a);
    strcat(c,b);
    printf("%s\n",c);
    }

  4. #4
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by karthigayan View Post
    printf("%s\n",c);
    [/code]
    thanks karthigayan... it works.. but in printing i am getting some garbage value before the actual one... like...

    "........................ Hello World"


    can you please tell me what could be the problem?

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,776
    Quote Originally Posted by apus29
    can you please tell me what could be the problem?
    There are a number of problems with karthigayan's example, among them:
    • <stdlib.h> should be included instead of <malloc.h>
    • Although c points to sufficient memory, it does not actually point to the first character of a string as the dynamic array is left uninitialised.
    • There is no corresponding free() to the malloc().
    • a and b should be declared as points to const char.
    • The main function should be declared as returning int
    • Unless you are compiling with respect to C99, the main function should return a value, usually 0.
    • The code is severely in need of proper indention.


    You might want to try:
    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main(void)
    {
        const char *a = "Hello ";
        const char *b = "World";
        char *c = malloc(strlen(a) + strlen(b) + 1);
        c[0] = '\0';
        strcat(c, a);
        strcat(c, b);
        printf("%s\n", c);
        free(c);
        return 0;
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Mar 2010
    Posts
    3
    Quote Originally Posted by laserlight View Post
    There are a number of problems with karthigayan's example,
    Thanks laserlight.. for your informational reply..

    I had to add <stdlib.h> to run the program and i also freed the memory too..

    what i did is here...

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include <stdlib.h>
    
    int main()
    
    {
    char *a="Hello ";
    char *b="World";
    char *c = (char*)malloc(strlen(a) + strlen(b) + 1);
    strcpy(c,a);  //use strcpy to avoid the garbage value
    strcat(c,b);
    
    a = c;
    
    int i;
    
    for(i = 0; i < strlen(a); i++)
    	printf("%c", *(a+i));
    
    free (c);
    
    return 0;
    
    }
    and its giving my desired answer (to modify the variable 'a').please correct me if there is anything wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Need help understanding info in a header file
    By hicpics in forum C Programming
    Replies: 8
    Last Post: 12-02-2005, 12:36 PM
  4. error: identifier "byte" is undefined.
    By Hulag in forum C++ Programming
    Replies: 4
    Last Post: 12-10-2003, 05:46 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM