Welcome to the most active Linux Forum on the web.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 03-25-2005, 03:44 PM   #1
Registered: Dec 2003
Location: Canada
Distribution: Suse 10
Posts: 206

Rep: Reputation: 30
Concat string (not ended ...)


i tried to concat some string (not ended, can use strcpy, strcat...)

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...

printf("msg complet: %s\n", p);
return me

msg complet: c
any idea?
Old 03-25-2005, 04:10 PM   #2
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
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:
p += sizeof(???);
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
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 02:43 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration