how to use memset to fill a char array with space key?
-
Hi, I want to fill out a char array with space. like:
memset(msgArray, '*', 100);
but '*' should be a Space key.
memset(msgArray, ' ', 100); memset(msgArray, #32, 100); or a myriad of others
In vino veritas
-
memset(msgArray, ' ', 100); memset(msgArray, #32, 100); or a myriad of others
In vino veritas
-
Hi, I want to fill out a char array with space. like:
memset(msgArray, '*', 100);
but '*' should be a Space key.
-
I find this question a bit mysterious, if you had tried the obvious thing it just should have worked. The "obvious thing" is:
focusdoit wrote:
'*' should be a Space key.
.. literally just replace '*' by ' '.
in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:
memset (msgArray, ' ', 100);
snprintf (msgArray, 2, "%d", 1);
snprintf (messageArray+10, 33, "%s", "main");I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..
-
in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:
memset (msgArray, ' ', 100);
snprintf (msgArray, 2, "%d", 1);
snprintf (messageArray+10, 33, "%s", "main");I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..
-
in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:
memset (msgArray, ' ', 100);
snprintf (msgArray, 2, "%d", 1);
snprintf (messageArray+10, 33, "%s", "main");I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..
Here's the prototype for the function :
int _snprintf( char *buffer, size_t count, const char *format, ... );
I usually use it like this :
const size_t bufferSize = 127
char buffer[bufferSize+1] = {0};_snprintf( buffer, bufferSize, "%3d %s", index, yourString );
You can combine the _snprintf calls into one since they are going into the same memory buffer.
-
in fact, it's snprintf() reason. snprintf seems fill out the gap in a string with '\0'. the code like:
memset (msgArray, ' ', 100);
snprintf (msgArray, 2, "%d", 1);
snprintf (messageArray+10, 33, "%s", "main");I assume the string should output like: 1 main but debugged it. it is : 1,\0, ' ', ' ', .. main, \0, ..
That is because you are doing it all wrong you either join them all up in one sequence or move the first pointer along They are designed to be used a completely different way :-) Single line:
snprintf (msgArray, sizeof(msgArray),"%d%s", 1,"main");
Concat sequence (with buffer safety):
char *cur = &msgArray[0];
char *end = &msgArray[sizeof(msgArray)-1];
cur += snprintf(cur, end-cur, "%d", 1);
snprintf(cur, end-cur, "%s", "main");In vino veritas