concat const char * with char *

hello everybody!
i have aproblem! i dont know how to concatenate const char* with char
const char *buffer;
char *b;
sprintf(b,"result.txt");
strcat(buffer,b);

thanx in advance

You cannot strcpy( const char *buffer, char *src)

This is because you told the compiler with "const" that you promised not to alter buffer.
You will have to create a temorary char * variable then sprintf buffer + "results.txt" into the temporary char variable.

post your code and we can help.

Hi,

const char *buffer does not mean that buffer is a constant. It only says buffer points to a constant char. To declare buffer as a constant, you will have give,

char * const buffer;

Here in your code, you cannot give

char *b
sprintf(b,"result.txt");

Either you have to allocate memory to b and then use sprintf or strcpy or you can use,

b="result.txt";

To append something to buffer, you dont want to concatinate the new string with buffer, you may append the string to the array buffer points to. You cannot give strcat(buffer,b) as strcat does not allow const char * as its first argument.

You may try this

char b[1024]={"result.txt"}; //You may try dynamic allocation also
const char *buffer=b;

cout<<"\n Value of buffer 1 : "<<buffer<<endl; //It will be result.txt

strcat(b,",result1.txt");

cout<<"\n Value of buffer 2 : "<<buffer<<endl; //It will be result.txt,result1.txt

.

I get this with a C99-compliant compiler:

csaprd:/home/jmcnama> cc t.c
cc: "t.c", line 6: error 1549: Modifiable lvalue required for assignment operator.
cc: "t.c", line 7: warning 611: Type conversion loses "const" qualifier.
cc: "t.c", line 7: warning 563: Argument #1 is not the correct type.

on this code:

#include <string.h>
#include <stdlib.h>

const char *mystrcpy(const char *dest,const char *source, const size_t len)
{
    *dest=0x0;
	memcpy(dest, source, len);
	return dest;
}

int main()
{
    char dest[10]={0x0};    
    const char *p=mystrcpy(dest, "test", 4);
    printf("%s\n", dest);
    return 0;
}

const char *foo means it cannot be modified (the "modifiable lvalue" complaint), for example. ujeshm may use a non-standard compiler or a really old one that allows it.

You can work around it by copying a const char into a regular string then modifying the new string.

Note: A warning means the code did not compile correctly, and if it runs you are then coding by coincidence and will sooner or later cause serious problems that are difficult to resolve.

If you are using gcc, compile

gcc -Wall mycode.c 

to see full errors and warnings.

"const char *foo means it cannot be modified" -> This is not completly correct. 'const char *foo' means foo is a pointer which points to a constant char (pointer to a constant variable, not a constant pointer variable). That means, the address in foo can be changed(foo is not a constant), but we cannot modify the value contained in the address held by foo(the value is considered as constant).

For example

const char *foo;

foo="Test1";

cout<<"\n foo 1 : "<<foo<<endl;  // Result will be 'Test1'

foo="Foo is changed now."       

cout<<"\n foo 2 : "<<foo<<endl;  // Result will be 'Foo is changed now'

In your code,

you got error in line 6 because, you are trying to change the value contained in the address held by 'dest'. That is if you remove the '*' it wont generate any errors. But the meaning will be different.

you got error in 7 because the dest is a const char * variable. memcpy does not allow const char * as its first argument (memcpy(void *,const void *,size_t))

The work around you are suggesting is similar to what I have given in my previous post. Only difference is you are asking to assign the address of the const char * variable to a char * variable and change the content of the char * variable. I declared the char * variable first and assigned its address to the const char * variable. After that I am also doing the same thing.

I think my previous post created some miss understandings in you. Sorry if it is so.