int or size_t as the index for the string

M

Mike Wahler

qazmlp said:
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
 
Q

qazmlp

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 ?
 
D

Dan Pop

In said:
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.

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

Dan
 
K

Keith Thompson

Neither. Both will render your code syntactically invalid.


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.
 
B

Barry Schwarz

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>>
 
B

Bill Cunningham

qazmlp said:
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]
 
H

Hugh Slater

Barry Schwarz said:
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>>
 
R

Randy Howard

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.
 
E

Emmanuel Delahaye

In said:
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 */
}
 
E

Emmanuel Delahaye

Bill Cunningham said:
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.
 
R

Rouben Rostamian

White space or nothing at all.


Neither. Both will render your code syntactically invalid.


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?
 
D

Dan Pop

In said:
(e-mail address removed) (Dan Pop) writes:
[...]
Neither. Both will render your code syntactically invalid.


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
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top