-
Help - string array
Hi,
I have one string as below and would like to change the content one by one. I tried in this way but it's not successed.
Code:
unsigned char newtime[7];
for (x=7; x>0; x--)
{ sprintf (newtime[x-1], "%d", x);
printf ("newtime[%d] %d\n",x, newtime[x-1]);
}
The console output is:
newtime[6] 0
newtime[5] 0
newtime[4] 0
newtime[3] 0
newtime[2] 0
newtime[1] 0
newtime[0] 0
Where's the problem and what should do?
Thanks.
-
Wrong forum, yes?
(This is C, not C++.)
-
you need to assign data to newtime.
not sure what your intentions were for the sprintf call.
<code>
newtime[ 0 ] = 'a';
newtime[ 1 ] = 'b';
newtime[ 2 ] = 'c';
</code>
-
Sorry if I have posted to a wrong area.
I've been assigned the newtime like this but the content still has not changed:
Code:
unsigned char time[7] = {0x00,0x30,0x09,0x01,0x03,0x00,0x11};
Actually, I want to replace the content of the array with a varable input from terminal.
Thanks.
-
I'm not sure what you want to achieve but sprintf does not change the value of any individual character. sprintf is actually part of the printf family of functions and works a lot like printf; the only difference is the output is stored as a C-string.
-
Well, I want to change the time of a RTC IC by input a command line from terminal.
Example:
settime,10,20,30,40,50,60,70
There's an array to store the time information. Example:
Code:
unsigned char time[7] = {0x00,0x30,0x09,0x01,0x03,0x00,0x11};
I use a for loop and the strtok to separate the string into
10
20
30
40
50
60
70
Code:
unsigned char time[7] = {0x00,0x30,0x09,0x01,0x03,0x00,0x11}; // original data
unsigned char newtime[7];
unsigned char * tmp;
int x;
if (strncmp(uart_tmp, "settime", 7) == 0)
{ tmp = strtok(uart_tmp, ",");
for (x=7; x>0; x--)
{ tmp = strtok(NULL,",");
newtime[x-1] = tmp;
printf ("tmp: %s, newtime[%d] %d\n", tmp, (x-1), newtime[x-1]);
}
When I type in "settime,10,20,30,40,50,60,70":, the output shows:
tmp: 10, newtime[6] 20
tmp: 20, newtime[5] 23
tmp: 30, newtime[4] 26
tmp: 40, newtime[3] 29
tmp: 50, newtime[2] 32
tmp: 60, newtime[1] 35
tmp: 70, newtime[0] 38
Then, how can I repalce the array content with these seven data?
Thanks.
-
Use strncpy(time, newtime, 7); after all that.
sprintf only outputs C-strings, so in this case it was just not the right function to call, since you are not using a C-string.
Well wait, that's wrong too. Now you've got me doing it.
There's a fundamental problem with what's happening here. A character is just an indivisible piece of data like 'a'. If you went to store a in a character array, you could.
somewhere[ x ] = 'a';
But the string "a" is different. It has to occupy at least two characters in somewhere.
somewhere[ x ] = 'a';
somewhere[ x + 1 ] = '\0';
Likewise for any time you split up a string. You can't munge a "10" into 10 and the like. You would have to convert "10" to an integer and then downcast to char before finally assigning it somewhere.
-
Wow! Got it after converting the tmp to int before assigning to newtime.
Thanks!