Thread: comparison pointer and integer

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    10

    comparison pointer and integer

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    #include <ctype.h>
    #include <ctype.h>
    #include <time.h>
    #include <sys/types.h>
    
    char *password;
    char *username;
    char *path;
    char  usernameLogin;
    char  passwordLogin;
    
    int main ( void )
    {
    static const char filename[] = "ogr.txt";
    FILE *file = fopen ( filename, "r" );
    int i, j;
    char arra[128][128];
    char line[128];
    
    
        for(i=0; i<128; i++)
            for(j=0; j<128; j++)
            arra[i][j] = '\0';
    
        for(i=0; i<128; i++)
        line[i] = '\0';
    
        if ( file != NULL )
        {
           i=0;
    
            while ( fgets ( line, sizeof line, file ) != NULL )
            {
                strcpy(arra[i], line);
                i++;
                 username = arra[0];
                 password = arra[1];
                 path     = arra[2];
            }
          //  printf("username=%s password=%s path=%s ", username,password,path);
             printf("type username :"); scanf("%s",&usernameLogin); printf("\n");
             printf("type password :"); scanf("%s",&passwordLogin); printf("\n");
    
    
             if(usernameLogin==username &&  passwordLogin==password)
             {
               printf("Correct!");
             }
             else
             {
               printf("Wrong!");
             }       
         fclose ( file );
        }
        else
        {
            perror ( filename );
        }
    return 0;
    }
    ogr.txt
    Code:
    20009874 //uname
    9874        /pword
    desktop //path
    these are pointers which is getting the values from file
    username = arra[0];
    password = arra[1];
    path = arra[2];

    but usernameLogin and passwordLogin are not pointer
    if(usernameLogin==username && passwordLogin==password)
    {
    printf("Correct!");
    }
    else
    {
    printf("Wrong!");
    }

    how can I compare the pointer and normal variable ?
    I want to make read from file and login!

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    1) char is not a string. You cannot store a string in a char. A char contains a character, such as 's'. A string is an array of chars.
    2) I urgue you to read http://apps.sourceforge.net/mediawik...tle=Scanf_woes
    3) Realize that you are not copying the actual password or username with your assignment. You are assigning an address, so whenever you read new data from the file, username and password will lose its previous values.
    4) Use strcmp to compare strings.
    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. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by plodos
    but usernameLogin and passwordLogin are not pointer
    They should be arrays of char instead.

    The way I see it, you are overcomplicating things with your arra and line variables. You just need password, username, path, usernameLogin and passwordLogin to be arrays of char. They should also be local variables instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    But usernameLogin and passwordLogin are only one char each - and when compared to the pointer to char, that gets converted to an integer [char really is a integer of tiny size], so you ARE comparing usernameLogin (an integer) with username (a pointer).

    Even if both of them where pointers, you can not compare strings this way in C - you need to use strcmp().

    Why are you using global variables? [further, those global variables point to local variables - that is indeed a VERY good way to get very strange results if you ever do this in any function other than main - as the space used by local variables is freed when the function leaves, and later on will get used for other purposes].
    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.

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    thnx for urgent reply but the another problem is
    i dont know how to apply

    is it possible go get values from the file without using pointers?
    than I can compare only the strings ( i will change char to string for all of them )

    if its possible can u write the sample code for me :s

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You need to take a step back and learn C properly, or alternatively, switch to another language, before proceeding.
    Learn how to use strings, first of all.
    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.

  7. #7
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by plodos View Post
    if its possible can u write the sample code for me :s
    No, we do not do homework for you - see the
    http://cboard.cprogramming.com/annou...t.php?f=4&a=39

    --
    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.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    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.

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    Code:
    char  usernameLogin[10];
    char  passwordLogin[10];
    char *usernameLoginP;
    char *passwordLoginP;
    
    //same code
    
             printf("type username :"); scanf("%s",&usernameLogin); printf("\n");
             printf("type password :"); scanf("%s",&passwordLogin); printf("\n");
             
             usernameLoginP = &usernameLogin;
             passwordLoginP = &passwordLogin;
    
             // pointer to pointer
             if(*usernameLoginP==username &&  passwordLogin==password)
    cannot convert `char (*)[10]' to `char*' in assignment ... Why ?

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Because arrays decay to a pointer to the first element. You are trying to assign a pointer to an array, which is different.

    usernameLoginP = usernameLogin;
    passwordLoginP = passwordLogin;

    This will work.
    You must still read http://apps.sourceforge.net/mediawik...tle=Scanf_woes
    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.

  11. #11
    Registered User
    Join Date
    Dec 2007
    Posts
    10
    thank you but :s
    Code:
     usernameLoginP = usernameLogin;
             passwordLoginP = passwordLogin;
    
             printf("usernameLoginP=%s\n",usernameLoginP); // usernameLoginP=20009874
             printf("username=%s\n",username);                      // usernameLogin=20009874
             if(usernameLoginP==username )
             {
               printf("Correct!");
             }
             else
             {
               printf("Wrong!");
             }
    it return wrong! but the values are correct, WHY the comparision is not working ?

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by plodos View Post
    thank you but :s
    Code:
     usernameLoginP = usernameLogin;
             passwordLoginP = passwordLogin;
    
             printf("usernameLoginP=%s\n",usernameLoginP); // usernameLoginP=20009874
             printf("username=%s\n",username);                      // usernameLogin=20009874
             if(usernameLoginP==username )
             {
               printf("Correct!");
             }
             else
             {
               printf("Wrong!");
             }
    it return wrong! but the values are correct, WHY the comparision is not working ?
    Because you are still not using strcmp(). You are checking if userNameLoginP has the same address as username - which obviously would not be the case, as they are distinct different arrays.

    --
    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.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    When comparing arrays, the array turns into a pointer to the first element and when comparing to a pointer, well, you are comparing its contents - an address!
    So unless the both sides have the same contents (ie point to the same place, same value)...
    Well, you get what I'm getting at, right?
    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
    Dec 2007
    Posts
    10
    but the outputs are same and decimal
    Code:
    printf("usernameLoginP=%s\n",usernameLoginP); // usernameLoginP=20009874
             printf("username=%s\n",username);                      // usernameLogin=20009874
    strcmp();
    Code:
    if( strcmp(username, usernameLoginP  ) == 0 )
             {
               printf("Correct!");
             }
    I lost myself

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    But basically you are comparing two pointers. Pointers store a memory address. In your case, those pointers would point to the same value, but at two different locations in memory.
    And as pointers are variables, the contents of those variables are compared (and pointers contains memory addresses!), so since the contents of the pointers is not the same, the comparison is false!
    That is why there is such a function as strcmp.
    C was made to be efficient, not programmer friendly.
    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. 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