LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 03-25-2005, 03:44 PM   #1
os2
Member
 
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206

Rep: Reputation: 30
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?
 
Old 03-25-2005, 04:10 PM   #2
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 58
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.
 
Old 03-25-2005, 04:23 PM   #3
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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:
Code:
p += sizeof(???);
p now no longer points to ALL, but ALL + sizeof(???);

I don't know if that's what you meant to do.
 
  


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
MySQL ended... wtf? :( ilhbutshm Linux - Software 5 12-09-2004 08:48 AM
java test if string in string array is null. exodist Programming 3 02-21-2004 02:39 PM
mysqld ended message on Slack... Vincent_Vega Slackware 1 12-20-2003 05:04 AM
Mysql ended BlackRabbit Linux - Software 2 10-25-2003 10:35 AM
mysqld ended.. xdrainox Slackware 9 10-02-2003 05:26 PM


All times are GMT -5. The time now is 09:32 PM.

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