ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
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).
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....
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.
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.
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..
> 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.
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
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;
}
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.