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.

465 questions
41
votes
8 answers

error: function returns address of local variable

I'm beginner with C and I am learning on my own. I am creating the following function: char *foo(int x){ if(x < 0){ char a[1000]; char b = "blah"; x = x - 1; char *c = foo(x); strcpy(a, b); …
Hello World
  • 1,123
  • 2
  • 9
  • 8
30
votes
5 answers

Matlab strcat function troubles with spaces

I'm trying to accomplish this: strcat('red ', 'yellow ', 'white ') I expected to see "red yellow white", however, I see "redyellowwhite" on the command output. What needs to be done to ensure the spaces are concatenated properly? Thanks in advance.
stanigator
  • 10,102
  • 32
  • 88
  • 126
17
votes
3 answers

realloc(): invalid next size when reallocating to make space for strcat on char *

I am getting invalid memory error on following code: printf(" %s\n","FINE 5"); printf("%s LENGTH IS: %d\n","FINE 6",strlen(": ")); buffer = (char *)realloc(buffer, strlen(buffer)* sizeof(char) + (strlen(": ")+1)* sizeof(char)); printf(" %s\n","FINE…
PUG
  • 3,867
  • 11
  • 69
  • 106
17
votes
8 answers

How to find the length of argv[] in C

#include #include #include int main(int argc, char *argv[]){ int fir; //badly named loop variable char *input[] = calloc( strlen(argv), sizeof(char)); //initializing an array for( fir = 1; fir<…
user1787351
  • 183
  • 1
  • 1
  • 5
12
votes
7 answers

strcat concat a char onto a string?

Using GDB, I find I get a segmentation fault when I attempt this operation: strcat(string,¤tChar); Given that string is initialized as char * string = ""; and currentChar is char currentChar = 'B'; Why does this result in a segmentation…
Blackbinary
  • 3,674
  • 15
  • 45
  • 61
11
votes
3 answers

How to generate a constexpr version string from three integers (or perhaps a git/SVN commit/rev. string)?

Say I have constexpr const std::uint8_t major = 1; constexpr const std::uint8_t minor = 10; constexpr const std::uint8_t bugfix = 0; and I want constexpr const char* version_string(){ ... } to return the equivalent of "1.10.0" in this example, how…
rubenvb
  • 69,525
  • 30
  • 173
  • 306
7
votes
3 answers

Why is “strcat” considered as “unsafe”?

Possible Duplicate: Why does MSVC++ consider “std::strcat” to be “unsafe”? (C++) Here is my code: char sentence[ 100 ] = ""; char *article[ 5 ] = { "the", "a", "one", "some", "any" }; lexeme = rand() % 4; // random lexeme strcat( sentence,…
Alex
  • 133
  • 2
  • 3
  • 5
7
votes
2 answers

Does strncat() always null terminate?

Considering this code: limit = sizeof(str1)-strlen(str1)-1; strncat(str1,str2,limit); If str2 length is greater than limit, does strncat Nul terminates str1 or I have to add this code, like in the case of strncpy? str1[sizeof(str1)-1] = '\0'
Demetrio
  • 457
  • 1
  • 6
  • 18
7
votes
5 answers

strcat implementation

I tried to implement the strcat by myself, and I found the strcat implementation from Wiki like this......but when I use it, there is segmentation fault. What's wrong with the code below? char * strcat(char *dest, const char *src) { size_t i,j; …
skydoor
  • 23,142
  • 48
  • 138
  • 193
6
votes
7 answers

Why does MSVC++ consider "std::strcat" to be "unsafe"? (C++)

When I try to do things like this: char* prefix = "Sector_Data\\sector"; char* s_num = "0"; std::strcat(prefix, s_num); std::strcat(prefix, "\\"); and so on and so forth, I get a warning warning C4996: 'strcat': This function or variable may be…
user98188
6
votes
5 answers

Size of strcat Destination Array

Take the following program: #include #include using namespace std; int main() { char a[8] = "Hello, "; char b[7] = "world!"; strcat(a, b); cout << a; return 0; } Notice that a and b have the same size…
wjm
  • 2,403
  • 2
  • 21
  • 31
5
votes
6 answers

strcat in c program is not working

#include #include void main() { char *str1="hello"; char *str2="world"; strcat(str2,str1); printf("%s",str2); } If I run this program, I'm getting run time program termination. Please help me. If I use this: char…
user3815049
5
votes
4 answers

C char to string (passing char to strcat())

my problem is in convert a char to string i have to pass to strcat() a char to append to a string, how can i do? thanks! #include #include char *asd(char* in, char *out){ while(*in){ strcat(out, *in); // <-- err arg…
frx08
  • 3,738
  • 7
  • 34
  • 44
5
votes
3 answers

Convert integer to be used in strcat

I'm trying to open different files by having a for loop increment a counter, then appending that counter to the filename to be opened, but I'm stuck on how to use strcat to do this. If I understand right, strcat takes 2 strings, but my counter is an…
Gary M
  • 157
  • 2
  • 2
  • 8
4
votes
5 answers

Matlab character to string convertion problem. What function to use?

x = 1234 56789 7654 x(1) is 1, x(2) is 2 and so on... there are 5 spaces in between.. size(x) = 1 23 One row with 23 columns I've tried using num2str, strcat but I cannot club the numbers. y = num2str(x), y = strcat(x) I want it to be..…
emil
  • 105
  • 3
  • 8
1
2 3
30 31