passing arg 1 of 'strlen' makes pointer from integer without a cast

This is a discussion on passing arg 1 of 'strlen' makes pointer from integer without a cast within the C Programming forums, part of the General Programming Boards category; Does anyone know, why a warning come up when I run this? Warnings are the lines with red color. How ...

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    27

    Unhappy passing arg 1 of 'strlen' makes pointer from integer without a cast

    Does anyone know, why a warning come up when I run this?
    Warnings are the lines with red color.
    How can I fix this warning, please help...

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    #define MAX 30
    
    int main()
    {
        int customerCount=0;
        char input[32];
        char drinks[MAX][10];
        char size[MAX][6];
        char hotCold[MAX][5];
        int i=0;
    
        printf("How many people are at your table?: ");
        fgets(input,32,stdin);
        sscanf(input,"%d",&customerCount);
    
        if(customerCount==0)
            {
                printf("END");
                exit(0);
                /*summary*/
            }
    
        printf("Choose what type of drink would you like.\n");
    
        while(strcmp(drinks[i],"None")!=0)
            do
            {
                printf("Coffee, Tea, Cocoa, or Ovaltine?: ");
                fgets(input,32,stdin);
                sscanf(input,"%s",drinks[i]);
            }
            while(strlen(drinks[i]==0));
    
            do
            {
                printf("Small or Large?: ");
                fgets(input,32,stdin);
                sscanf(input,"%s",size);
            }
            while(strlen(size==0));
    
            do
            {
                printf("Hot or Cold?: ");
                fgets(input,32,stdin);
                sscanf(input,"%s",hotCold);
            }
            while(strlen(hotCold==0));
    }

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Code:
    while(strlen(drinks[i]==0));
    should be
    Code:
    while(strlen(drinks[i])==0);
    The other while() statements have the same problem.
    bit∙hub [bit-huhb] n. A source and destination for information.

  3. #3
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Let's dissect this:
    while(strlen(drinks[i]==0));

    while(
    strlen(drinks[i]==0)
    );

    while(
    strlen(
    drinks[i]==0
    )
    );

    So essentially, you're comparing drinks[i] to 0 (ie is it a NULL pointer?), which gives true or false, and that is passed to strlen. Oops.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Some other issues with your code:
    Code:
    sscanf(input,"%s",size);
    ...
    sscanf(input,"%s",hotCold);
    You can't copy a string into a 2D char array.

    Also 2 of your while loops attempt to treat a 2D char array as a string (they are passed to strlen()).
    bit∙hub [bit-huhb] n. A source and destination for information.

  5. #5
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    Thanks a lot
    I've already make the change
    but when i run the program,
    it keeps asking what's in the loop eventhough i type in sth
    did i do anything wrong???

  6. #6
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    You told it to loop until the user enters in the text, "None".
    bit∙hub [bit-huhb] n. A source and destination for information.

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    i mean the small loop of strlen
    eventhough i type in sth, it still loops

  8. #8
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    No it doesn't. It loops because of this line:
    Code:
    while(strcmp(drinks[i],"None")!=0)
    Look at your code:
    Code:
        while(strcmp(drinks[i],"None")!=0)
            do
            {
                printf("Coffee, Tea, Cocoa, or Ovaltine?: ");
                fgets(input,32,stdin);
                sscanf(input,"%s",drinks[i]);
            }
            while(strlen(drinks[i]==0));
    The first loops says, "While drinks[i] doesn't equal 'None'"...
    The second loops says, "Get user input while drinks[i] is of zero length".
    If you type in anything other than the text, "None", it will continue looping.
    bit∙hub [bit-huhb] n. A source and destination for information.

  9. #9
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    i see...
    is there a way to fix this?
    the program want to ask the type of drink until None
    but if the user type nothing, i want it to ask again.

  10. #10
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Quote Originally Posted by ChoCo View Post
    i see...
    is there a way to fix this?
    the program want to ask the type of drink until None
    but if the user type nothing, i want it to ask again.
    That's exactly what it is doing...
    It will loop until the user types in "None". Maybe I don't understand what it is you are asking...
    bit∙hub [bit-huhb] n. A source and destination for information.

  11. #11
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    I mean it already loop until None already
    but I also want to check if the user doesn't type anything, the program should ask again
    and if the user type in sth but it's not None, the program should proceed to the next operation

  12. #12
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    Then you probably want something more like:
    Code:
    while(1)
    {
        do
        {
            get drink type
        }
        while(drink type length > 0);
    
        if(drink type is "None")
            break; // break out of the main loop
    
       get the size, and hot/cold down here
    }
    bit∙hub [bit-huhb] n. A source and destination for information.

  13. #13
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,411
    Use a flowchart. You should not have to ask this because the solution is in your mind.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    Sorry to ask a lot, because i'm very new to c programming
    can you explain,
    1. what is while(1)?
    2. why do you use
    Code:
    while(drink type length > 0);
    to loop?

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    fgets() will include a \n in the input string, so the strcmp will never succeed:

    "None" != "None\n"

    Use strncmp() for this instead:

    while(strcmp(drinks[i],"None",4)!=0)

    ps. while(1) is an infinite loop -- (1) is always true. You must use break, return, or exit to end the loop.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. warning: cast to pointer from integer of different size
    By DavidDobson in forum C Programming
    Replies: 6
    Last Post: 12-03-2008, 05:37 PM
  2. Replies: 1
    Last Post: 03-24-2008, 10:16 AM
  3. "assignment makes integer from pointer without a cast"
    By Freez3L in forum C Programming
    Replies: 4
    Last Post: 11-04-2002, 03:26 AM
  4. assignment makes pointer from integer
    By crescen7 in forum C Programming
    Replies: 4
    Last Post: 06-25-2002, 10:08 PM
  5. Replies: 3
    Last Post: 01-14-2002, 11:13 AM

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