B
bushman
Hello,
I wrote a simple program that reads a formatted string into union
members and then does an sprintf to rewrite those values back into
another formatted string. Below is the code, if you run it, you will
see that you get a valid number printed for the first %d, however, all
values after that are invalid pointers. Please see the code below and
I will explain what I have found so far:
#include <stdio.h>
#include <windows.h>
union localizeValue
{
int intValue;
char charValue[260];
};
void main()
{
char formatted[128] = {0};
char unFormatted[128] = {0};
localizeValue values1, values2, values3;
char tempStr[128] = {0};
strcpy(formatted, "Using the help system %d times, every %d days,
every %d weeks");
strcpy(unFormatted, "Using the help system 27 times, every 5 days,
every 1 weeks");
sscanf(unFormatted, formatted, &values1, &values2, &values3);
sprintf(tempStr, formatted, values1, values2, values3);
}
so in this case I get the value of tempStr = "Using the help system 27
times, every -858993460 days, every -858993460 weeks"
if I specify the values as value1.intValue, value2.intValue, etc. in
the sprintf statement, everything prints out fine. So it seems that
the first value (values1) is has sprintf pointing at the beginning of
my union so it displays the correct number. It also seems as though
from there on out it's not pointing at the beggining of the next
union, so it has not value. If I change the value in the union of
charValue to be an array of size 4 (size of int), everything works
correctly also.
Can anyone explain why I am seeing this behavior... is there a fix for
this that anyone knows of?
Thanks!
I wrote a simple program that reads a formatted string into union
members and then does an sprintf to rewrite those values back into
another formatted string. Below is the code, if you run it, you will
see that you get a valid number printed for the first %d, however, all
values after that are invalid pointers. Please see the code below and
I will explain what I have found so far:
#include <stdio.h>
#include <windows.h>
union localizeValue
{
int intValue;
char charValue[260];
};
void main()
{
char formatted[128] = {0};
char unFormatted[128] = {0};
localizeValue values1, values2, values3;
char tempStr[128] = {0};
strcpy(formatted, "Using the help system %d times, every %d days,
every %d weeks");
strcpy(unFormatted, "Using the help system 27 times, every 5 days,
every 1 weeks");
sscanf(unFormatted, formatted, &values1, &values2, &values3);
sprintf(tempStr, formatted, values1, values2, values3);
}
so in this case I get the value of tempStr = "Using the help system 27
times, every -858993460 days, every -858993460 weeks"
if I specify the values as value1.intValue, value2.intValue, etc. in
the sprintf statement, everything prints out fine. So it seems that
the first value (values1) is has sprintf pointing at the beginning of
my union so it displays the correct number. It also seems as though
from there on out it's not pointing at the beggining of the next
union, so it has not value. If I change the value in the union of
charValue to be an array of size 4 (size of int), everything works
correctly also.
Can anyone explain why I am seeing this behavior... is there a fix for
this that anyone knows of?
Thanks!