473,385 Members | 1,673 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Concat string (not ended ...)

hi

if i need to concat some string, how can i do it?
Generic Code:

char CMD_INIT[]={'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0',
'\2'};
char CMD_HOLD[]={'\142'};
char CMD_TXT_GREEN[]={'\34','\62'};
char MSG[]={'T','e','s','t'};
char CMD_END[]={'\04'};
i would like to do something

CMD_INIT + CMD_HOLD + CMD_TXT_GREEN + MSG + CMD_END

Nov 14 '05 #1
13 2327


collinm wrote:
hi

if i need to concat some string, how can i do it?
Generic Code:

char CMD_INIT[]={'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0',
'\2'};
char CMD_HOLD[]={'\142'};
char CMD_TXT_GREEN[]={'\34','\62'};
char MSG[]={'T','e','s','t'};
char CMD_END[]={'\04'};
i would like to do something

CMD_INIT + CMD_HOLD + CMD_TXT_GREEN + MSG + CMD_END


Nomenclature: A "string" is an array of characters
containing some number of "payload" characters (perhaps
none) followed by a character with the value zero. Only
the first of the five arrays above qualifies as a "string,"
and that string has zero length. (I'm not saying this
merely to be pedantic, but to warn you against trying to
use any of C's string-manipulating functions to operate
on these arrays. They are not strings, and if you feed
them to functions that expect strings all Hell will break
loose.)

If what you want is one array containing all the
characters from the five given arrays, you can do

#include <string.h> /* Yes, I know. Trust me. */

char ALL[sizeof CMD_INIT + sizeof CMD_HOLD
+ sizeof CMD_TXT_GREEN + sizeof MSG
+ sizeof CMD_END];
char *p = ALL;
memcpy (p, CMD_INIT, sizeof CMD_INIT);
p += sizeof CMD_INIT;
memcpy (p, CMD_HOLD, sizeof CMD_HOLD);
p += sizeof CMD_HOLD;
memcpy (p, CMD_TXT_GREEN, sizeof CMD_TXT_GREEN);
p += sizeof CMD_TXT_GREEN;
memcpy (p, MSG, sizeof MSG);
p += sizeof MSG;
memcpy (p, CMD_END, sizeof CMD_END);
p += sizeof CMD_END; /* optional */

(The pointer `p' is just a convenience and could be dispensed
with at the cost of more typing.)

If that's not what you want, you'll need to explain
further.

--
Er*********@sun.com

Nov 14 '05 #2

Eric Sosman wrote:

#include <string.h> /* Yes, I know. Trust me. */

char ALL[sizeof CMD_INIT + sizeof CMD_HOLD
+ sizeof CMD_TXT_GREEN + sizeof MSG
+ sizeof CMD_END];
char *p = ALL;
memcpy (p, CMD_INIT, sizeof CMD_INIT);
p += sizeof CMD_INIT;
memcpy (p, CMD_HOLD, sizeof CMD_HOLD);
p += sizeof CMD_HOLD;
memcpy (p, CMD_TXT_GREEN, sizeof CMD_TXT_GREEN);
p += sizeof CMD_TXT_GREEN;
memcpy (p, MSG, sizeof MSG);
p += sizeof MSG;
memcpy (p, CMD_END, sizeof CMD_END);
p += sizeof CMD_END; /* optional */


i need to add a character, this is my code:

void analyzeFilename(char *filename, int size, char *led_line)
//void analyzeFilename()
{
#ifdef DEBUG
printf("analyzeFilename\n");
#endif

printf("led_line: %s\n",led_line);

char CMD_INIT[]={'\0', '\0', '\0', '\0', '\0', '\1', 'Z', '0', '0',
'\2'};
char CMD_TEXT_FILE_TYPE[]={'A'};
char CMD_FILE_LABEL=filename[0]; //get the first character of
filename
char CMD_HOLD[]={'\142'};
char CMD_TXT_GREEN[]={'\34','\62'};
char CMD_END[]={'\04'};

char ALL[sizeof(CMD_INIT) + sizeof(CMD_TEXT_FILE_TYPE) +
sizeof(CMD_FILE_LABEL) + sizeof(CMD_HOLD) + sizeof(CMD_TXT_GREEN) +
size + sizeof(CMD_END)];

char *p=ALL;

memcpy(p,CMD_INIT, sizeof(CMD_INIT));
p += sizeof(CMD_INIT);
memcpy (p, CMD_TEXT_FILE_TYPE, sizeof(CMD_TEXT_FILE_TYPE));
p += sizeof(CMD_TEXT_FILE_TYPE);
memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL)); //problem here
p += sizeof(CMD_FILE_LABEL);
memcpy (p, CMD_HOLD, sizeof(CMD_HOLD));
p += sizeof(CMD_HOLD);
memcpy (p, CMD_TXT_GREEN, sizeof(CMD_TXT_GREEN));
p += sizeof(CMD_TXT_GREEN);
memcpy (p, filename, sizeof(size));
p += sizeof(size);
memcpy (p, CMD_END, sizeof(CMD_END));
p += sizeof(CMD_END);

printf("msg complet: %s", p);

....
....
}

i got a problem with:

memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

the message here:

warning: passing arg 2 of `memcpy' makes pointer from integer without a
cast

i don't know why i get this error and how to solve it

Nov 14 '05 #3


collinm wrote:
[...]
char CMD_FILE_LABEL=filename[0]; //get the first character of
filename
[...]
i got a problem with:

memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

the message here:

warning: passing arg 2 of `memcpy' makes pointer from integer without a
cast

i don't know why i get this error and how to solve it


This is Question 8.1 in the comp.lang.c Frequently
Asked Questions (FAQ) list

http://www.eskimo.com/~scs/C-faq/top.html

.... and a review of Section 6 would also be a good idea.

--
Er*********@sun.com

Nov 14 '05 #4

ok no error at compile time...
memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

convert to

memcpy (p, &CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

compile without error

but when i display p, i get bullshit...

Expand|Select|Wrap|Line Numbers
  1. printf("msg complet: %s\n", p);
  2.  
give

msg complet: ÿôÿóècÿôÈ

Nov 14 '05 #5
On 25 Mar 2005 12:33:08 -0800, in comp.lang.c , "collinm"
<co*****@laboiteaprog.com> wrote:
but when i display p, i get bullshit...


you snipped the definition of p. Was it
char* p;
or
char p[somesize];

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #6
"collinm" <co*****@laboiteaprog.com> writes:
ok no error at compile time...
memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

convert to

memcpy (p, &CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

compile without error

but when i display p, i get bullshit...

Expand|Select|Wrap|Line Numbers
  1.  printf("msg complet: %s\n", p);
  2.  

give

msg complet: ÿôÿóècÿôÈ


Your definition of CMD_FILE_LABEL was

char CMD_FILE_LABEL=filename[0];

(Incidentally, using all-caps for variable names is bad style;
all-caps is more commonly used for macros.)

So CMD_FILE_LABEL is a *copy* of filename[0], and you can't expect it
to be the start of a valid string.

What you want is the address of filename[0], not a copy of it.

Incidentally, your attempted solution uses sizeof() on fixed-size
arrays of characters. This isn't going to be helpful if you want to
use arrays whose size isn't determined at compilation time. If that's
all you're trying to do, that's fine, but if you need a more general
solution you'll probably need to store the sizes separately.

Also, if you didn't get a compilation error (or at least a warning) on

memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

then you should turn up the warning level on your compiler. For gcc
use something like "gcc -ansi -pedantic -W -Wall"; for other
compilers, consult the documentation.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #7
Groovy hepcat collinm was jivin' on 25 Mar 2005 12:33:08 -0800 in
comp.lang.c.
Re: Concat string (not ended ...)'s a cool scene! Dig it!
ok no error at compile time...
memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

convert to

memcpy (p, &CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

compile without error

but when i display p, i get bullshit...

Expand|Select|Wrap|Line Numbers
  1. printf("msg complet: %s\n", p);

give=20

msg complet: =FF=F4=FF=F3=E8c=FF=F4=C8


That's because you passed p to printf() instead of ALL. Remember,
you're changing the value of p so that it ends up pointing beyond the
end of your array.
But you can't print the array as a string anyhow. As Eric told you,
a string is a sequence of contiguous characters terminated by a null
character. ALL does not contain a string, as such. The null character
('\0') in the first element of ALL effectively makes a string whose
length is zero characters. Thus, if you simply pass ALL to printf(),
it will print nothing. Instead, you must loop through the elements of
ALL and print them one by one. Of course, if you try to print a null
character it will print nothing. So you might want to skip those, or
print something else in their place. You should also not try to print
arbitrary values such as '\1' and '\2' as they might have some meaning
to the system, and may cause strange behaviour. You could do something
like this perhaps:

#include <ctype.h>
....
printf("msg complet: ");

for(p = ALL; p < sizeof ALL; p++)
{
if(isprint((unsigned char)*p))
putchar(*p);
else
putchar('.');
}
putchar('\n');

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #8

Peter "Shaggy" Haywood wrote:
That's because you passed p to printf() instead of ALL. Remember,
you're changing the value of p so that it ends up pointing beyond the
end of your array.
But you can't print the array as a string anyhow. As Eric told you,
a string is a sequence of contiguous characters terminated by a null
character. ALL does not contain a string, as such. The null character
('\0') in the first element of ALL effectively makes a string whose
length is zero characters. Thus, if you simply pass ALL to printf(),
it will print nothing. Instead, you must loop through the elements of
ALL and print them one by one. Of course, if you try to print a null
character it will print nothing. So you might want to skip those, or
print something else in their place. You should also not try to print
arbitrary values such as '\1' and '\2' as they might have some meaning to the system, and may cause strange behaviour. You could do something like this perhaps:

#include <ctype.h>
...
printf("msg complet: ");

for(p = ALL; p < sizeof(ALL); p++)
{
if(isprint((unsigned char)*p))
putchar(*p);
else
putchar('.');
}
putchar('\n');


this code doesn't work, it compile but nothing is displayed

also on the for line, we get this warning: comparison between pointer
and integer...

i changed: p < sizeof(ALL) to p* < sizeof(ALL)

and now we get: ......

Nov 14 '05 #9
Keith Thompson wrote:

Your definition of CMD_FILE_LABEL was

char CMD_FILE_LABEL=filename[0];

(Incidentally, using all-caps for variable names is bad style;
all-caps is more commonly used for macros.)

So CMD_FILE_LABEL is a *copy* of filename[0], and you can't expect it
to be the start of a valid string.

What you want is the address of filename[0], not a copy of it.
you mean:

char CMD_FILE_LABEL=&filename[0];
Incidentally, your attempted solution uses sizeof() on fixed-size
arrays of characters. This isn't going to be helpful if you want to
use arrays whose size isn't determined at compilation time. If that's all you're trying to do, that's fine, but if you need a more general
solution you'll probably need to store the sizes separately.
only ALL is not determined at compilation time
Also, if you didn't get a compilation error (or at least a warning) on
memcpy (p, CMD_FILE_LABEL, sizeof(CMD_FILE_LABEL));

then you should turn up the warning level on your compiler. For gcc
use something like "gcc -ansi -pedantic -W -Wall"; for other
compilers, consult the documentation.


with gcc, i use: CFLAGS= -Os -Wall $(DEFINES)

Nov 14 '05 #10
"collinm" <co*****@laboiteaprog.com> wrote:
Peter "Shaggy" Haywood wrote:
#include <ctype.h>
...
printf("msg complet: ");

for(p = ALL; p < sizeof(ALL); p++)
{
if(isprint((unsigned char)*p))
putchar(*p);
else
putchar('.');
}
putchar('\n');
this code doesn't work, it compile but nothing is displayed


If you've declared your objects right it should work.
also on the for line, we get this warning: comparison between pointer
and integer...
Aha.
- How have you declared p?
- How have you declared ALL?
- Are you sure you're writing C, not C++?
i changed: p < sizeof(ALL) to p* < sizeof(ALL)


That doesn't even compile. Guess why.

Richard
Nov 14 '05 #11
Richard Bos wrote: If you've declared your objects right it should work.
also on the for line, we get this warning: comparison between pointer and integer...


Aha.
- How have you declared p?
- How have you declared ALL?
- Are you sure you're writing C, not C++?


char ALL[sizeof(CMD_INIT) + sizeof(CMD_TEXT_FILE_TYPE) +
sizeof(CMD_FILE_LABEL) + sizeof(CMD_HOLD) + sizeof(CMD_TXT_GREEN) +
size + sizeof(CMD_END)];

char *p=ALL;

we don't have c++ compiler, only c...
i changed: p < sizeof(ALL) to p* < sizeof(ALL)


That doesn't even compile. Guess why.

Richard


Nov 14 '05 #12
On 29 Mar 2005 06:36:05 -0800, "collinm" <co*****@laboiteaprog.com>
wrote:
Keith Thompson wrote:

Your definition of CMD_FILE_LABEL was

char CMD_FILE_LABEL=filename[0];

(Incidentally, using all-caps for variable names is bad style;
all-caps is more commonly used for macros.)

So CMD_FILE_LABEL is a *copy* of filename[0], and you can't expect it
to be the start of a valid string.

What you want is the address of filename[0], not a copy of it.


you mean:

char CMD_FILE_LABEL=&filename[0];


Syntax error. &filename[0] is a value with type pointer to char.
CMD_FILE_LABLE is a variable with type char. You cannot implicitly
convert between the two. Explicit conversion is allowed,
implementation specific, and in this case almost certainly useless.
On most systems, it would also lead to undefined behavior due to the
converted value being too large to fit in a char.


<<Remove the del for email>>
Nov 14 '05 #13
Groovy hepcat collinm was jivin' on 29 Mar 2005 06:17:23 -0800 in
comp.lang.c.
Re: Concat string (not ended ...)'s a cool scene! Dig it!

Peter "Shaggy" Haywood wrote:
That's because you passed p to printf() instead of ALL. Remember,
you're changing the value of p so that it ends up pointing beyond the
end of your array.
But you can't print the array as a string anyhow. As Eric told you,
a string is a sequence of contiguous characters terminated by a null
character. ALL does not contain a string, as such. The null character
('\0') in the first element of ALL effectively makes a string whose
length is zero characters. Thus, if you simply pass ALL to printf(),
it will print nothing. Instead, you must loop through the elements of
ALL and print them one by one. Of course, if you try to print a null
character it will print nothing. So you might want to skip those, or
print something else in their place. You should also not try to print
arbitrary values such as '\1' and '\2' as they might have some

meaning
to the system, and may cause strange behaviour. You could do

something
like this perhaps:

#include <ctype.h>
...
printf("msg complet: ");

for(p = ALL; p < sizeof(ALL); p++)
{
if(isprint((unsigned char)*p))
putchar(*p);
else
putchar('.');
}
putchar('\n');


this code doesn't work, it compile but nothing is displayed

also on the for line, we get this warning: comparison between pointer
and integer...


Whoops! Sorry 'bout that. Wasn't thinking straight. That should be

for(p = ALL; p < (ALL + sizeof ALL); p++)

--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?
Nov 14 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gerald Aichholzer | last post by:
Hello, I need to specify the following attribute in an xhtml-file containing TAL templates: <div tal:attributes="onMouseOver concat('func(',xyz,')')"> which results in <div...
5
by: F. Da Costa | last post by:
Hi, Could it be correct that the following code does *not* work because i'm not using the var arr = new Array("a","b","c"); methodology?? Read through...
3
by: Bryan Valencia | last post by:
Ok, I tried to use the concat function. It's in the Help, but it claims it can't find the namespace that contains 'concat'. You'd think there'd be a note in the help system if I have to use some...
16
by: Jacky | last post by:
Hi, Concat wors as tmpStr = tmpStr.Concat(tmpStr, tmpStr2) Why it do not refer to owner object so tmpStr.Concat(tmpStr, tmpStr2) and now tmpStr has same value as upper?
8
by: Doug Stiers | last post by:
Is there a downside to using string.concat? Other than a little overhead? str1 = string.concat(str1,str2) vs. str1 &= str2 It seems to me like the string class should be optimized to do this...
4
by: Martin Fletcher | last post by:
I cant get the Concat function to work, please help. Here's some of my code. Module modTCP Public TCP_RECEIVED_DATA As String Private WithEvents TCP_SERVER As New WinSockSVR Private Sub...
1
by: Trint Smith | last post by:
Ok, I have a webform that has these checkboxes: 1. something 2. something else 3. and something else When the user clicks on the checkbox, I want all of the selections to go into a textbox...
3
by: Mythran | last post by:
Out of curiosity, only, which is recommended for SHORT concatenation...or concatenating two or three strings that are relatively small in size? Dim a As String = "bah" Dim b As String = "bah2"...
17
by: ramadu | last post by:
I know its a sin to use strings, lets skip that part... Which of these is faster and uses less memory? String.Format("SomeValue='{0}'", m_Value); or String.Concat("SomeValue='", m_Value,...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.