strcat

This is a discussion on strcat within the C Programming forums, part of the General Programming Boards category; i have const char *a const char *b where a and b will get string values strcat(a,b) gives me error: ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    strcat

    i have
    const char *a
    const char *b
    where a and b will get string values
    strcat(a,b)
    gives me error:
    discards qualifier from pointer target type
    how can i add a and b so
    i am using C programming in linux

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Redefine a so that it's not a const...Actually neither of them should be const because they'll be receiving values somewhere besides the declaration.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    not possible

    i cant change a and b ........it has to be const........
    is there anyother way to add the contents of a and b

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    Code:
    {
      char merged[BUFSIZ], *m = merged, *p;
    
      for(p = a;*p && p-a < BUFSIZ-1;p++)
        *m++ = *a;
      for(p = b;*p && p-b < BUFSIZ-1;p++)
        *m++ = *b;
      *m = '\0';
    }
    Something like that perhaps.

    But just so you know:
    Code:
    {
      const char *a;
      const char *b;
    }
    ...is never right. How can you define it as const without giving it a value? The fact that it's a const says that you won't be modifying it.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    not possible

    i cant change a and b ........it has to be const........
    is there anyother way to add the contents of a and b

  6. #6
    Registered User
    Join Date
    Aug 2004
    Posts
    36

    error

    using that code gave me this error
    warning: assignment discards qualifiers from pointer target type
    warning: assignment discards qualifiers from pointer target type

  7. #7
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,163
    That's okay...it wasn't quite right anyway (the bounds checking against BUFSIZ were incorrect in the second loop)

    Try using an index variable and using a and b in array notation instead.

    I still don't understand why your two variables need to be const. The fact that you're so hell-bent on it makes it seem like a homework assignment and I've probably already helped out more than I should code-wise.
    If you understand what you're doing, you're not learning anything.

  8. #8
    Registered User
    Join Date
    Mar 2004
    Posts
    16
    Code:
    const char *a = "some string ";
    const char *b = "some other string";
    char *c;
    
    /* Allocate memory for both strings and string terminator ('\0'): */
    if ((c = malloc(strlen(a) + strlen(b) + 1)) != NULL) {
        strcpy(c, a); /* Copy string a to string c. */
        strcat(c, b); /* Concatenate string b to string c. */
    
        /* Show strings: */
        printf("a=%s\nb=%s\nc=%s\n", a, b, c);
    
        /* After use, free allocated memory: */
        free(c);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting question about strcat
    By meili100 in forum C++ Programming
    Replies: 3
    Last Post: 07-07-2009, 12:59 PM
  2. strcat the inside of a structure
    By ebullient in forum C Programming
    Replies: 3
    Last Post: 12-09-2005, 04:58 PM
  3. strcat and a char
    By jjacobweston in forum C++ Programming
    Replies: 2
    Last Post: 05-09-2005, 04:10 PM
  4. strcat makes program crash
    By imoy in forum C++ Programming
    Replies: 4
    Last Post: 05-09-2002, 02:41 PM
  5. Removing 'strcat' ed string.
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:09 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21