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
56
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,273
  • 2
  • 10
  • 8
32
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,768
  • 34
  • 94
  • 129
21
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
  • 223
  • 1
  • 2
  • 5
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
  • 4,301
  • 13
  • 73
  • 115
14
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,936
  • 18
  • 49
  • 62
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
  • 74,642
  • 33
  • 187
  • 332
9
votes
5 answers

Appending element into an array of strings in C

I have an array of strings with a given size, without using any memory allocation, how do I append something into it? Say I run the code, its waiting for something you want to enter, you enter "bond", how do I append this into an array ? A[10] ?
dave_1234
  • 147
  • 1
  • 3
  • 10
8
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
  • 153
  • 2
  • 3
  • 5
8
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
  • 25,218
  • 52
  • 147
  • 201
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'
dems98
  • 814
  • 3
  • 9
  • 22
7
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…
wjmolina
  • 2,625
  • 2
  • 26
  • 35
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
2 answers

Use strcat function with char pointer

I want to print "Hello - World" by using two char pointers but I have a "Segmentation fault (core dumped)" problem. #include #include #include #define Hyphen " - " int main() { char* x="Hello"; char*…
Ran Marashi
  • 71
  • 1
  • 3
6
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
6
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
  • 167
  • 2
  • 2
  • 9
1
2 3
35 36