Thread: request for member _mp_size in something not a structure or union

  1. #1
    Registered User
    Join Date
    Aug 2019
    Posts
    2

    request for member _mp_size in something not a structure or union

    Hello all,

    I have a problem with a C program using GMP.
    I boiled the issue down to the following:
    Code:
    #include <stdio.h>
    #include <gmp.h>
    
    int main(void) {
      mpz_t foo;
      void *ptrfoo;
    
      mpz_init(foo);
    
      if(foo->_mp_size == 0) {
        printf("foo is ZERO\n");
      }
      else {
        printf("foo is not ZERO\n");
      }
    
      ptrfoo = foo;
      if(((mpz_t *)ptrfoo)->_mp_size == 0) {
        printf("foo is ZERO\n");
      }
      else {
        printf("foo is not ZERO\n");
      }
    
      return (0);
     }
    The error request for member _mp_size in something not a structure or union occurs on the second if statement.
    Any idea?
    Thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    foo->_mp_size worked, with foo being an mpz_t. You then tried ((mpz_t *)ptrfoo)->_mp_size, and now the expression on the left of ->_mp_size is an mpz_t*, not an mpz_t. Therein lies your mistake. You should have written (*(mpz_t *)ptrfoo)->_mp_size
    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

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Quote Originally Posted by Over_score View Post
    The error request for member _mp_size in something not a structure or union occurs on the second if statement. Any idea?
    The error:
    Code:
    test.c:18:30: error: request for member ‘_mp_size’ in something not a structure or union
       if ( ( ( mpz_t * ) ptrfoo )->_mp_size == 0 )
                                  ^~
    You should write:
    Code:
    if ( ( * ( mpz_t * ) ptrfoo )->_mp_size == 0 ) ...

  4. #4
    Registered User
    Join Date
    Aug 2019
    Posts
    2
    Many thanks laserlight and flp1969

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    A common convention is that symbols which begin with underscores (like _mp_size) should NOT be used in your code.

    Sure, you can see that member, and you can tell the compiler to access it, but that doesn't mean you should use it.

    If this were a C++ class and not a C struct, it would be declared private and you would be FORCED to use the appropriate member access function.
    ftp://ftp.gnu.org/old-gnu/Manuals/gm...p_6.html#SEC33

    You should only access your mpz_t variables through the published API.
    Picking away at the innards risks breaking something, or being broken if the GMP team decide to call that member something else.
    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
    Registered User
    Join Date
    May 2012
    Location
    Arizona, USA
    Posts
    948
    Since mpz_t is already a pointer (otherwise foo->_mp_size would not compile), shouldn't it be just ((mpz_t)ptrfoo)->_mp_size?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 02-04-2011, 10:07 AM
  2. Replies: 2
    Last Post: 11-10-2010, 05:04 PM
  3. Replies: 4
    Last Post: 09-04-2010, 09:12 AM
  4. Replies: 1
    Last Post: 05-18-2010, 04:14 AM
  5. Replies: 1
    Last Post: 04-07-2008, 04:08 PM

Tags for this Thread