-2

I have some device i need to connect with by ASCII Commands. i declared some chars array and each command state i assigning it with the request command and then sent it through UART.

Do i have another option to assign the array instead of do it as follows?(for example)

    Buf[0]='M'; 
    Buf[1]='O';
    Buf[2]='D';
    Buf[3]='E';
    Buf[4]='L';
    Buf[5]=' ';
   .
   .
   .

what i thought is to declare char array for each command and each state i will copy the requested command array to the one will be sent to the UART.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
ItamarE
  • 41
  • 4
  • 5
    `char Buf[]="MODEL ";` or `char Buf[SIZE]; strcpy(Buf,"MODEL ");` – Spikatrix Mar 26 '15 at 09:37
  • 1
    `strncpy(Buf, "MODEL ", 6` or `sprintf(Buf, "MODEL ")` – kyriosli Mar 26 '15 at 09:39
  • 2
    @kyriosli Please never use strncpy. It is a very dangerous function since nobody manages to use it correctly. It is **only good for creating bugs**, as in your case. Run this code and you'll see what I mean: `char buf[] = {0, 0, 0, 0, 0, 0, 'b', 'u', 'g'}; strncpy(buf, "MODEL ", 6); puts(buf);` In particular, [strncpy was never intended to be a safer version of strcpy](http://stackoverflow.com/questions/2114896/why-is-strlcpy-and-strlcat-considered-to-be-insecure). – Lundin Mar 26 '15 at 10:46

2 Answers2

4

You didn't specify whether the buffer is '\0'-terminated so it is impossible to give one particular answer.

There are several options:

char Buf[]="MODEL ";

This makes it '\0'-terminated. If you don't want '\0'-termination, you can do:

char Buf[] = {'M','O','D','E','L',' '};

If you want to leave the array uninitialized and populate it later, you could do:

char Buf[SIZE];
strcpy(Buf,"MODEL ");

...but if you do that, please do ensure that SIZE is large enough, otherwise you run into problems.

It's also possible to do:

char Buf[SIZE];
memcpy(Buf,"MODEL ", 6); // or memcpy(Buf, "MODEL ", sizeof("MODEL ")-1);

if you don't want '\0' termination. If you do that, please also ensure that SIZE is large enough. If you use the variant in the comment, please consider also using #define THE_STRING "MODEL " and then using THE_STRING instead of "MODEL " to avoid repeating the string literal twice.

juhist
  • 4,210
  • 16
  • 33
  • 1
    Just a tip: instead of using strlen to know the size of a string literal in runtime, you could do `memcpy(Buf, "MODEL ", sizeof("MODEL ")); // includes null term` or better yet, `#define THE_STRING "MODEL " ... memcpy(Buf, THE_STRING, sizeof(THE_STRING));`. – Lundin Mar 26 '15 at 10:51
  • Yes, I agree that strlen() isn't the most optimal way, so I'll edit my answer. – juhist Mar 26 '15 at 10:54
0

You know you can typedef string to realize what you are doing and it becomes something like java and C#

typedef char* string;
/* The entry point */
int main(int argc,char** argv) {
string str;
str = "Hello, World";
puts(str);// or fputs(str,strlen(str),stdout);
return 0;
}

This does not have any difference with what juhist just said.