Thread: Concatenation of strings as char pointers

  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
    Registered User
    Join Date
    May 2005
    Posts
    7
    tried but did not work...

    i think it's cos c2 does not have an explicit '\0' character.....
    so it crashes when it encounters the strcat() function...

    -sameer

  8. #8
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    7.20.4.5 The getenv function

    Synopsis

    1 #include <stdlib.h>
    char *getenv(const char *name);


    Description

    2 The getenv function searches an environment list, provided by the host environment, for a string that matches the string pointed to by name. The set of environment names and the method for altering the environment list are implementation-defined.

    3 The implementation shall behave as if no library function calls the getenv function.

    Returns

    4 The getenv function returns a pointer to a string associated with the matched list member. The string pointed to shall not be modified by the program, but may be overwritten by a subsequent call to the getenv function. If the specified name cannot be found, a null pointer is returned.
    Using "string" in the C standard sense implies null termination.

    Were you dynamically allocating memory for the right amount of space? Or were you trying to strcat more than the string could hold?
    Last edited by Dave_Sinkula; 05-09-2005 at 04:28 PM. Reason: Spruced up quote.
    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.*

  9. #9
    Registered User
    Join Date
    May 2005
    Posts
    7
    hmm...then i guess i thought up the wrong reason....

    i did allocate a lot of space....

    let me paste some of my actual code... (you will see there's hardly any difference between the example that i discussed earlier)


    Code:
    char * classpath = new char[65536];
    
    if (classpath == NULL)
    {
    	 fprintf(stderr,"\nMemory allocation failed!");
    	 exit(1);
    }
    
    char * javapath = "-Djava.class.path=";
    
    char * env_var = getenv("JNI_CLASSPATH");
    
    
    if ( classpath != NULL )
    {
             strcpy(classpath, javapath);
             strcat(classpath, env_var);
    }

  10. #10
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    [Ew. C/C++. (As opposed to one language or the other.)]

    Is getenv successful? You're not checking it's return value. Dereferencing a null pointer will generally do bad things.

    [edit]http://www.parashift.com/c++-faq-lit....html#faq-16.6
    Last edited by Dave_Sinkula; 05-09-2005 at 04:41 PM. Reason: Added link.
    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.*

  11. #11
    Registered User
    Join Date
    May 2005
    Posts
    7
    thanx dave....

    but this finally seemed to work....i guess it was the (char *) before the malloc that was needed

    Code:
    c3 = (char *)malloc(strlen(c1) + strlen(c2) +1);
    strcpy(c3,c1);
    strcat(c3,c2);

  12. #12
    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