Thread: Concatenation of strings as char pointers

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User
    Join Date
    May 2005
    Posts
    7

    Question Concatenation of strings as char pointers

    haven't used C in a while....so this might be really simple for most of you....

    can someone please tell me how to concatenate two char * strings?

    eg.
    char * c1 = "SYSTEM PATH = "
    char *c2 = getenv("PATH");

    how do I concatenate c1 + c2 and put the result into a third char * variable?

    Thanks very much.

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    847

  3. #3
    Registered User
    Join Date
    May 2005
    Posts
    7
    that doesn't work here cos I guess the getenv function returns a pointer to the whole PATH but the PATH does not have a defined '\0' character at the end....

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(void)
    {
       char *a = "SYSTEM PATH = ";
       char *b = getenv("PATH");
       if ( b != NULL )
       {
          char *c = malloc(strlen(a) + strlen(b) + 1);
          if ( c != NULL )
          {
             strcpy(c, a);
             strcat(c, b);
             puts(c);
             free(c);
          }
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    Registered User
    Join Date
    May 2005
    Posts
    7
    thanx a lot for your reply....

    however, i ran the code that you listed in a MS vc++ environment, and it gives me an error:

    error C2440: 'initializing' : cannot convert from 'void *' to 'char *'
    Conversion from 'void*' to pointer to non-'void' requires an explicit cast

    thanks,
    sameer

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Compile the C code as C? If you have the file saved as .cpp, try it again as a .c file.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Not in C it isn't. In C++ yes. But not in C. You never need to typecast the return of malloc, and in fact you shouldn't. Search the FAQ or forum for why.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 08-11-2008, 11:02 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  4. code condensing
    By bcianfrocca in forum C++ Programming
    Replies: 4
    Last Post: 09-07-2005, 09:22 AM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM