LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Concat string (not ended ...) (https://www.linuxquestions.org/questions/programming-9/concat-string-not-ended-306015/)

os2 03-25-2005 02:44 PM

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?

itsme86 03-25-2005 03:10 PM

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.

TheLinuxDuck 03-25-2005 03:23 PM

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.


All times are GMT -5. The time now is 01:44 PM.