sprintf function doesn't surrender to me!!!

Discussion in 'C Programming' started by Yodai, Jan 14, 2004.

  1. Yodai

    Yodai Guest

    Hey guys...

    I've looked up and down, tested and retested, and I've found out my program
    is stoping in this function.... Do you guys see anything weird here? cause
    it compiles all right... (Just so you know, the functions I am calling to be
    written are copied below)


    {
    sprintf(NewKey, "%s/%s/%s %s:%s%03u", Get_dia1(),
    Get_mes1(), Get_any1(), Get_hora1(), Get_min1(), Get_valor1());
    memcpy(Key, NewKey, 30);
    break;
    }




    /*here the functions that serve the data to the sprintf above: (they're
    really simple and pretty much failsafe, only that (so far) they allways
    output 0xFF, and I am starting to wonder if maybe unsigned char cannot hold
    0xff?? */

    unsigned char Get_tipus1(void)
    {
    volatile unsigned char r1tipus = *R1TIPUS;
    return(r1tipus);
    }

    unsigned char Get_dia1(void)
    {
    volatile unsigned char r1dia=*R1DIA;
    return(r1dia);
    }

    unsigned char Get_mes1(void)
    {
    volatile unsigned char r1mes = *R1MES;
    return(r1mes);
    }

    unsigned char Get_any1(void)
    {
    volatile unsigned char r1any=*R1ANY;
    return(r1any);
    }

    unsigned char Get_hora1(void)
    {
    volatile unsigned char r1hora=*R1HORA;
    return(r1hora);
    }

    unsigned char Get_min1(void)
    {
    volatile unsigned char r1min=*R1MIN;
    return(r1min);
    }

    unsigned int Get_valor1(void)
    {
    volatile unsigned int r1valor=*R1VALOR;
    return(r1valor);
    }


    cheers!!!

    Yodai
    Yodai, Jan 14, 2004
    #1
    1. Advertising

  2. Yodai wrote:

    > Hey guys...
    >
    > I've looked up and down, tested and retested, and I've found out my program
    > is stoping in this function.... Do you guys see anything weird here?

    Yes ;-)



    cause
    > it compiles all right...


    You should bump up your compilers warning level, try e.g. this for gcc:
    gcc -Wall -W -ansi -pedantic
    for a start.

    (Just so you know, the functions I am calling to be
    > written are copied below)
    >
    >
    > {
    > sprintf(NewKey, "%s/%s/%s %s:%s%03u", Get_dia1(),
    > Get_mes1(), Get_any1(), Get_hora1(), Get_min1(), Get_valor1());
    > memcpy(Key, NewKey, 30);
    > break;
    > }


    The %s directive expects a pointer to a zero-terminated string and
    you're giving it an unsigned char. Expect trouble ;-)

    HTH,
    Bjørn


    >
    >
    >
    >
    > /*here the functions that serve the data to the sprintf above: (they're
    > really simple and pretty much failsafe, only that (so far) they allways
    > output 0xFF, and I am starting to wonder if maybe unsigned char cannot hold
    > 0xff?? */
    >
    > unsigned char Get_tipus1(void)
    > {
    > volatile unsigned char r1tipus = *R1TIPUS;
    > return(r1tipus);
    > }
    >
    > unsigned char Get_dia1(void)
    > {
    > volatile unsigned char r1dia=*R1DIA;
    > return(r1dia);
    > }
    >
    > unsigned char Get_mes1(void)
    > {
    > volatile unsigned char r1mes = *R1MES;
    > return(r1mes);
    > }
    >
    > unsigned char Get_any1(void)
    > {
    > volatile unsigned char r1any=*R1ANY;
    > return(r1any);
    > }
    >
    > unsigned char Get_hora1(void)
    > {
    > volatile unsigned char r1hora=*R1HORA;
    > return(r1hora);
    > }
    >
    > unsigned char Get_min1(void)
    > {
    > volatile unsigned char r1min=*R1MIN;
    > return(r1min);
    > }
    >
    > unsigned int Get_valor1(void)
    > {
    > volatile unsigned int r1valor=*R1VALOR;
    > return(r1valor);
    > }
    >
    >
    > cheers!!!
    >
    > Yodai
    >
    >
    >



    --
    The worlds fastest web server is now available
    at http://highlander.metasystems.no:2000. Enjoy!
    =?ISO-8859-1?Q?Bj=F8rn_Augestad?=, Jan 14, 2004
    #2
    1. Advertising

  3. Yodai <> wrote:
    > Hey guys...


    > I've looked up and down, tested and retested, and I've found out my program
    > is stoping in this function.... Do you guys see anything weird here? cause
    > it compiles all right... (Just so you know, the functions I am calling to be
    > written are copied below)



    > {
    > sprintf(NewKey, "%s/%s/%s %s:%s%03u", Get_dia1(),
    > Get_mes1(), Get_any1(), Get_hora1(), Get_min1(), Get_valor1());
    > memcpy(Key, NewKey, 30);
    > break;
    > }


    I am going to assume that NewKey is correctly defined as either
    a character array or a valid pointer to some dynamically allocated
    memory. I am also going to assume that its size is sufficient.

    Now, given these assumptions, your problem is as follows:

    The %s format specifier expects a pointer to a null-terminated
    character array, aka 'string'.

    > unsigned char Get_dia1(void)
    > {
    > volatile unsigned char r1dia=*R1DIA;
    > return(r1dia);
    > }


    This and your other functions, which I have snipped, return
    a single unsigned char. This is not what sprintf is expecting.

    --
    Alex Monjushko ()
    Alex Monjushko, Jan 14, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pep
    Replies:
    5
    Views:
    4,059
  2. Mike Chirico
    Replies:
    2
    Views:
    3,837
    Grumble
    Nov 19, 2003
  3. Pilatus
    Replies:
    3
    Views:
    544
    Pilatus
    Dec 18, 2003
  4. Earth

    sprintf function

    Earth, Feb 8, 2004, in forum: C Programming
    Replies:
    19
    Views:
    6,433
    Irrwahn Grausewitz
    Feb 9, 2004
  5. Replies:
    8
    Views:
    212
    Martien Verbruggen
    Sep 27, 2008
Loading...

Share This Page