Questions tagged [strcat]

A standard C function that appends a copy of the source string to the destination string.

The declaration of strcat function which is found in string.h is:

char *strcat(char *dest, const char *src)

This function appends the string pointed to by src to the end of the string pointed to by dest. Here,dest is a pointer to the destination array, which should contain a C string, and be large enough to contain the concatenated resulting string and src is the string to be appended. This should not overlap destination.

strcat returns a pointer to the resulting string dest.

For more information, check the man page.

529 questions
2
votes
2 answers

Modify char array passed to a function, C

I would like to concat 2 strings inside a function. However, I would like the function to modify also the destination string (char array). So far I got this code below, but it shows 'segmentation fault' and I do not know how can I fix this.…
yak
  • 3,770
  • 19
  • 60
  • 111
2
votes
2 answers

Const char * vs const wchar_t* (concatenation)

which is the best way to concat? const char * s1= "\nInit() failed: "; const char * s2 = "\n"; char buf[100]; strcpy(buf, s1); strcat(buf, initError); strcat(buf, s2); wprintf(buf); It gives error. What should be the correct way? Thanks.
Azodious
  • 13,752
  • 1
  • 36
  • 71
2
votes
2 answers

String concatenation with variables and pointers in 'C'

I am working on Caesar's cipher for an online course and I have a problem with the original value being in the final output, and I cannot seem to get rid of it. My suspicion is that it is due to strcpy(str1, &final_val); strcat(str2, str1); being…
Florestan Korp
  • 662
  • 2
  • 12
  • 24
2
votes
4 answers

The correct way to use `strcat()` when the destination does not have enough space?

man strcat says the following. char *strcat(char *restrict s1, const char *restrict s2); The string s1 must have sufficient space to hold the result. What is the correct way to handle the situation when s1 does not have enough space?
user1424739
  • 11,937
  • 17
  • 63
  • 152
2
votes
5 answers

Confusion in "strcat function in C assumes the destination string is large enough to hold contents of source string and its own."

So I read that strcat function is to be used carefully as the destination string should be large enough to hold contents of its own and source string. And it was true for the following program that I wrote: #include #include…
2
votes
3 answers

How do strcat() and read() work with '\0' in C

here's my whole code first : 1. #include 2. #include 3. #include 4. #include 5. #include 6. int main(int argc, char *argv[]) { 7. int p[2]; // p[0]: file descriptor for read end of…
2
votes
2 answers

Add one character from a string to the end of another string (strcat)

for (unsigned int i = 0; i < strlen(s); i++) { if (s[i] != ' ') strcat(p, s[i]); I want to add the current character of the s string at the end of the p string provided it is not a space. How do I do that using strcat? The code above gives…
Radu Gabriel
  • 111
  • 4
2
votes
3 answers

Writing concise code in C

strcat(msg, ": "); strcat(msg, buf); Is there a way to do this in one line? I want to make my code cleaner and reduce clutter
2
votes
4 answers

How do I properly work with Strings in C?

The likes of Java, Python, and others have ruined me. I'm trying to automate an FTP client by responding to server codes: For example: // I know this is ugly, please bear with me char username[25]; strcat(username, USER); //"USER " strcat(username,…
Brian D
  • 9,863
  • 18
  • 61
  • 96
2
votes
1 answer

C: "zsh: abort" error

Here's my program: #include char *ft_strcat(char *dest, char *src) { int i; int k; i = 0; k = 0; while (dest[i]) i++; while (src[k]) { dest[i + k] = src[k]; //i++; k++; …
Alexander
  • 109
  • 1
  • 2
  • 14
2
votes
2 answers

How to use strcat() function?

I am very new in C language. I was trying to use strcat function. #include #include int main(int argc, const char *argv[]) { char s1[] = "12345"; char s2[] = "abcde"; strcat(s1, s2); puts(s1); puts(s2); …
zyx
  • 47
  • 1
  • 4
2
votes
3 answers

malloc puts "garbage" values

how can i prevent or bypass the garbage valus malloc puts in my variable? attached the code and the output! thanks! #include #include "stdlib.h" #include int main() { char* hour_char = "13"; char* day_char = "0"; …
JOSI
  • 63
  • 1
  • 5
2
votes
2 answers

Append part of a string to another string

I'm trying to make a simple program where parts of other strings are appended to another string. When I run this code, it doesn't output anything. Sorry, my C knowledge is very low. All help appreciated. #include #include…
SycoSins
  • 39
  • 1
  • 2
  • 8
2
votes
5 answers

Using strcat inside another function: Segmentation fault (core dumped)

static void foo(unsigned char *cmd) { strcat(cmd, "\r\n"); printf("\r\nfoo: #%s#", cmd); } int main() { foo("test"); return 0; } Compiler says Segmentation fault (core dumped) What is the…
HardCoder
  • 249
  • 3
  • 12
2
votes
3 answers

Learning C pointers i cant figure out why this is not working (K&R excercise 5-2)

Ok, so i am actually going through K&R C book (I know it is old and it has a lot of outdated stuff specially on the security side but i am just trying to do the excercises) Ive been playing with excercise 5-2 where i need to implement my own strcat…
jaferic
  • 21
  • 4