sprintf function doesn't surrender to me!!!

Y

Yodai

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
 
?

=?ISO-8859-1?Q?Bj=F8rn_Augestad?=

Yodai said:
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
 
A

Alex Monjushko

Yodai said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,820
Latest member
GilbertoA5

Latest Threads

Top