Why my if doesn't work?

This is a discussion on Why my if doesn't work? within the C Programming forums, part of the General Programming Boards category; Code: scanf("%s",&data); while ((fgets(test_line,512,eoftest))!=NULL) { if(Contactos[z].data==data) { printf("test"); } z++; } Hey guys I am having a problem this isn't ...

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

    Why my if doesn't work?

    Code:
    scanf("%s",&data);
       while ((fgets(test_line,512,eoftest))!=NULL)
       {
    
          if(Contactos[z].data==data)
          {  
             
             printf("test");
          }
          z++;
       }
    Hey guys I am having a problem this isn't working what's up?
    I have tested the data with a printf before the if and I have tested the z with a printf as well and I have tested the Contactos[z].data with a printf

    The results of my testes are Contactos[1].data=27.05.88
    data=27.05.88
    z=0,1,2,3,4
    why doesn't it write what I ask it to in the printf
    This is: test. Contactos[1].data is the same as data shouldn't it be printing test??

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Because data and Contactos[1].data are pointers. Even though the things they point to are equal, they do not "live" in the same place, so their addresses are different. To compare strings, use strcmp.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    More code is needed to tell.

    if data is a pointer, scanf("%s",&data); is wrong.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by robwhit View Post
    More code is needed to tell.

    if data is a pointer, scanf("%s",&data); is wrong.
    Ooh, good point. Although, when it comes down to it, what declaration would make that statement right? (If &data is a char *, then data is a char, but how do I know that I have a pile of memory right behind it that I can write into? A union, I guess; I don't see anything else.)

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    9

    Thanks

    It worked fine with strcmp ;-)

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by tabstop View Post
    Ooh, good point. Although, when it comes down to it, what declaration would make that statement right? (If &data is a char *, then data is a char, but how do I know that I have a pile of memory right behind it that I can write into? A union, I guess; I don't see anything else.)
    I guess anything with sizeof(data) >= 2. But not with type correctness. not sure if you can overwrite a union like that.

  7. #7
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    scanf("%s",&data);
    Since OP had confirmed that strcmp had worked , it should definitely be the char * pointer. Which he should seriously has to change the way he is calling the scanf function.

    ssharish

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Of course, using scanf with %s is unsafe as it stands, anyway. At least use %20s (or whatever your size is), or go with fgets.
    Last edited by tabstop; 01-13-2008 at 07:21 PM. Reason: Wrong button

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Perhaps, i can see that he is using scanf and fgets to read string, which makes me wonder what is he trying to do. He should be knowing the consequences of using scanf function on reading string.

    OP: What is it are you trying to achieve?

    ssharish

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,295
    I get the feeling Elysia would have a field day with this one.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,452
    Read this
    Need help with altering info on a txt file
    regarding your scanf, please.
    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. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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