Thread: adding extra to char*

  1. #1
    Registered User
    Join Date
    May 2007
    Posts
    24

    adding extra to char*

    Hi,i was doing c program to read files and manipulate it.

    i got
    Code:
    char *word="test";
    then, i wanted to add a extra letter at the end? how to do it?any one can tell?

    that means i wanted

    Code:
    char *word="test";
    word[4]='z';  //adding z to the end so i can get 'testz'
    word[5]='\0';
    //word would now be 'testz' instead of 'test'
    the above doesnt work.any idea to go about adding a extra letter at the end.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Well, the first step would be to make sure that there is space for the extra letter. The simplest way to do that is to create an array instead of just pointing to a literal constant [that is, your "test" string].

    The other problem, which is automatically solved too by the above approach, is that your constant "test" is really a CONSTANT - you can't modify the content of the memory where it resides.

    So here's how to do it:
    Code:
    char word[6]="test";
    word[4]='z';  //adding z to the end so i can get 'testz'
    word[5]='\0';
    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If you want to add an extra string rather than an extra character, you could assign word[4] and word[5] etc, but it would be rather tedious. A better solution in this case is to use strcat().
    Code:
    #include <stdio.h>
    #include <string.h>  /* for strcpy() and strcat() */
    
    int main() {
        char *firstname = "John", *lastname = "Doe";  /* constant string literals, cannot be modified */
        char name[100];
    
        strcpy(name, firstname);  /* strcpy(), not strcat(), for an uninitialized string */
        strcat(name, " ");
        strcat(name, lastname);
    
        printf("Hello, &#37;s!\n", name);
    
        return 0;
    }
    Alternatively, you could use sprintf().
    Code:
    sprintf(name, "%s %s", firstname, lastname);
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. buffer has extra contents in gtk+ 2.0
    By MK27 in forum Linux Programming
    Replies: 5
    Last Post: 08-04-2008, 11:57 AM
  2. SVN Import Causes Crash
    By Tonto in forum Tech Board
    Replies: 6
    Last Post: 11-01-2006, 03:44 PM
  3. Java: Sorting and Adding a Row in Table
    By alphaoide in forum Tech Board
    Replies: 4
    Last Post: 08-12-2005, 08:22 PM
  4. Adding extra characters to output
    By COBOL2C++ in forum C++ Programming
    Replies: 2
    Last Post: 07-11-2003, 08:12 AM