Thread: comparison pointer and integer

  1. #16
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Elysia you know what are you doing but im not

    If you did similar things, just give me an idea or hint! ( ex: can I do it without using pointers )

    in my humble opinion, pointer is not for me

    Thanks in advance

  2. #17
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Arrays can act like pointers, but if you do it right, there will not be a problem. You must learn pointers sooner or later (or you can forget C altogether).
    To compare two strings (be they stored in pointer or arrays doesn't matter), use strcmp. As simple as that.
    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.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you print using printf() and %s format, you are not printing the address of the string, are you? If you do
    Code:
    printf("username = %p, usernameLoginP=%p", username, usernameLoginP);
    Do you still get the same value? Probably not. That is what "usernam == usernameLoginP" is comparing - and if they aren't the same in the printout, then they are also not the same in the comparison.

    strcmp() does something along the lines of:
    Code:
    int strcmp(char *s1, char *s2)
    {
        while(*s1 && *s1 == *s2)   // Exit when we hit a "zero", or when the characters are different.  
        {
           s1++;
           s2++;
        } 
        return *s2 - *s1;   // If they are different, return a signed indication of which is greater. 
    }
    This walks through the two arrays (or blocks of memory) until it finds either the end of the first string, or the strings are different (which also happens if the second string is shorter than the first string). That is different from comparing s1 with s2 - that's the location of the string.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #19
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    hımm I understood it now.
    Code:
    printf("usernameLoginP=%p\n",usernameLoginP);  //0040,,,,
             printf("username=%p\n",username);   //0022cc...
             if( strcmp(username, usernameLoginP  ) == 0 )  // comparing the addresses
             {
               printf("Correct!");
             }
    is it possible to convert username and usernameLoginP pointer to string
    I dont know to escape from pointer

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by plodos View Post
    hımm I understood it now.
    Code:
    printf("usernameLoginP=%p\n",usernameLoginP);  //0040,,,,
             printf("username=%p\n",username);   //0022cc...
             if( strcmp(username, usernameLoginP  ) == 0 )  // comparing the addresses
             {
               printf("Correct!");
             }
    is it possible to convert username and usernameLoginP pointer to string
    I dont know to escape from pointer
    A string in C is "a block of characters that end with a zero-value" - so a string is a pointer or array - and since arrays and pointers appear to be very similar in most aspects, you can most of the time use them interchangably [but beware that they ARE NOT THE SAME THING - just that they appear to be most of the time].

    You comment "// comparing the addresses" is incorrect - strcmp() is comparing the CONTENT of the two strings passed in (as the address of the first character).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM