[RESOLVED] Clarification required: char* and null terminators
I need a function to add an ordinal suffix to an integer. ie. ordinalize(...1) gives "1st", ordinalize(...2) gives "2nd" and so on.
I'm porting some C# code that already does this but i'm uncertain about how to handle the null terminators on the end of the string...
Code:
void ordinalize(char *buf, int n);
{
char *ends[10] = {"th","st","nd","rd","th","th","th","th","th","th"};
int t = (n < 0 ? -n : n) % 100;
t %= ((t >= 11 && t <= 13) ? 1 : 10); //anything modded by 1 is always 0, this forcing a "th" out of the ends array...
sprintf(buf, "%d%s", n, ends[t]);
}
(fills the buf array with the string we require)
I'm calling this with:
Code:
char *temp[13]; //number will need 10 digits max plus 2 chars plus the null terminator
ordinalize(temp, 12345);
//temp now equals????
My questions:
1) Is a char array's contents always zeroes when its declared or is it undefined?
2) Does sprintf add a null terminator?
3) Do I need to manually add any null terminators anywhere?
:confused:
Re: Clarification required: char* and null terminators
Sorry, i hasten to add that my above code DOES work, but I'm a little unsure of the reliability of how I'm leaving my data.
Is there a memory leak issue with declaring a char array of 13 chars when most of the time maybe 5 will be used by my function?
Re: Clarification required: char* and null terminators
1) No,it just points to some trash in memory.
2) yes, sprintf() will null terminate the string, according to the man page.
3) don't think so ;)
also, whenever you use a string constant, such as in your array declarations, that string constant is automatically null terminated by the compiler as well.
So in memory, "th" should look like: 't' 'h' '\0'
The big question with this code vs. the C# code, I would think, is whether or not your buffer is big enough to hold the result. Using snprintf will prevent buffer overruns.
Re: Clarification required: char* and null terminators
Also, your "char* temp[13]" is an array of thirteen pointers to characters. Don't you want a simple string? (ie, char x[13] or char* f = new char(13))
Re: Clarification required: char* and null terminators
Sorry yes you are right, that extra * on the temp array was a typo.
Thanks for the reassurance :D
I chose 13 as the buffer length because a signed integer will be at most 10 (decimal) chars long then you add the "th", thats 2 more and finaly a null is a total of 13.
However it suddenly occurs to me that it should be 14 since negative numbers have a "-" on the front :D
Doh!
Re: [RESOLVED] Clarification required: char* and null terminators
In a loop, my function gives these results:
-30th,-29th,-28th,-27th,-26th,-25th,-24th,-23rd,-22nd,-21st,
-20th,-19th,-18th,-17th,-16th,-15th,-14th,-13th,-12th,-11th,
-10th,-9th,-8th,-7th,-6th,-5th,-4th,-3rd,-2nd,-1st,
0th,1st,2nd,3rd,4th,5th,6th,7th,8th,9th,10th
11th,12th,13th14th,15th,16th,17th,18th,19th,
20th,21st,22nd,23rd,24th,25th,26th,27th,28th,29th
Re: [RESOLVED] Clarification required: char* and null terminators
the only calue that causes a funny result is 0x80000000 (-2^31) which gives this result on the console:
"-2147483648(null)"
I don't know where the (null) thing is coming from, looks like a compiler message to me.
(0x80000001) gives -2147483647th, which is correct though.
Re: [RESOLVED] Clarification required: char* and null terminators
That's because 2^31 is outside the domain of a signed int, so the operation -i yields undefined behaviour when i is -2^31.
Perhaps you could do your reducing modulos first and then invert. Or at least do the % 100 first.
By the way, that problem would exist in the C# version, too.
Re: [RESOLVED] Clarification required: char* and null terminators
Its rare enough that I'm not worried enough to do anything about it.