Comparing integer value to array.

This is a discussion on Comparing integer value to array. within the C Programming forums, part of the General Programming Boards category; Should the following code work: Code: #include <stdio.h> int main() { int users[3]= {1, 2, 3}; int user[8]; int count; ...

  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,185
    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
    Katy, Texas
    Posts
    2,309
    When you code
    Code:
     scanf(" &#37;d", &user);
    you are passing the address of an address of an array of ints.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Then you'll have to rewrite scanf(), or, change the definition of variable user.

    Todd
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    scanf() is not designed to accept a pointer to a pointer. That's what you have coded.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    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.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Well, perhaps the BOOK MADE AN ERROR?

    Try this:
    Code:
      int user ;
    instead of int user[8];
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    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")
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  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
    Katy, Texas
    Posts
    2,309
    Oh, but you ARE working with pointers!! Anytime you use an array, you are working with pointers.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,543
    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.

Page 1 of 2 12 LastLast
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, 09: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, 03:36 PM

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