Something wrong with 'for' loop.

This is a discussion on Something wrong with 'for' loop. within the C Programming forums, part of the General Programming Boards category; When i tried using the strlen inside the for loop, it gives me the following warning msg. I would like ...

  1. #1
    Registered User
    Join Date
    Mar 2004
    Posts
    41

    Something wrong with 'for' loop.

    When i tried using the strlen inside the for loop, it gives me the following warning msg. I would like to know why this happend ??

    "warning C4018: '<' : signed/unsigned mismatch"

    Code:
    for(i=1; i < strlen(inputx); i++)

  2. #2
    Registered User
    Join Date
    Apr 2004
    Posts
    11
    warnings are warning.. just ignore them..

    but if you want an explanation:

    'i' probably is of the type signed int..

    because strlen always returns a positive value, strlen returns an unsigned int..

    to fix this warning: i < (int) strlen(inputx) --> typecasting
    Last edited by zuiplap; 05-05-2004 at 06:06 AM.

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > warnings are warning.. just ignore them..
    Remarkably bad advice IMO (who isn't joking)

    > warning C4018: '<' : signed/unsigned mismatch"
    Well what do you think would happen if you compared say -1 (a signed number) with an unsigned number?

    > strlen returns an unsigned int..
    It returns a size_t

    The easy fix is to declare
    Code:
    size_t i;
    for(i=1; i < strlen(inputx); i++)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Apr 2004
    Posts
    11


    ok, only ignore them when you know there is no harm
    here you can ignore the warning: here i > 0 always so there is no problem ignoring this warning..

    i was only guessing strlen returned an unsigned int.. i was to lazy to look it up.. sorry about that..

    now you have two solutions.. the point is, there is a type mismatch.. either change type of i, or use typcasting with one of them..

    btw. Salem, size_t, was is it? i remember seeing it in some functionheaders.. explain plz

    edit: wow, i went overload with the smileys

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    127
    >ok, only ignore them when you know there is no harm
    Why not fix all warnings so that they don't hide real errors?

    >i remember seeing it in some functionheaders.. explain plz
    size_t is the "unsigned integer type of the result of the sizeof operator".

  6. #6
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    size_t is a typedef for portability. It might be different on some machines

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 9
    Last Post: 07-15-2004, 03:30 PM
  2. Debugging-Looking in the wrong places
    By JaWiB in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 11-03-2003, 09:50 PM
  3. Confused: What is wrong with void??
    By Machewy in forum C++ Programming
    Replies: 19
    Last Post: 04-15-2003, 12:40 PM
  4. God
    By datainjector in forum A Brief History of Cprogramming.com
    Replies: 746
    Last Post: 12-22-2002, 11:01 AM
  5. Whats wrong?
    By Unregistered in forum C Programming
    Replies: 6
    Last Post: 07-14-2002, 01:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21