LinuxQuestions.org
Did you know LQ has a Linux Hardware Compatibility List?
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 08-10-2006, 12:46 AM   #1
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Rep: Reputation: 15
Angry A question about C concat ||


I have some strings. some are text ABC and some are digits. but I concat a " "||input_value and I get a binary number back the input_value is a string "1" but when I try to strcpy(V0 , ((char *)" "||(char *)input_value) i get back a binary 1 not a string. obviously I am doing something wrong. but all I want to to is paste a blank on a general string and put the pointer in a working
string.

josephus


It is not what you know that gets you in dutch.
It's what you know that aint so.
-- josh billings
 
Old 08-11-2006, 11:50 AM   #2
timmeke
Senior Member
 
Registered: Nov 2005
Location: Belgium
Distribution: Red Hat, Fedora
Posts: 1,515

Rep: Reputation: 61
In C, you can't concatenate using '||'. '||' does a logical OR operation (not a bitwise one). Hence, it's result is a logical boolean (0 or 1).

Try using the 'strcat' function instead.

PS. I'm just curious: where did you get the idea to use '||' for concatenation? I've seen quite a few programming languages, which support string concatenation in different ways (ie 'strcat', simple append, '+' or '.' operators, ...), but I haven't seen any language that uses '||' for that (yet).

Last edited by timmeke; 08-11-2006 at 11:52 AM.
 
Old 08-11-2006, 11:51 AM   #3
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,455

Rep: Reputation: 83
I cannot quite figure out what and where your problem is, however

1. "||" is not an operator for concat.
2. all stored data are binary. YOU decide whether these are "char", "int", "long", "double", "float", "pointer" or whatever....
 
Old 08-11-2006, 11:45 PM   #4
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
Sorry I write about a dozen computer languages and often confuse tokens. But when I paste up (char *)( char *)" "||"f1") I get back a binary '\1'. I do not get an error.

I changed to

V0=strcat((char *) minus , (char *)input_value);

now I get segfault in strcat.

(char *)minus = " "
(char *)input_value = "1".

all I want to do is paste the two strings together so I can write it to disk.
 
Old 08-12-2006, 09:07 AM   #5
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,455

Rep: Reputation: 83
Code:
NAME
     strcat - concatenate strings

LIBRARY
     Standard C Library (libc, -lc)

SYNOPSIS
     #include <string.h>

     char *
     strcat(char * restrict s, const char * restrict append);

     char *
     strncat(char * restrict s, const char * restrict append, size_t count);

DESCRIPTION
     The strcat() and strncat() functions append a copy of the null-terminated
     string append to the end of the null-terminated string s, then add a ter-
     minating `\0'.  The string s must have sufficient space to hold the
     result.
You do not have enough space (or to be precise, you have nothing you can modify). Type of string "thisIsString" is const char*. refer my previous post at http://www.linuxquestions.org/questi...d.php?t=439038
 
Old 08-14-2006, 02:32 AM   #6
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
Angry a problem with strcat.

I am trying to concat " "and some variable or integer. All of these are strings.

I found if I dont want warnings that



gcc thinks this is cool.

V0 = strcat(input1,input2);

but at runtime I get segfault


ok
I try this.

(char *)V0 = (char *)strcat(&inpu1,&input2);


this will execute and not segfault. but the character string is garbage.

if I say strcat(" ","1") it returns " 1"
if I say
char * termp1;
temp1 = malloc(32);
temp1 = " ";
temp2 = "1";

now I get segfualts with out warnings and now I get garbage if I use &temp1 it executes but is wrong text.

I do not know how to fix this error.
 
Old 08-14-2006, 03:52 AM   #7
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by dogbird
Code:
       char * termp1;
       temp1 = malloc(32);
       temp1 = " ";
       temp2 = "1";
I do not know how to fix this error.
In C, temp1=" " does not work for what you want it to do. temp1 is a pointer and you will give it the address of the string containing the space. Use:
Code:
strcpy(temp1," ");
Quote:
Originally Posted by dogbird
Code:
strcat(" ","1")
looks dangerous to me. You pass a pointer to a fixed-size string and next change the contents to a longer string.


BTW, this fits better in programming. You can ask a moderator to move it by reporting the post.

Last edited by Wim Sturkenboom; 08-14-2006 at 03:59 AM.
 
Old 08-14-2006, 06:04 AM   #8
reddazz
Guru
 
Registered: Nov 2003
Location: N. E. England
Distribution: Fedora, CentOS, Debian
Posts: 16,298

Rep: Reputation: 73
Moved to the programming section as requested.
 
Old 08-14-2006, 11:11 AM   #9
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
as Wim Sturkenboom was saying, you cannot modify a string literal in c, they are constant. if you modify a string literal you will get undefined behavior as a result.
this is an example of a literal.
char* my_string = "this is a literal";

the easiest way to do what you want to do is to make a char array locally on the stack or alloc memory.. here is a simple example using a local string buffer..

Code:
char buffer[16];
bzero(buffer, sizeof(buffer));

char* temp1 = "1";
char* temp2 = " ";

strcat(buffer, temp1);
strcat(buffer, temp2);
printf("result is %s\n", buffer);
 
Old 08-14-2006, 06:24 PM   #10
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
I am not using constants. I read in values and put them in Char * variables. I malloc the damn thins. and I expect to concat them to gether.


my text actually looks like this
char * input_value;
char * minus;
/*
input_value == '1'
minus = ' '
*/

both input_value and minus are read from the file into variables.


then at some time later



(char *) V0 = strcat(minus,input_value);
/* this statement will segfault.

debugger says minus = " "
input_value = "1"

(char *) V0 = (char *)strcat(&minus,&input_value);

this GCC generates warnings.

but this works and does not segfault
Unfortunately it is WRONG. the text is garbage.

I do not know how to fix or just make this work.
 
Old 08-14-2006, 07:16 PM   #11
kaz2100
Senior Member
 
Registered: Apr 2005
Location: Penguin land, with apple, no gates
Distribution: Debian testing woody(32) sarge etch lenny squeeze(+64) wheezy jessie
Posts: 1,455

Rep: Reputation: 83
Can you post EXACT source which you get segmentation fault? (not digest please)
 
Old 08-14-2006, 08:19 PM   #12
xhi
Senior Member
 
Registered: Mar 2005
Location: USA::Pennsylvania
Distribution: Slackware
Posts: 1,065

Rep: Reputation: 45
> I malloc the damn thins. and I expect to concat them to gether.

well maybe you are not malloc'n the right "damn thins".

is the dest string big enough to hold the concatenation?

why not post some code, strcat does work, you obviously are not doing something right. code will solve this in one post instead of everyone trying to guess what you are doing.
 
Old 08-15-2006, 03:15 AM   #13
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
As said, please post your (relevant) code as I don't see where you malloc (and please put it in code-tags so it's easily recognizable and readable).

Code:
int main (int argc, char *argv[])
{
char buf[100];

    strcpy(buf,argv[1]);
    strcat(buf,argv[2]);

    printf("buf = '%s'\n"buf);
    return 0;
}
PS 1
One should use strncpy and strncat or test that no buffer overflow will occur, but that was to much work for me so early in the morning.
PS 2
Code not tested
 
Old 08-15-2006, 04:18 AM   #14
dogbird
Member
 
Registered: Nov 2004
Location: oklahoma
Distribution: slackware 9.0
Posts: 98

Original Poster
Rep: Reputation: 15
ok I am writing generally generic c


i have dozens of variables and I spend a lot of time trying to get straight with GCC.

the code is a data base and reads 3 different files. the

char * minus;
char * input_value;
char * V0;
V0 = malloc(80);
(char *) input_value = (char *)malloc(80);
for (i =1;i < 564; i++) ; {
(char *)input[i] = malloc[80};
k = yylex();
strcpy(input[i],yytext);;
};

for { i = 1; i< 563: i++); {
(char *)minus = malloc(80);
if ( minus <> " " ) strcpy(minus"-";
input_value = input[i];
(char *) V0= (char *)strcat( minus,input_value);
:



I left out all the unrelated code. strcat should accept any two strings and store the result in a third string. it should not segfault.
 
Old 08-15-2006, 06:24 AM   #15
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Sorry but code does not compile at all.
variable input, i, k are not declared, braces missing etc etc
input is probably char * input[564] and i and k are probably ints

What is yylex? Can't find a man page for it, so is it your own?

Next some errors (I suppose)
Code:
 for (i =1;i < 564; i++) ; {
(char *)input[i] = malloc[80};
This is a loop that does 564 times nothing. Next it does one malloc (for input[563]). Same for the next for loop that you showed.

Other things:
Code:
(char *) input_value = (char *)malloc(80);
Why the first 'cast'?


Code:
input_value = input[i];
If that is how you use it, you do not need to allocate memory for input_value first (and you should not). You simply copy a pointer.


Code:
if ( minus <> " " )
is absolutely no C code. What do you want to do?


Code:
 (char *) V0= (char *)strcat( minus,input_value);
Again you don't have to allocate memory for V0 (and you should not).


Code:
for { i = 1; i< 563: i++); {
    minus = (char *)malloc(80);
It's useless to allocate 564 times on the same variable.


Quote:
strcat should accept any two strings and store the result in a third string.
No, strcat adds the second string to the first one (so the first one gets modified)

Code:
int main(int argc, char *argv[])
{
char *minus;
char *input_value;
char *V0;
char *input[564];

int i,k;

    for (i =1;i < 564; i++) {
        input[i] = malloc(80);
        if(input[i]==NULL)
        {
            printf("malloc error for input %d\n",i);
            // exit here
        }
//    k = yylex();
        strcpy(input[i],"yytext"); // as yytext was not declared, I made it a string
    }

    minus = (char *)malloc(80);
    if(minus==NULL)
    {
        printf("malloc error for minus %d\n",i);
        // exit here
    }
    for ( i = 1; i< 563; i++); {
        if( minus <> " " )    // do not know what you want to do, so left it in
                              // further it is not initialized yet
            strcpy(minus,"-");
        input_value = input[i];
        V0=strcat(minus,input_value);
    }

    return 0;

}
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
Question, Apples Contribution to Open Source + MacOs file structure question Higgy3k Other *NIX 5 07-25-2005 05:23 AM
Concat string (not ended ...) os2 Programming 2 03-25-2005 04:23 PM
Not your regular GRUB question - just a short question for a fried MBR!! ziphem Linux - General 3 01-31-2005 02:51 PM
login prompt question & kde scheme question JustinCoyan Slackware 2 06-09-2004 03:02 PM
RE: Suse 8.0 hardware question {newbie question, pls help} Radiouk Linux - Distributions 2 06-04-2002 01:53 PM


All times are GMT -5. The time now is 11:43 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration