int or size_t as the index for the string

Discussion in 'C Programming' started by Mike Wahler, Jul 30, 2003.

  1. Mike Wahler

    Mike Wahler Guest

    qazmlp <> wrote in message
    news:...
    > I am sorry for my previous incomplete posting.
    >
    > Here is the correct one:
    >
    > size_t len = strlen( myCString ) ;
    >
    > for( WHAT i = 0 ; i < len ; ++i )
    > {
    > // iterate through the string
    > // and do some operations
    > }
    >
    >
    > What is the best choice to replace 'WHAT' in the above code?
    > size_t or int ? Why ?


    If you *know* your indices will *always* fall in the range
    of type 'int', then you could use it.

    Or if you don't want to be bothered with such annoying
    details that could bite you later, use 'size_t' which
    is *guaranteed* to be able to represent any index value.

    So, neither 'int' nor 'size_t' is necessarily "right" or
    "wrong" per se, but if asked for advice, I say use 'size_t'
    whenever storing the size of an object or an array index.

    -Mike
     
    Mike Wahler, Jul 30, 2003
    #1
    1. Advertising

  2. Mike Wahler

    qazmlp Guest

    I am sorry for my previous incomplete posting.

    Here is the correct one:

    size_t len = strlen( myCString ) ;

    for( WHAT i = 0 ; i < len ; ++i )
    {
    // iterate through the string
    // and do some operations
    }


    What is the best choice to replace 'WHAT' in the above code?
    size_t or int ? Why ?
     
    qazmlp, Aug 1, 2003
    #2
    1. Advertising

  3. Mike Wahler

    Dan Pop Guest

    In <> (qazmlp) writes:

    >Here is the correct one:
    >
    >size_t len = strlen( myCString ) ;
    >
    >for( WHAT i = 0 ; i < len ; ++i )
    >{
    > // iterate through the string
    > // and do some operations
    >}
    >
    >
    >What is the best choice to replace 'WHAT' in the above code?


    White space or nothing at all.

    >size_t or int ?


    Neither. Both will render your code syntactically invalid.

    >Why ?


    Because the currently implemented C specification says so. Do not
    confuse C with C99 or C++ !

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 1, 2003
    #3
  4. Mike Wahler

    Alan Balmer Guest

    On Fri, 01 Aug 2003 12:45:21 -0700, "E. Robert Tisdale"
    <> wrote:

    >Historically, C subscripts have always been type int


    An integral type, not necessarily int.

    --
    Al Balmer
    Balmer Consulting
     
    Alan Balmer, Aug 1, 2003
    #4
  5. (Dan Pop) writes:
    [...]
    > Neither. Both will render your code syntactically invalid.
    >
    > >Why ?

    >
    > Because the currently implemented C specification says so. Do not
    > confuse C with C99 or C++ !


    C99 is C. It's worth pointing out that C90 is still more widely
    implemented than C99, but that doesn't make C99 invalid or off-topic.

    I know of at least two C compilers (a recent gcc with "-std=c99", and
    Intel's ecc for IA-64) that claim C99 support and seem to handle the

    for (int i = 1; i <= 10; i ++)

    construct correctly. Whether they fully support the entire C99
    language is another question.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
    Schroedinger does Shakespeare: "To be *and* not to be"
     
    Keith Thompson, Aug 2, 2003
    #5
  6. On 31 Jul 2003 22:58:23 -0700, (qazmlp)
    wrote:

    >I am sorry for my previous incomplete posting.
    >
    >Here is the correct one:
    >
    >size_t len = strlen( myCString ) ;
    >
    >for( WHAT i = 0 ; i < len ; ++i )
    >{
    > // iterate through the string
    > // and do some operations
    >}
    >
    >
    >What is the best choice to replace 'WHAT' in the above code?
    >size_t or int ? Why ?


    Since size_t is guaranteed to be able to hold the size of the largest
    object the compiler can support and int is not, what do you think?


    <<Remove the del for email>>
     
    Barry Schwarz, Aug 2, 2003
    #6
  7. "qazmlp" <> wrote in message
    news:...
    > I am sorry for my previous incomplete posting.
    >
    > Here is the correct one:
    >
    > size_t len = strlen( myCString ) ;
    >
    > for( WHAT i = 0 ; i < len ; ++i )
    > {
    > // iterate through the string
    > // and do some operations
    > }
    >
    >
    > What is the best choice to replace 'WHAT' in the above code?
    > size_t or int ? Why ?


    Am I correct in guessing the headers for this code are stdio.h and string.h,
    and that's it.

    Bill [beginner]





    -----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
    http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
    -----== Over 80,000 Newsgroups - 16 Different Servers! =-----
     
    Bill Cunningham, Aug 2, 2003
    #7
  8. Mike Wahler

    Hugh Slater Guest

    Barry Schwarz <> wrote in message news:<bgf8jg$bf4$0@216.39.134.41>...
    > On 31 Jul 2003 22:58:23 -0700, (qazmlp)
    > wrote:
    >
    > >I am sorry for my previous incomplete posting.
    > >
    > >Here is the correct one:
    > >
    > >size_t len = strlen( myCString ) ;
    > >
    > >for( WHAT i = 0 ; i < len ; ++i )
    > >{
    > > // iterate through the string
    > > // and do some operations
    > >}
    > >
    > >
    > >What is the best choice to replace 'WHAT' in the above code?
    > >size_t or int ? Why ?

    >
    > Since size_t is guaranteed to be able to hold the size of the largest
    > object the compiler can support and int is not, what do you think?
    >
    >

    <<Remove the del for email>>
     
    Hugh Slater, Aug 2, 2003
    #8
  9. Mike Wahler

    Randy Howard Guest

    In article <>, says...
    > C99 is C. It's worth pointing out that C90 is still more widely
    > implemented than C99, but that doesn't make C99 invalid or off-topic.


    I don't think anyone said C99 was off-topic.

    > I know of at least two C compilers (a recent gcc with "-std=c99", and
    > Intel's ecc for IA-64) that claim C99 support and seem to handle the
    >
    > for (int i = 1; i <= 10; i ++)


    Do the docs for Intel's ecc claim 100% C99 conformance, or just that
    "some C99 constructs work"? Not being argumentative, I'm very curious.
    Up until now, only the Comeau compiler has been mentioned as falling
    into that category, but I don't know how widespread its use is, and
    if it's known to be true.

    > construct correctly. Whether they fully support the entire C99
    > language is another question.


    You could argue that since gcc is probably available on more platforms
    than any other C compiler (or perhaps even all the rest combined),
    that unless you are doing embedded work for hardware not supported by
    gcc, that writing to whatever "-std=c999" supports for a given version
    of gcc might be good enough to provide "ample portabality", provided that
    you realize that's quite a nebulous term.
     
    Randy Howard, Aug 2, 2003
    #9
  10. In 'comp.lang.c', (qazmlp) wrote:

    > size_t len = strlen( myCString ) ;
    >
    > for( WHAT i = 0 ; i < len ; ++i )


    Your construct is C99. Don't use too many spaces. Just the minimum to make
    the code clear.

    > {
    > // iterate through the string
    > // and do some operations
    > }
    >
    >
    > What is the best choice to replace 'WHAT' in the above code?
    > size_t or int ? Why ?


    The 'size_t' type is a good choice for an array index, because it covers from
    0 to the maximum size for an object - 1. (except in twisted cases using a
    sub-array where the index can be negative).

    size_t len = strlen (myCString);
    size_t i;

    for (i = 0; i < len; ++i)
    {
    /* iterate through the string */
    /* and do some operations */
    }

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Aug 2, 2003
    #10
  11. In 'comp.lang.c', "Bill Cunningham" <> wrote:

    >> size_t len = strlen( myCString ) ;
    >>
    >> for( WHAT i = 0 ; i < len ; ++i )
    >> {
    >> // iterate through the string
    >> // and do some operations
    >> }
    >>
    >> What is the best choice to replace 'WHAT' in the above code?
    >> size_t or int ? Why ?

    >
    > Am I correct in guessing the headers for this code are stdio.h and
    > string.h, and that's it.


    Not really. Assuming 'size_t' is used, there are <stddef.h> and <stringh>,
    but because strlen() returns a size_t, <stddef.h> is probably already
    included in <string.h>, so <string.h> would suffice.

    --
    -ed- [remove YOURBRA before answering me]
    The C-language FAQ: http://www.eskimo.com/~scs/C-faq/top.html
    <blank line>
    FAQ de f.c.l.c : http://www.isty-info.uvsq.fr/~rumeau/fclc/
     
    Emmanuel Delahaye, Aug 2, 2003
    #11
  12. In article <bgdgi6$e6l$>, Dan Pop <> wrote:
    >In <> (qazmlp) writes:
    >
    >>size_t len = strlen( myCString ) ;
    >>
    >>for( WHAT i = 0 ; i < len ; ++i )
    >>{
    >> // iterate through the string
    >> // and do some operations
    >>}
    >>
    >>What is the best choice to replace 'WHAT' in the above code?

    >
    >White space or nothing at all.
    >
    >>size_t or int ?

    >
    >Neither. Both will render your code syntactically invalid.
    >
    >>Why ?

    >
    >Because the currently implemented C specification says so. Do not
    >confuse C with C99 or C++ !


    Isn't the discussion of implementatons off-topic in this newsgroup?

    --
    Rouben Rostamian <>
     
    Rouben Rostamian, Aug 3, 2003
    #12
  13. Mike Wahler

    Dan Pop Guest

    In <> Keith Thompson <> writes:

    > (Dan Pop) writes:
    >[...]
    >> Neither. Both will render your code syntactically invalid.
    >>
    >> >Why ?

    >>
    >> Because the currently implemented C specification says so. Do not
    >> confuse C with C99 or C++ !

    >
    >C99 is C.


    In comp.std.c. Which is a different newsgroup. None of the
    implementations currently used by c.l.c regulars claims C99 conformance.
    Therefore C99 should not be confused with C in this newsgroup.

    >It's worth pointing out that C90 is still more widely
    >implemented than C99, but that doesn't make C99 invalid or off-topic.


    This being comp.lang.c, C99 is mostly a curiosity rather than the
    newsgroup's topic. When was the last time you've seen people recommending
    solutions based on <fenv.h>, <tgmath.h> or <complex.h>?

    >I know of at least two C compilers (a recent gcc with "-std=c99", and
    >Intel's ecc for IA-64) that claim C99 support


    To the best of my knowledge, neither claims C99 *conformance*.
    See http://gcc.gnu.org/c99status.html for gcc.

    >and seem to handle the
    >
    > for (int i = 1; i <= 10; i ++)
    >
    >construct correctly. Whether they fully support the entire C99
    >language is another question.


    Nope, it ain't! There is a whole world of difference between writing C99
    code for conforming C99 implementations (i.e. writing portable C99 code)
    and writing code that uses the C99 features supported by one
    non-conforming C99 implementation or another. Especially considering
    that the support for such features might be broken (which is currently
    the case with gcc: the feature is there, but it has GNU C semantics, not
    C99 semantics (unless they are the same)).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
     
    Dan Pop, Aug 4, 2003
    #13
    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. Schnoffos
    Replies:
    2
    Views:
    1,251
    Martien Verbruggen
    Jun 27, 2003
  2. Mike Wahler

    int or size_t as the index for the string

    Mike Wahler, Jul 31, 2003, in forum: C Programming
    Replies:
    4
    Views:
    518
  3. Hal Styli
    Replies:
    14
    Views:
    1,709
    Old Wolf
    Jan 20, 2004
  4. Replies:
    6
    Views:
    6,478
    Greg Comeau
    Oct 19, 2005
  5. Tim Prince

    Re: int vs. unsigned int vs. size_t

    Tim Prince, Apr 30, 2011, in forum: C Programming
    Replies:
    3
    Views:
    309
    Ian Collins
    Apr 30, 2011
Loading...

Share This Page