Thread: dereferencing pointer to incomplete type

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    3

    dereferencing pointer to incomplete type

    im getting the error on the threads name whenever a try to access an attribute of a struct

    here is the relevant code

    the structure
    Code:
    typedef struct logrw_struct_ {
        
        unsigned int num_reg, num_bks, bks_sz;
        unsigned int pos_livre;
        
        blocks_t* bks_a, *bks_i;
        blocks_t* regs[0];
    }logrw_struct_t;


    initializing the structure
    Code:
    logrw_struct_t* logrw_init(unsigned int num_reg, unsigned int array_dim, unsigned int bloco_dim) {
        
        int i;
        
        logrw_struct_t *log = (logrw_struct_t*) 
            malloc(sizeof(logrw_struct_t) + num_reg);
        
        log->num_reg = num_reg;
        log->num_bks = array_dim;
        log->bks_sz = bloco_dim;
        
        log->bks_a = block_new(array_dim, bloco_dim);
        log->bks_i = block_new(array_dim, bloco_dim);
    
        for(i = 0; i<num_reg; i++){
            log->regs[i] = log->bks_a;
        }
        log->pos_livre = i;
        
        return log;
    }
    the main where i get the error
    Code:
    int main(){
        
        logrw_struct_t *log;
        log = logrw_init(2, 12, 4);
        printf("registos->%d\nnum_blocos->%d\n", log->num_registos, log->num_blocos);
                     
        return 0;
    }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    The point of incomplete types is to create opaque interfaces which hide implementation detail.

    Your logrw API needs to provide some get methods, so you can write
    Code:
    printf("registos->%d\nnum_blocos->%d\n", logrw_getRegs(log), logrw_getBlocks(log) );
    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.

  3. #3
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    but there is no way of acessing directly to the log values, with the ->?

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    3
    but there is no way of acessing directly to the log values, with the ->?
    because i have another API thats supposed to receive blocks_t* and returns the number of blocks, but if i make

    Code:
    printf("registos->%d\nnum_blocos->%d\n", logrw_getRegs(log), logrw_getBlocks(log->bks_a) );
    it gives the same error

  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
    > but there is no way of acessing directly to the log values, with the ->?
    Yes, you could include the header file containing the struct declaration, but that would defeat the whole point.

    > printf("registos->%d\nnum_blocos->%d\n", logrw_getRegs(log), logrw_getBlocks(log->bks_a) );
    > it gives the same error
    Of course it would, you still have log->someMember in there.
    You can ONLY use log here in main, not log->someMember.
    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. Another 'dereferencing pointer to incomplete type'
    By thealmightyone in forum C Programming
    Replies: 7
    Last Post: 11-25-2010, 08:49 PM
  2. dereferencing pointer to incomplete type
    By SasDutta in forum C Programming
    Replies: 2
    Last Post: 07-28-2010, 09:32 AM
  3. dereferencing pointer to incomplete type
    By johnny_ in forum C Programming
    Replies: 4
    Last Post: 03-01-2010, 10:46 AM
  4. error: dereferencing pointer to incomplete type
    By nasim751 in forum C Programming
    Replies: 2
    Last Post: 04-18-2008, 12:59 AM
  5. dereferencing pointer to incomplete type
    By kataya in forum C Programming
    Replies: 2
    Last Post: 04-16-2008, 01:37 AM