Results 1 to 9 of 9

Thread: [RESOLVED] Clarification required: char* and null terminators

  1. #1

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Resolved [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?

    Last edited by wossname; Oct 9th, 2006 at 02:09 PM.
    I don't live here any more.

  2. #2

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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?
    I don't live here any more.

  3. #3
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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.
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  4. #4
    PowerPoster sunburnt's Avatar
    Join Date
    Feb 2001
    Location
    Boulder, Colorado
    Posts
    1,403

    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))
    Every passing hour brings the Solar System forty-three thousand miles closer to Globular Cluster M13 in Hercules -- and still there are some misfits who insist that there is no such thing as progress.

  5. #5

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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

    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

    Doh!
    I don't live here any more.

  6. #6

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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
    I don't live here any more.

  7. #7

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.
    I don't live here any more.

  8. #8
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    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.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  9. #9

    Thread Starter
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    Re: [RESOLVED] Clarification required: char* and null terminators

    Its rare enough that I'm not worried enough to do anything about it.
    I don't live here any more.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width