Thread: Help - string array

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    4

    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.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Wrong forum, yes?
    (This is C, not C++.)
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    23
    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>

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    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.

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    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.

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    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.
    Last edited by whiteflags; 03-02-2011 at 12:11 AM.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    4
    Wow! Got it after converting the tmp to int before assigning to newtime.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pointer to array of string and Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 2
    Last Post: 06-15-2007, 06:04 AM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 09:11 AM
  3. Program using classes - keeps crashing
    By webren in forum C++ Programming
    Replies: 4
    Last Post: 09-16-2005, 03:58 PM
  4. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  5. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM