473,395 Members | 2,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

int or size_t as the index for the string

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 ?
Nov 13 '05 #1
11 5249

qazmlp <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c 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

Nov 13 '05 #2
On Fri, 01 Aug 2003 12:45:21 -0700, "E. Robert Tisdale"
<E.**************@jpl.nasa.gov> wrote:
Historically, C subscripts have always been type int


An integral type, not necessarily int.

--
Al Balmer
Balmer Consulting
re************************@att.net
Nov 13 '05 #3
Da*****@cern.ch (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) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"
Nov 13 '05 #4
On 31 Jul 2003 22:58:23 -0700, qa********@rediffmail.com (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>>
Nov 13 '05 #5

"qazmlp" <qa********@rediffmail.com> wrote in message
news:db**************************@posting.google.c 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! =-----
Nov 13 '05 #6
Barry Schwarz <sc******@deloz.net> wrote in message news:<bg**********@216.39.134.41>...
On 31 Jul 2003 22:58:23 -0700, qa********@rediffmail.com (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>>
Nov 13 '05 #7
In article <ye*************@king.cts.com>, ks*@cts.com 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.
Nov 13 '05 #8
In 'comp.lang.c', qa********@rediffmail.com (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- em**********@noos.fr [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/
Nov 13 '05 #9
In 'comp.lang.c', "Bill Cunningham" <so**@some.net> 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- em**********@noos.fr [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/
Nov 13 '05 #10
In article <bg**********@sunnews.cern.ch>, Dan Pop <Da*****@cern.ch> wrote:
In <db**************************@posting.google.com > qa********@rediffmail.com (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 <ro*******@umbc.edu>
Nov 13 '05 #11
In <ye*************@king.cts.com> Keith Thompson <ks*@cts.com> writes:
Da*****@cern.ch (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: Da*****@ifh.de
Nov 13 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Pelle Beckman | last post by:
Hi, Honestly, what is this 'size_t'? And while I'm at it - what is a 'mutable' var.? I've understood it somehow makes a const a non-const, but what's the point of that? Short explanations?...
4
by: qazmlp | last post by:
size_t len = strlen( myCString ) ; for( WHAT i = 0 ; i < len ; ++i ) { // iterate through the string // and do some operations }
7
by: William Xuuu | last post by:
Hi, This's a way of defining size_t and ssize_t in Linux: //"linux/types.h" typedef __kernel_size_t size_t; typedef __kernel_ssize_t ssize_t; //"asm/posix_types.h" typedef unsigned...
17
by: G Patel | last post by:
E. Robert Tisdale wrote: > > int main(int argc, char* argv) { > quad_t m = {0, 1, 2, 3}; > int r; > fprintf(stdout, "m = ("); > for (size_t...
17
by: candy_init | last post by:
I sometimes comes across statements which invloves the use of size_t.But I dont know exactly that what is the meaning of size_t.What I know about it is that it is used to hide the platform...
39
by: Mark Odell | last post by:
I've always declared variables used as indexes into arrays to be of type 'size_t'. I have had it brought to my attention, recently, that size_t is used to indicate "a count of bytes" and that using...
4
by: Jon Rea | last post by:
I have a class that has a private std::vector and uses it to store its content. I want to search the data within this vector and return the size_t index of that data. Pseudo-code: class Foo...
409
by: jacob navia | last post by:
I am trying to compile as much code in 64 bit mode as possible to test the 64 bit version of lcc-win. The problem appears now that size_t is now 64 bits. Fine. It has to be since there are...
89
by: Tubular Technician | last post by:
Hello, World! Reading this group for some time I came to the conclusion that people here are split into several fractions regarding size_t, including, but not limited to, * size_t is the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.