-2

It prints nothing.I think the problem is at the type char * but i dont know if and why.

int main() {
    char *a = "0123456789" ;
    char *b = "abcdefghij" ;
    strcat (a,b) ;
    printf( "%s", a ) ;
}
Loga
  • 39
  • 5

2 Answers2

1

strcat modifies the destination parameter buffer, but a's buffer is in read-only memory as it's a string literal.

The solution is to create a buffer at runtime and use that as the buffer. You'll need to copy a into it first, of course.

char* a = "1234567890";
char* b = "abcdefghij";

size_t aLen = strlen( a );

char* combined = malloc( sizeof(char) * ( strlen(a) + strlen(b) + 1 ) );
strcpy_s( combined, aLen, a ); // always use _s versions of string/buffer functions!
strcat( combined, b );

puts( combined );

free( combined );

Alternatively in C99 we could just use a stack-allocated array that's preinitialized (and preallocated to be large enough). Stack arrays, including pre-initialized ones, can have their buffer written to:

char a[100] = "1234567890";
strcat( a, "abcdefghij" );
puts( a );
Dai
  • 141,631
  • 28
  • 261
  • 374
  • It's not really necessary to initialize the memory to zero if you are immediately going to overwrite it. Also not a fan of consecutive `strcat` calls as it is the textbook example of a Shlemiel The Painter's algorithm. – Christian Gibbons Jun 04 '18 at 22:07
  • `char combined` -> `char *combined` – chqrlie Jun 04 '18 at 22:10
  • 1
    Is it in read-only memory? Are you sure the implementation has that? If you base your answer on a comment, either incorporate it into the question and / or at least point that implementation-dependency out in the answer. Why `calloc()` instead of `malloc()`, or even better an automatic array? – Deduplicator Jun 04 '18 at 22:16
  • While the OP did mention they are using Windows, I would generally shy away from windows-specific libraries when avoidable. In this case, since dealing with string literals, they are guaranteed to be null-terminated, so you can safely use the standard `strcpy`. – Christian Gibbons Jun 04 '18 at 22:26
  • @ChristianGibbons I've added a 3-line answer that uses a preinitialized stack array. – Dai Jun 04 '18 at 22:27
  • @ChristianGibbons I didn't use any Windows-specific functions. `strcat` and `calloc` are both in the standard library and "Portable C" conventions dictate string literals are read-only. – Dai Jun 04 '18 at 22:29
  • @Dai the windows-specific function was was referring to was `strcpy_s`. Unless I am mistaken, that is a microsoft function for safer string manipulation. – Christian Gibbons Jun 04 '18 at 22:30
  • @ChristianGibbons The `_s` functions were added to C11. I think they started-out as Microsoft extensions around 2003-2005? But they're standard now. – Dai Jun 04 '18 at 22:34
  • Oh really? Good to know. – Christian Gibbons Jun 04 '18 at 22:35
  • 1
    @ChristianGibbons The `_s`-functions are in the optional and oft-rejected Annex K of C11. At least an incompatible variant of them. Consider browsing [tag:tr24731]. – Deduplicator Jun 04 '18 at 23:41
-1

You cant cat to string literal as dest. Use an array as dest buffer

char str[30] = "Ala ";
const char *strFrom = "ma kota";
strcat (str, strFrom);

edit: extending the answer

Take a look how string literals get compiled: https://godbolt.org/g/hZuR1o

Array buffer is defined inline and it's value is constructed during execution and is located on the stack. Thus the buffer itself can be freely modified.

If you allocated it dynamically (with malloc or calloc), you'd get a pointer to the buffer on the heap which could be modified as well.

String literals on the other hand land in read-only memory regions and are shared across various instances since they're read-only anyway. That's how it's designed, man:)

jedzej
  • 422
  • 3
  • 12