Thread: Array of structers

  1. #1
    Registered User
    Join Date
    Nov 2012
    Posts
    41

    Array of structers

    In my book a question is like below:
    Match the following with reference to the following program segment:
    Code:
    struct
    {
        int x, y;
    }s[]={10, 20,15,25,8,75,6,2};
    int *i;
    i=s;

    1 *(i+3) a 85
    2 s[i[7]].x] b 2
    3 s[(s+2)->y/3[I]].y c 6
    4 i[i[1]-i[2]] d 7
    5 i[s[3].y] e 16
    6 (s+1)->x+5 f 15
    7 *(1+i)**(i+4)/*i g 25
    8 s[i[0]-i[4]].y+10 h 8
    9 (*(s+*(i+1)/*i)).x+2 i 1
    10 ++i[i[6]] j 100
    11 k 10
    12 l 20



    My doubts are, is it to declare *i as int and assign like i=s;
    and division with pointer gives error in compiler.
    Last edited by justine; 11-27-2012 at 02:26 AM.

  2. #2
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by justine View Post
    My doubts are, is it to declare *i as int and assign like i=s;
    This is fine - it's called pointer decay. In this case, use of the variable "s" (which has an array type) decays to a pointer to that array.


    Quote Originally Posted by justine View Post
    and division with pointer gives error in compiler.
    On one hand, this is how the compiler should behave - division with a pointer is not a thing you can do. However, I don't see any division involving pointers - what am I missing?
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    The assignment "i = s" is invalid, since i is a pointer to int, but s is an array of a struct type.

    A compiler will therefore reject the code, and all of the entries in the table are therefore meaningless.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    However, I don't see any division involving pointers - what am I missing?
    Code:
    *(1+i)**(i+4)/*i
    (*(s+*(i+1)/*i)).x+2
    A compiler will therefore reject the code
    It is not rejecting, but gives warning.
    untitled1.c:9:3: warning: assignment from incompatible pointer type [enabled by default]
    Last edited by justine; 11-27-2012 at 07:45 AM.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    200
    Quote Originally Posted by justine View Post
    Code:
    *(1+i)**(i+4)/*i
    (*(s+*(i+1)/*i)).x+2
    You're not dividing pointers here - the first involves "/*i", which is a pointer that's dereferenced to an int, and in the second operator precedence means you're adding "s" to "*(i+1)/*i".

    But grumpy's already given you the reason that the compiler's rejecting this code.
    Programming and other random guff: cat /dev/thoughts > blogspot.com (previously prognix.blogspot.com)

    ~~~

    "The largest-scale pattern in the history of Unix is this: when and where Unix has adhered most closely to open-source practices, it has prospered. Attempts to proprietarize it have invariably resulted in stagnation and decline."

    Eric Raymond, The Art of Unix Programming

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    As long as the first line is corrected to i = (int *)&s, the answers are: 25, 8, 2, 75, 15, 20, 16, 85, 10, 2.

  7. #7
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Code:
    #include <stdio.h>
    int main()
    {
         struct
         {
             int x, y;
         }s[]={
             {10, 20},
             {15,25},
             {8,75},
             {6,2}
             };
             int *i;
             i=(int*)&s;
             printf("%d", *(1+i)**(i+4)/*i);
             return 0;
    }
    Still rejecting:
    gcc -Wall -o "untitled1" "untitled1.c" (in directory: /home/duh/Desktop/C)
    untitled1.c: In function ‘main’:
    untitled1.c:15:36: error: unterminated comment
    untitled1.c:15:10: error: expected ‘)’ at end of input
    untitled1.c:15:10: error: expected declaration or statement at end of input
    untitled1.c:15:10: warning: control reaches end of non-void function [-Wreturn-type]
    Compilation failed.
    How to fix it?
    Thanks

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    That is because the compiler interpreted /* as the start of a comment. You should space out your code, e.g.,
    Code:
    #include <stdio.h>
    
    int main(void)
    {
        struct
        {
            int x, y;
        } s[] = {{10, 20}, {15,25}, {8,75}, {6,2}};
        int *i;
        i = (int*)&s;
        printf("%d", *(1 + i) * *(i + 4) / *i);
        return 0;
    }
    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

  9. #9
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Ok this is working

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    By the way, instead of "array of structers", we would normally say "array of objects of a struct type" which may be abbreviated to "array of struct objects" or even "array of structs".
    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

  11. #11
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Here what is the use of "s", I think s[0] is the base address.

    Edit: Nope 's' is, How did that happen. Is it two dimensional?
    Last edited by justine; 11-27-2012 at 08:30 PM.

  12. #12
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Quote Originally Posted by laserlight View Post
    By the way, instead of "array of structers", we would normally say "array of objects of a struct type" which may be abbreviated to "array of struct objects" or even "array of structs".
    So there is other thing with name array of structers?

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by justine
    Here what is the use of "s", I think s[0] is the base address.

    Edit: Nope 's' is, How did that happen. Is it two dimensional?
    s is not an address and neither is s[0]. s is an array, s[0] is a struct object, where the struct type is anonymous. The notation that you are looking at is used to initialise an aggregate, in this case an array of struct objects. It happens to be the same notation that you might use to initialise a 2D array, but that does not mean that s is a 2D array.

    Quote Originally Posted by justine
    So there is other thing with name array of structers?
    Not that I know of.
    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

  14. #14
    Registered User
    Join Date
    Nov 2012
    Posts
    41
    Thanks laserlight

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by laserlight View Post
    That is because the compiler interpreted /* as the start of a comment. You should space out your code
    Personally, I prefer adding brackets.

    Instead of "*(i + 4) / *i" I might write "(*(i + 4))/(*i)". I prefer the visual cue of additional brackets over whitespace (which can work by absence of visual artefact). It also is slightly less likely to be broken by tools that do automated reformatting/indenting.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-20-2012, 08:41 AM
  2. pointers/structers/if statements
    By vjefcoat in forum C Programming
    Replies: 6
    Last Post: 11-26-2010, 05:27 PM
  3. Replies: 9
    Last Post: 08-23-2010, 02:31 PM
  4. Structers
    By rhouli67 in forum C Programming
    Replies: 1
    Last Post: 03-17-2009, 07:36 AM
  5. Replies: 6
    Last Post: 11-09-2006, 03:28 AM