question about struct

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kalar
    New Member
    • Aug 2007
    • 82

    question about struct

    In an example i found this
    We have this struct:
    [PHP]struct catalog {
    char name[80];
    char title[80];
    char pub[80];
    unsigned date;
    unsigned char ed;
    } cat[MAX];
    [/PHP]

    When we fill the cat[i].name i saw this
    [PHP]
    for(i=0 i<MAX; i++)
    printf("Enter name..")
    gets(cat[i].name);
    if(!*cat[i].name) break;/* what this command do? I mean the !* ?[/PHP]
    What is !* ?
    Thanks in advance
  • ahoyer
    New Member
    • Aug 2007
    • 14

    #2
    Originally posted by kalar
    When we fill the cat[i].name i saw this
    [PHP]
    for(i=0 i<MAX; i++)
    printf("Enter name..")
    gets(cat[i].name);
    if(!*cat[i].name) break;/* what this command do? I mean the !* ?[/PHP]
    What is !* ?
    Hey im not too familiar with php or anything, but the ! is the not operator for conditional statements, as far as the * i think it might be dereferencing a member of the struct, just written unclearly.

    so what i think theyre doing is just checking to see if the value that the user entered is null, and if it is then kick out of the loop.

    thats my best guess anyways.

    Comment

    • weaknessforcats
      Recognized Expert Expert
      • Mar 2007
      • 9214

      #3
      cat is an array of catalog struct variables.
      cat[i] is one of those array elements.
      cat[i].name is an array of char that is a member of one of those elements.

      In C/C++, the name of an array is the address of element 0. Therefore:

      cat[i].name is the address of element 0 of the char name array.

      Therefore:

      *cat[i].name is element 0 of the name array

      Therefore:
      !*cat[i].name is true if element 0 of the name array is false.

      Element 0 of the name array is false if it is a '\0'.

      Therefore:

      !*cat[i].name is true of the name is a null string.

      This could also have been written as:

      if (cat[i].name[0] == '\0') break;

      Comment

      • kreagan
        New Member
        • Aug 2007
        • 153

        #4
        Originally posted by kalar
        In an example i found this

        When we fill the cat[i].name i saw this
        [PHP]
        for(i=0 i<MAX; i++)
        printf("Enter name..")
        gets(cat[i].name);
        if(!*cat[i].name) break;/* what this command do? I mean the !* ?[/PHP]
        What is !* ?
        Thanks in advance
        The question should probably be: What is "!" and what is "*". It might make more sense to write that line:

        [CODE=PHP]if ( !(*cat[i].name)) break;[/CODE]

        The "!" means not and the * is most likely dereferencing a pointer. If there is no value in cat[i].name, then break.

        This is only a guess. Some languages see primatives (int, char) as "TRUE". So if there is no value in cat[i].name, then *cat[i].name should return NULL. NULL is FALSE. Therefore !NULL = TRUE.

        Hope this helps.

        Comment

        • kalar
          New Member
          • Aug 2007
          • 82

          #5
          I also think about pointers but. When we have as example an array p[i] and we use a pointer pp for the array, we don't say *pp[i] but only *pp.Am i wrong?


          edit:Maybe help you that the programm says that if we enter an empty line in the name breaks

          Comment

          • kreagan
            New Member
            • Aug 2007
            • 153

            #6
            Originally posted by kalar
            I also think about pointers but. When we have as example an array p[i] and we use a pointer pp for the array, we don't say *pp[i] but only *pp.Am i wrong?


            edit:Maybe help you that the programm says that if we enter an empty line in the name breaks
            Are you sure *pp.Am is how you use pointers for an array? With *pp[i].Am, you are stating ... get the value of AM of the ith element of pp.

            pp.Am doesn't really make sense to me because there are multiple Ams. I could be mistaken though.

            Comment

            • JosAH
              Recognized Expert MVP
              • Mar 2007
              • 11453

              #7
              Did anyone actually *read* post #3? WeaknessForCats actually explained what
              that expression is all about. No need for guessing anymore.

              kind regards,

              Jos

              Comment

              Working...