Thread: Comparing integer value to array.

  1. #1
    Novice.
    Join Date
    Oct 2005
    Posts
    88

    Comparing integer value to array.

    Should the following code work:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int users[3]= {1, 2, 3};
      int user[8];
      int count;
      puts("Enter usernumber: ");
      scanf(" %d", &user);
      for (count = 0; count < 4; count++)
          {
           if (user == users[count])
              {
               puts("User found!");
              }
           }
    
      getchar();
      getchar();
      return 0;
    }
    Thank You.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    No.






    I suppose you want reasons? Fine:
    1. You only ever use user as a scalar (one integer), not an array of eight integers.
    2. You try to access users[3], which doesn't exist.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    When you code
    Code:
     scanf(" &#37;d", &user);
    you are passing the address of an address of an array of ints.
    Mainframe assembler programmer by trade. C coder when I can.

  4. #4
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I'm sorry. I started out with a program that compared strings, but then I changed my mind and thought I'd better try it out with integers first.

    One way or the other
    Code:
     scanf(" %d", &user);
    is neccesary for the program to function correcty.

  5. #5
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Then you'll have to rewrite scanf(), or, change the definition of variable user.

    Todd
    Mainframe assembler programmer by trade. C coder when I can.

  6. #6
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    What do you mean exactly?

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    scanf() is not designed to accept a pointer to a pointer. That's what you have coded.
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    If you want to refer to a particular element of the user array, you can code
    Code:
    scanf(" &#37;d", &user[3]) ;
    or some such index into the array.
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I don't see your point, as this is exactly the way the user is asked for input in the book I'm using now. It will only work with the Adress-of operator, somehow.

    I do see that &user should actually refer to the memory-adress of user.

    Rather odd this.

  10. #10
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, perhaps the BOOK MADE AN ERROR?

    Try this:
    Code:
      int user ;
    instead of int user[8];
    Mainframe assembler programmer by trade. C coder when I can.

  11. #11
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    I see that. So let me show you what I've got now:
    Code:
    #include <stdio.h>
    
    int main()
    {
      int users[3] = {1, 2, 3};
      int input;
      int count;
      puts("Enter user number: ");
      scanf(" %d", &input);
      for (count = 0; count < 4; count++)
          {
           if (input == users[count])
              {
               puts("User found.");
              }
           }
      getchar();
      getchar();
      return 0;
    }
    This works. The confusing thing for me now is the & in
    Code:
      scanf(" %d", &input);
    What is it doing there?

  12. #12
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    The & means "address of". Reading it like text, the scanf() says:

    Quote Originally Posted by scanf()
    Read data from stdin, into the address of variable input, put a blank in the first position and then format the rest of the data as an integer (because the user told me "%d")
    Mainframe assembler programmer by trade. C coder when I can.

  13. #13
    Novice.
    Join Date
    Oct 2005
    Posts
    88
    Well I still don't understand completely, but you have at least given me some indication of what it's for there, even though I'm not working with pointers.

    Thanks :-)

  14. #14
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Oh, but you ARE working with pointers!! Anytime you use an array, you are working with pointers.
    Mainframe assembler programmer by trade. C coder when I can.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Arrays are pointers? Hmmm
    Let's see it this way. If you give someone a copy of something (your homepage?) and they modify it, it won't affect your original, will it? No, I don't think so.
    However, if you were to give them the address of that homepage, then they could change your original homepage.

    That's the whole pointer ordeal. By using pointers, you pass the address and not a copy.
    But remember that you must not take the address of an array. The address of an array will be evaluated as a pointer to pointer (wrong).
    To take the address of something, you use the & operator.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No Match For Operator+ ???????
    By Paul22000 in forum C++ Programming
    Replies: 24
    Last Post: 05-14-2008, 10:53 AM
  2. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  5. Linked Lists Integer addition ? HELP Please??
    By green_eel in forum C Programming
    Replies: 3
    Last Post: 03-12-2003, 04:36 PM