strcat in c program is not working
Asked Answered
M

6

6
#include<string.h>
#include<stdio.h>
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 str1[]="hello";
char str2[]="world";

then it is working!

But why

char *str1="hello";
char *str2="world";

this code is not working????

Manheim answered 26/8, 2014 at 9:22 Comment(2)
possible duplicate of Char array vs Char Pointer in CSanasanabria
possible duplicate of strcat() implementation works but causes a core dump at the endSis
B
7

You are learning from a bad book. The main function should be declared as

int main (void);

Declaring it as void invokes undefined behaviour when the application finishes. Well, it doesn't finish yet, but eventually it will.

Get a book about the C language. You would find that

char *srt1="hello";

is compiled as if you wrote

static const char secret_array [6] = { 'h', 'e', 'l', 'l', 'o', 0 };
char* srt1 = (char*) &secret_array [0];

while

char srt1[]="hello";

is compiled as if you wrote

char srt1 [6] = { 'h', 'e', 'l', 'l', 'o', 0 };

Both strcat calls are severe bugs, because the destination of the strcat call doesn't have enough memory to contain the result. The first call is also a bug because you try to modify constant memory. In the first case, the bug leads to a crash, which is a good thing and lucky for you. In the second case the bug isn't immediately detected. Which is bad luck. You can bet that if you use code like this in a program that is shipped to a customer, it will crash if you are lucky, and lead to incorrect results that will cost your customer lots of money and get you sued otherwise.

Blackberry answered 26/8, 2014 at 9:32 Comment(0)
C
2

In your code.

char *srt1="hello";

you created a pointer and pointed it at a constant string. The compiler puts that in a part of memory that is marked as read-only.

So strcat will try to modifying that which cause undefined behaviour.

it has no name and has static storage duration (meaning that it lives for the entire life of the program); and a variable of type pointer-to-char, called p, which is initialized with the location of the first character in that unnamed, read-only array.

see my answer for proper understanding why it working for first and not for second case.

Cata answered 26/8, 2014 at 9:28 Comment(0)
D
1
char *srt1="hello";
char *srt2="world";

when you declare string like this, it is stored in Read-only Memory!

You can't change the variables stored in Read-only memory!

When you do strcat it is trying to modify the string, which is present in read only memory. So it is not allowed here! It is "Undefined".

Democracy answered 26/8, 2014 at 9:24 Comment(1)
@Sanasanabria where the actual program code gets stored!Democracy
T
1

String literals are read-only, you cannot change them.

This:

char *srt2="world";

means srt2 (bad name, btw) is a pointer variable, pointing at memory containing the constant data "world" (and a terminating '\0' character). There's no additional room after the six characters, and you cannot even change the letters.

You need:

char str2[32] = "world";

This, on the other hand, makes str2 be an array of 32 characters, where the first 6 characters are initialized to "world" and a terminating '\0'. It's fine to append to this, since new characters can fit into the existing array, as long as you don't overstep and try to store more than 32 characters (including the terminator).

Tagmeme answered 26/8, 2014 at 9:29 Comment(0)
Y
0

In your program, str1 and str2 are not supposed to be written to because they're declared as pointer to read-only memory. To see this, do 'objdump -s a.out' and you'll see the following:

Contents of section .rodata:
400640 01000200 68656c6c 6f00776f 726c6400  ....hello.world.
400650 257300                               %s.    

strcat tries to write to that part of memory, thus causing a segmentation fault.

Yuhas answered 26/8, 2014 at 9:35 Comment(0)
J
0

You can also use an Array. Here is a simple program of mine where I faced a problem like this, but in this problem you have to declare the size of array. Like, mahe = [10].

#include <stdio.h>
#include <stdlib.h>

int main()
{
    char cname[10] = "mahe";
    strcat(cname, "Karim");
    printf("%s\n", cname);
    return 0;
}
Joachima answered 25/3, 2017 at 5:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.