Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > int or size_t as the index for the string

Reply
Thread Tools

int or size_t as the index for the string

 
 
Mike Wahler
Guest
Posts: n/a
 
      07-30-2003

qazmlp <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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



 
Reply With Quote
 
 
 
 
qazmlp
Guest
Posts: n/a
 
      08-01-2003
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 ?
 
Reply With Quote
 
 
 
 
Dan Pop
Guest
Posts: n/a
 
      08-01-2003
In <(E-Mail Removed) > http://www.velocityreviews.com/forums/(E-Mail Removed) (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: (E-Mail Removed)
 
Reply With Quote
 
Alan Balmer
Guest
Posts: n/a
 
      08-01-2003
On Fri, 01 Aug 2003 12:45:21 -0700, "E. Robert Tisdale"
<(E-Mail Removed)> wrote:

>Historically, C subscripts have always been type int


An integral type, not necessarily int.

--
Al Balmer
Balmer Consulting
(E-Mail Removed)
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      08-02-2003
(E-Mail Removed) (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) (E-Mail Removed) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
 
Reply With Quote
 
Barry Schwarz
Guest
Posts: n/a
 
      08-02-2003
On 31 Jul 2003 22:58:23 -0700, (E-Mail Removed) (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>>
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      08-02-2003

"qazmlp" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
> 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! =-----
 
Reply With Quote
 
Hugh Slater
Guest
Posts: n/a
 
      08-02-2003
Barry Schwarz <(E-Mail Removed)> wrote in message news:<bgf8jg$bf4$0@216.39.134.41>...
> On 31 Jul 2003 22:58:23 -0700, (E-Mail Removed) (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>>
 
Reply With Quote
 
Randy Howard
Guest
Posts: n/a
 
      08-02-2003
In article <(E-Mail Removed)>, (E-Mail Removed) 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.


 
Reply With Quote
 
Emmanuel Delahaye
Guest
Posts: n/a
 
      08-02-2003
In 'comp.lang.c', (E-Mail Removed) (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- (E-Mail Removed) [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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: int vs. unsigned int vs. size_t Tim Prince C Programming 3 04-30-2011 10:34 PM
When should I use ptrdiff_t and size_t instead of int and unsigned int? PengYu.UT@gmail.com C++ 6 10-19-2005 01:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
int or size_t as the index for the string Mike Wahler C Programming 4 08-03-2003 12:16 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments