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
3
votes
3 answers

Is there anyway to check the success of the str functions?

I was looking through the manuals on strcpy() and strcat(). Seems there's no way to evaluate the "success" of the function call. (ie return value will never be NULL), is that correct? It's just assumed that if you follow the rules for the input of…
Mike
  • 47,263
  • 29
  • 113
  • 177
2
votes
4 answers

c strcat with pointer

I'm trying to use pointers and strcat from C. This is part of my learning process. The idea is the user inputs a string that contains digits and the output should return only the digits. So, if the user inputs te12abc the output should be 12. This…
Favolas
  • 6,963
  • 29
  • 75
  • 127
2
votes
2 answers

Including a string into another string in C

How do I "include" a string into another string in C ? Here is an example : string1 = "www.google"; string2 = "http://"+string1+".com"; I'm having difficulties with strcat(). Thanks
kyori
  • 487
  • 1
  • 7
  • 21
2
votes
4 answers

Function to concat two char* in C++

First off, I'm a C# programmer, so my working knowledge of C++ is fairly limited. I took it back in college, but haven't touched it in 10 years, so please forgive me if this is relatively simple stuff. I'm attempting to make a DLL that I can use in…
Trevor Watson
  • 415
  • 1
  • 8
  • 20
2
votes
2 answers

Why does referencing this char array cause Stack smashing, using C?

The program takes a pointer to a char array and an int. The char array consists of two numbers, separated by a space. The use of the function is to read the values of the char array as integers and replace them with the multiplied value of the…
HeapUnderStop
  • 378
  • 1
  • 9
2
votes
2 answers

How do I concat a character into a string?

I'm trying to take a char and add it to a string. OR if that's not possible, add it to an array of characters? I tried changed 'string updated = ""' to 'char updated[text_length]' but I was getting the same error - incompatible integer to pointer…
matt.mcg
  • 419
  • 5
  • 10
2
votes
5 answers

C memory question

char buffer[10]; strcat(buffer, "hi"); printf("%s", buffer); In the above code, it prints some weird symbol or number followed by the "hi", I know strcat is appending to buffer. And I normally zero the memory in buffer. But i'm curious why I…
Josh
  • 6,046
  • 11
  • 52
  • 83
2
votes
1 answer

Segmentation fault when trying to concatenate an element of a 2d array

I wanted to use strcat() to concatenate an element of an array of strings. I tried: #include #include #include int main() { char **str = malloc(sizeof(char *) * 3); for (int i = 0; i < 3; i++) { …
2
votes
2 answers

Why I am getting wrong output for source string when I am implementing strcat()?

Here I tried implementing strcat function in C. Problem is that when I am printing the source string it is showing me "hyam" not "shyam" #include #include char *fun(char *target, const char *source) { printf("%s\n",…
2
votes
9 answers

Writing more characters than malloced. Why does it not fail?

Why does the following work and not throw some kind of segmentation fault? char *path = "/usr/bin/"; char *random = "012"; // path + random + \0 // so its malloc(13), but I get 16 bytes due to memory alignment (im on 32bit) newPath = (char *)…
2
votes
2 answers

strange return behavior in strcat()

void mystrcat(char* to, const char* from) { while (*to) to++; while (*from) *to++ = *from++; *to = '\0'; } int main() { char addthis[]= "rest of the sentence"; char start_of[] = "going to add "; mystrcat(start_of,…
EagleEye
  • 33
  • 5
2
votes
3 answers

How can I copy multiple char* strings into one using strcpy_s()?

Using the strcpy_s() function I want to collate the first three strings into the final one to print my full name. This is what I have and it doesn't work as I'm using char* strings and not std::strings. #include using namespace std; int…
2
votes
2 answers

String concats onto another without an assignment, why is this?

Below is a function from a program: //read the specified file and check for the input ssn int readfile(FILE *fptr, PERSON **rptr){ int v=0, i, j; char n2[MAXS+1], b[1]=" "; for(i=0; i
Kabir
  • 31
  • 1
  • 5
2
votes
2 answers

How to concatenate string and int in C without using sprintf?

I am writing the following code for an embedded system, where I am putting up strings and integers into a buffer. There are several sprintfs in the code similar to the one below (with more than one strings and integers). sprintf(txbuf,…
Annie
  • 21
  • 5
2
votes
6 answers

Appending character arrays using strcat does not work

Can some one tell me what's wrong with this code??? char sms[] = "gr8"; strcat (sms, " & :)");
anonomys
  • 29
  • 2