Hi i'm trying to concatenate two char and i keep getting core dumpped:

char *user = "name";
char *temp = "USER ";
strcat(temp, user);

if anybody knows what i'm doing wrong please let me know.

Thank u

Recommended Answers

All 9 Replies

Yes, you are trying to write to read-only memory. Create an array with enough space instead of trying to write to a string literal initializer.

[edit]

#include <stdio.h>
#include <string.h>

int main( void )
{
   char user[] = "name";
   char temp[ sizeof user + 5 ] = "USER ";
   strcat(temp, user);
   puts(temp);
   return 0;
}

/* my output
USER name
*/

but i don't know how much data i will recieve. and how do u do an array in c?

thank u

ohh thank u i didn't see the code u provided

but i get the user name from argv how do i convert this

Could you post a reasonably-sized snippet of the code (that demonstrates the problem) that you are attempting to debug?

user = argv;
--------------------
char temp[sizeof(user) +5] = "USER ";//anonymous\r\n";
strcat(temp, user);
printf("%s\n", temp);


writeData(s, temp, strlen(temp)); //the function writeData has parameter char*temp
---------------------------------------------
void writeData(int s, char *d, int len)
{
int numChar;
while(len != 0)
{
numChar = write(s, d, len);


if(numChar == -1)
{
fputs("An error has occured in sending data",stderr);
exit(7);
}


len -= numChar;
d += numChar;


}
}

the program just freeze with this code

Thank you soooo much man, it is working. you have saved a lot of time for me.

I have another question: what does this warning means?
implicit declaration of function `read'

I have another question: what does this warning means?
implicit declaration of function `read'

You probably have a function called before a prototype is declared, or one that looks like this:

read(/*parameters*/);

rather than

int read(/*parameters*/); /* or other return type */
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.