I have a function which requires me to loop from the end of a string to
the beginning on a char by char basis:
int foo (char string[])
{
unsigned int i;
for(i = strlen(string); i >= 0; i--)
{
/* do something */
}
return 1;
}
Here is my problem. strlen() returns an unsigned value, so if I use a
signed loop variable, I receive a warning: "'<' : signed/unsigned
mismatch"
However, if I use a unsigned loop variable to compare against the
correct return type of strlen(), the loop never ends. The value of 'i'
turns from 0 to the highest unsigned value possible.
Any ideas on how to solve this problem, and not receive the warning?
Thanks,
P.S. Traversing the string forwards is not an option. 9 3538
No Such Luck wrote: I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--)
Why do you want to loop if the string is empty?
{ /* do something */ }
return 1; }
Here is my problem. strlen() returns an unsigned value,
More precisely it returns the best type for indexing, namely, size_t.
so if I use a signed loop variable, I receive a warning: "'<' : signed/unsigned mismatch"
However, if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
Any ideas on how to solve this problem, and not receive the warning?
A compiler can issue a warning for any reason it likes, so no one
can guarantee you won't get a warning, but the following may be what
you're after...
size_t i = strlen(string);
while (i--)
{
putchar(string[i]);
}
--
Peter
In article <11**********************@z14g2000cwz.googlegroups .com>,
No Such Luck <no*********@hotmail.com> wrote: I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--) { /* do something */ }
return 1; }
Here is my problem. strlen() returns an unsigned value, so if I use a signed loop variable, I receive a warning: "'<' : signed/unsigned mismatch"
Are you sure? I don't see any way for the code you posted to produce
a warning anything like that.
However, if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
Any ideas on how to solve this problem, and not receive the warning?
Don't compare signed values with unsigned values.
Or (sometimes better, depending heavily on philosophical and aesthetic
opinions) just document that the code produces a warning and why the
warning should be ignored.
dave
--
Dave Vandervies dj******@csclub.uwaterloo.ca
More proof that the surefire way to discover the answer to your question
is to ask it in a public forum.
--Peter Ammon in comp.lang.c
No Such Luck wrote: I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--) { /* do something */ }
return 1; }
Here is my problem. strlen() returns an unsigned value, so if I use a signed loop variable, I receive a warning: "'<' : signed/unsigned mismatch"
However, if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
Any ideas on how to solve this problem, and not receive the warning?
Yes. There is no need to do the test i >= 0 when the test
can easily be made i > 0. This would solve the problem.
Example:
#include <string.h>
#include <stdio.n>
void foo (const char string[])
{
size_t i;
for(i = strlen(string); i > 0; i--)
putchar(string[i-1]);
putchar('\n');
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email) http://www.geocities.com/abowers822/
No Such Luck wrote: I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--) { /* do something */ } return 1; }
Here is my problem. strlen() returns an unsigned value, so if I use a signed loop variable, I receive a warning: "'<' : signed/unsigned mismatch"
However, if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
Any ideas on how to solve this problem, and not receive the warning?
ix = 1 + strlen(string);
while (ix--) {
/* do something. string[ix] is last unused char */
}
--
"They that can give up essential liberty to obtain a little
temporary safety deserve neither liberty nor safety."
-- B. Franklin, 1759
No Such Luck wrote: I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--) { /* do something */ }
return 1; }
Here is my problem. strlen() returns an unsigned value, so if I use a signed loop variable, I receive a warning: "'<' : signed/unsigned mismatch"
However, if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
Any ideas on how to solve this problem, and not receive the warning?
Thanks,
P.S. Traversing the string forwards is not an option.
Suppose ..
char string[] = "Hello";
The constant array has length 6.
strlen(string) is 5.
The five subscripts are 0..4
for (i = strlen(string); i > 0; --i)
will loop for i == 5..1
treat the individual characters with string[i-1]
You can define
int i;
such that i is signed. Regard..
for (i = strlen(string)-1; i >= 0; --i)
will loop for i == 4..0
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Joe Wright wrote:
.... snip ... You can define int i; such that i is signed. Regard..
for (i = strlen(string)-1; i >= 0; --i)
will loop for i == 4..0
You will get a nasty surprise when strlen(string) is zero.
--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
CBFalconer wrote: Joe Wright wrote: ... snip ... You can define int i; such that i is signed. Regard..
for (i = strlen(string)-1; i >= 0; --i)
will loop for i == 4..0
You will get a nasty surprise when strlen(string) is zero.
Unfortunately, chances are he won't. On most modern systems
where the conversion from unsigned to signed is simply a
no-op or truncation in twos complement representation, programs
like...
#include <stdio.h>
int main(void)
{
int i = 0u - 1;
printf("%d\n", i);
return 0;
}
....will output -1 as 'expected'.
The issue CBFalconer is hinting at is the fact that size_t is
unsigned and often unsigned int or unsigned long. Because of
promotion, the subtraction of 1 from such unsigned integers
will yield UINT_MAX or ULONG_MAX where both are likely to be
outside the range of int. If so, these values will then be
converted in an implementation defined way back into int in
the assignment to i. [Under C99, an implementation defined
signal can even be raised.] There is no guarantee that i will
get the value -1 on every conforming implementation.
--
Peter
No Such Luck wrote: I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--) { /* do something */ }
return 1; }
if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
unsigned int i;
for (i = strlen(string); i--; )
{
/* do something */
}
Old Wolf wrote: No Such Luck wrote:
I have a function which requires me to loop from the end of a string to the beginning on a char by char basis:
int foo (char string[]) { unsigned int i;
for(i = strlen(string); i >= 0; i--) { /* do something */ }
return 1; }
if I use a unsigned loop variable to compare against the correct return type of strlen(), the loop never ends. The value of 'i' turns from 0 to the highest unsigned value possible.
unsigned int i; for (i = strlen(string); i--; ) { /* do something */ }
I like that.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein --- This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: lian |
last post by:
Hi all,
I want to write some UTF-8 Chinese characters to file with following
php codes:
<code>
.......
$fp = fopen($filepath,'wb');
fwrite($fp,$utf8string,strlen($utf8string));
fclose($fp);...
|
by: muser |
last post by:
I'am having problem with the following:
rec1len = strlen(temp1);
temp1 is a character array, multi dimensional array.
it has been initialised as char temp1;
int rec1len.
error for this...
|
by: ajikoe |
last post by:
Hi,
I tried to follow the example in swig homepage.
I found error which I don't understand.
I use bcc32, I already include directory where my python.h exist in
bcc32.cfg.
/* File : example.c...
|
by: Albert |
last post by:
Why doesn't:
#include <stdio.h>
void reverse(char, int);
main()
{
char s;
|
by: almurph |
last post by:
Hi everyone,
Can you help me please? I am having a problem with the
encryption/decryption of words with the Irish fada in them. The Irish
fada is like this: αινσϊ/ΑΙΝΣΪ. It's kind of like the...
|
by: aldrin |
last post by:
I'm trying to run this code under windows xp sp2 using codeblocks v1.0
compiler with great difficulty.There is no problem with running this
under KDevelop in linux. Any help would be greatly...
|
by: Bob |
last post by:
I have been working on the following program. The goal is to have a
tokenizing routine that avoids some of the problems of strtok(), the
comments should explain the features.
This runs fine on...
|
by: Wilson |
last post by:
i am learning to program using c++ and was set a task of making a
simple encryption algorithim. I choose to start with one where simply
each letter is replaced with its equivilent in the alphabet...
|
by: cannymonkey |
last post by:
Hi,
I am very new to C++ (i.e. only begun using it in the last few weeks) and am currently maintaining an existing .dll file for the company i work for. After reading quite a few threads in this...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |