Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 136,581 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 1,932 people online right now. Registration is fast and FREE... Join Now!




problem concatenating strings in C

 
Reply to this topicStart new topic

problem concatenating strings in C, strcat()

vangogh
27 Sep, 2008 - 07:16 AM
Post #1

New D.I.C Head
*

Joined: 30 May, 2008
Posts: 22

Hi, I am writing a program and I need to concatenate different strings. I tried using strcat() and strncat() (although I'd prefer to just use strcat() if possible) but couldn't get them to work (there was a segfault). I read the man pages and it looks like I am doing everything right. Below is my code. If anyone could help I'd appreciate it.

CODE

#include <string.h>
#include <stdio.h>

int main(int argc, char **args)
{
    char *str1 = "this";
    const char *str2 = "world";

    printf("%s\n", str1);
    printf("%s\n", str2);

    strcat(str1, str2);    //segfault occurs here

    printf("%s\n", str1);
    printf("%s\n", str2);

    return 0;
}

User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: Problem Concatenating Strings In C
27 Sep, 2008 - 08:13 AM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
Here's an example on how strcat works:

cpp

#include <stdio.h>
#include <string.h>

int main ()
{
printf("Please enter your name: ");
scanf("%s", name);
title = strcat(name, " the Magnificent");
printf("Hello, %s\n", title);
return 0;
}

User is offlineProfile CardPM
+Quote Post

JackOfAllTrades
RE: Problem Concatenating Strings In C
27 Sep, 2008 - 08:25 AM
Post #3

Cantankerous Old Fart
Group Icon

Joined: 23 Aug, 2008
Posts: 580



Thanked: 59 times
Dream Kudos: 50
My Contributions
There are two problems here.

First, when you create a string like this
cpp
char *str1 = "this";


you are actually creating an array of 5 characters ('t', 'h', 'i', 's', '\0') in read-only memory. Any attempt to modify this string will result in a segfault as you've seen. The proper way to declare this string is to declare it as you have the world variable, which is as a constant. That way, any attempts to change it will be caught by the compiler.

If you need to modify this string, then you would declare it thusly:
cpp
char [] str1 = "this";
which ends up being created in memory you *can* modify, but there's a caveat. You can only modify the four non-null characters in the string. If you concatenate onto this string, you may be writing over some other variable's memory and all hell could break loose down the line.

You should create a character buffer that contains enough space to accept both strings, copy the first one into it, then concatenate the second one to that:
cpp

const char *str1 = "this";
const char *str2 = "world";
char buffer[256];

strcpy(buffer, str1);
strcat(buffer, str2);

printf("Buffer contents: %s\n", buffer);


User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 12/3/08 12:29AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month