LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-17-2007, 08:23 AM   #1
MattFunke
LQ Newbie
 
Registered: Mar 2006
Distribution: TSLinux 3.0, RedHat Enterprise 4
Posts: 16

Rep: Reputation: 0
Something Loopy in Assigning Strings


I tried this in desperation after strcat() refused to work. I just want to concatenate three strings. Can anyone tell me why this doesn't work? I can see the creation of "10", but not of "11", though there's plenty of room on the disk for both. Moreover, if I ask for a printf of fieldn and strlen(fieldn) for all three fields, I get the right responses. field1[] is more than long enough to handle the job.

Code:
void AssembleData(char field1[], char field2[], char field3[])
{
    int i;
    int startPos;
    int stopPos;

    if (field2[0] != '\0')
    {
        startPos = strlen(field1);
        stopPos = startPos + strlen(field2);

        system("touch /var/log/10\n");

        for (i = startPos; i < stopPos; i++)
        {
            field1[i] = field2[i - startPos];
        }

        system("touch /var/log/11\n");

        if (field3[0] != '\0')
        {
            startPos += strlen(field2);
            stopPos += strlen(field3);

            for (i = startPos; i < stopPos; i++)
            {
                field1[i] = field2[i - startPos];
            }
        }
    }
} 
... and here's the bit I tried with strcat() that didn't work:

Code:
void AssembleData(char field1[], char field2[], char field3[])
{
    system("touch /var/log/10\n");

    field1 = strcat(field1, field2);
    field1 = strcat(field1, field3);

    system("touch /var/log/11\n");
}
Please help. Is there anything about assigning strings or something that I'm missing here?
 
Old 09-17-2007, 10:01 AM   #2
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
of course it works,
you are probably calling it wrong.

NOTE this isn't safe as we should use strncpy strncat to
prevent buffer overflows.

Code:
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
void AssembleData(char field1[], char field2[], char field3[])
{

    field1 = strcat(field1, field2);
    field1 = strcat(field1, field3);


}

int main(int argc, char ** argv)
{

    char buffer[1024];

    strcpy(buffer,  "hello ");
    AssembleData(buffer, "old ", "chap!");
    puts(buffer);

    return 0;
}

billym.primadtpdev>1
hello old chap!
 
Old 09-17-2007, 10:39 AM   #3
MattFunke
LQ Newbie
 
Registered: Mar 2006
Distribution: TSLinux 3.0, RedHat Enterprise 4
Posts: 16

Original Poster
Rep: Reputation: 0
Found it! Here's the problem. This is what I was doing, essentially:

Code:
#define HI_THERE "hi "
#define ID10T "idiot"
char *data;

data = strcat(HI_THERE, ID10T);
... and the statement, of course, after the preprocessor was done with it, looked like this:

Code:
data = strcat("hi ", "idiot");
It was trying to concatenate literals, not string variables. This works:

Code:
char data[80] = HI_THERE;
char data2[80] = ID10T;

data = strcat(data, data2);
<dope slaps self>
 
Old 09-19-2007, 02:26 AM   #4
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
the pitfalls of C
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
loopy skippy sound fixes itself with reboot from win2k randomsel Slackware 3 10-03-2007 06:30 PM
Loopy mouse pointer tamtam Linux - Laptop and Netbook 3 09-15-2006 06:34 AM
how to find duplicate strings in vertical column of strings markhod Programming 7 11-02-2005 04:04 AM
loopy aiptek 6000 tablet netsurf Linux - Hardware 0 04-23-2005 05:58 PM
assigning permissions ziox Programming 1 01-10-2005 09:13 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:02 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration