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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
03-25-2005, 02:44 PM
|
#1
|
|
Member
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206
Rep:
|
Concat string (not ended ...)
hi
i tried to concat some string (not ended, can use strcpy, strcat...)
Code:
void analyzeFilename(char *filename, int size, char *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));
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);
...
...
}
ok no error at compile time...
but when i display p, i get bullshit...
Code:
printf("msg complet: %s\n", p);
return me
Code:
msg complet: ÿôÿóècÿôÈ
any idea?
|
|
|
|
03-25-2005, 03:10 PM
|
#2
|
|
Senior Member
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246
Rep:
|
You should check to see where p is pointing after all those memcpy()'s. It's pointing to memory right after the string. Maybe you should be printing ALL instead? Also, CMD_END isn't nul-terminated so you shouldn't be trying to print it anyway. Besides, CMD_INIT starts with a '\0' so it will end up printing nothing since it thinks ALL is an empty string.
|
|
|
|
03-25-2005, 03:23 PM
|
#3
|
|
Member
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349
Rep:
|
itsme is correct in that you should be printing from ALL, not p. The issue is that the code is increasing the memory location of p each time it executes:
p now no longer points to ALL, but ALL + sizeof(???);
I don't know if that's what you meant to do.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 01:54 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|