473,800 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5289

qazmlp <qa********@red iffmail.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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_Keit h) 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********@redi ffmail.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********@red iffmail.com> wrote in message
news:db******** *************** ***@posting.goo gle.com...
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********@redi ffmail.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********@redi ffmail.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**********@no os.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.ne t> 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**********@no os.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

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

Similar topics

5
3361
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? Links? FAQ?
4
1099
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
52692
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 int __kernel_size_t;
17
2810
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 j = 0; j < 4; ++j) Why did you declare j as type size_t ?
17
134460
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 details.I tried to find its meaning in the header files but did'nt got a good answer.So can somebody please tell me that what is the meaning of size_t and what are its possible values? Thanks
39
8066
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 it otherwise is confusing. I also thought that size_t could be signed but it seems I was wrong on that one. So if you were to see code iterating through a table of Foo objects using an index of size_t type, would it be confusing? Should I have...
4
1773
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 { public: std::string name;
409
11186
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 objects that are more than 4GB long. The problem is, when you have in thousands of places
89
5775
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 right thing to use for every var that holds the number of or size in bytes of things. * size_t should only be used when dealing with library functions.
0
9691
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9551
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10507
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10279
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10036
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9092
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6815
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5607
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4150
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.