Thread: Dereferencing multiple levels of struct ptrs without ->

  1. #1
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137

    Dereferencing multiple levels of struct ptrs without ->

    Can someone correct my syntax? I'm trying to write the ->next->next->next part but with (*next).next notation instead of ->.

    Code:
    typedef struct mathematics {
     math *func[5]; // BASICALLY THESE TWO ARE THE SAME
     P_MATH func2[5];
     int (*myOwnMath)(int, int, int, int);
     struct mathematics *next;
    } MATH_TYPE;
    //THE BELOW LINE
     pMyMT->next->next->next = pMyMT4;
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    e.g.

    Code:
    .c:37:11: error: expected identifier before '(' token
      (*pMyMT).(*next).(*next).next = pMyMT4;
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  3. #3
    Registered User
    Join Date
    Jun 2009
    Posts
    120
    Code:
    (*(*(*pMyMT).next).next).next = pMyMT4;

  4. #4
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Quote Originally Posted by DRK View Post
    Code:
    (*(*(*pMyMT).next).next).next = pMyMT4;
    You are God.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    I hope this was curiosity, and not some actual desire to write code like that.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Old Fashioned
    Join Date
    Nov 2016
    Posts
    137
    Quote Originally Posted by Salem View Post
    I hope this was curiosity, and not some actual desire to write code like that.
    Salem, 99% of stuff with me is curiosity...

    My actual work code is much more boring, don't worry :P

    No seriously though, I often do experiments with extremely ugly syntax solely as an exercise to not just learn the ins and outs of the language, but also to train my mind to be able to understand it for when I'm handed crappy code or when I'm reverse engineering... Look up Hex-Rays IDA Pro decompiler output and you'll see what I mean :P
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  7. #7
    Registered User
    Join Date
    May 2016
    Posts
    104
    Quote Originally Posted by Salem View Post
    I hope this was curiosity, and not some actual desire to write code like that.
    I actually prefer this syntax over the standard ->. It shows clearly what you are trying to do and it is consistent with the rest of the language; not at all difficult to understand as so many proclaim.

    IMO the only benefit -> has, is that it is more convenient to type. That in itself may be a good reason to use it, but I prefer consistency and uniformity over convenience; otherwise I'd use c++, which probably has the ugliest and most inconsistent syntax of any language. C++11 and later makes me want to puke.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Do you also use *(arr+n) instead of arr[n] for the same reason?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reason behind multiple levels of header files?
    By Leam in forum C Programming
    Replies: 6
    Last Post: 01-24-2016, 10:10 AM
  2. Replies: 1
    Last Post: 12-23-2015, 07:35 AM
  3. Dereferencing a struct
    By jbarke12 in forum C Programming
    Replies: 4
    Last Post: 05-02-2013, 11:32 AM
  4. Replies: 6
    Last Post: 09-30-2010, 12:24 AM
  5. struct ptrs to struct ptrs
    By lolguy in forum C Programming
    Replies: 5
    Last Post: 02-24-2009, 12:02 AM

Tags for this Thread