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
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
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.
i cant change a and b ........it has to be const........
is there anyother way to add the contents of a and b
Something like that perhaps.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'; }
But just so you know:
...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.Code:{ const char *a; const char *b; }
If you understand what you're doing, you're not learning anything.
i cant change a and b ........it has to be const........
is there anyother way to add the contents of a and b
using that code gave me this error
warning: assignment discards qualifiers from pointer target type
warning: assignment discards qualifiers from pointer target type
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.
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); }