Thread: dereferencing pointer to incomplete type

  1. #1
    Registered User
    Join Date
    Mar 2010
    Posts
    2

    dereferencing pointer to incomplete type

    how can i fix the following? i get "dereferencing pointer to incomplete type" on compilation inside the func()

    Code:
    #include <stdio.h>
    
    void func(char *buf) {
    	printf("x func: %d\n", (*(struct s1 *)buf).x);
    }
    
    
    int main() {
    	struct s { int x; };
    
    	struct s s1;
    	s1.x = 5;
    	printf("x main: %d\n", s1.x);
    	func((char *)&s1);	
    
    	return 0;
    }

  2. #2
    Registered User
    Join Date
    Feb 2010
    Posts
    57

    Re: dereferencing pointer to incomplete type

    You can convert directly that character pointer into integer,.

    Code:
    void func(char *buf) {
            printf("x func: %d\n", *((int *)buf));
    }

  3. #3
    Registered User
    Join Date
    Mar 2010
    Posts
    2
    but you didn't refer to the int value inside the struct.
    it's just a portion of something i would like to achieve.
    here is another example.

    file: tester.c
    Code:
    #include <stdio.h>
    #include <string.h>
    //#include "z.h"
    #include "/usr/include/netinet/ip.h"
    
    void func(struct ip *ip) {
    	printf("ip_ttl func: %d\n", (*(struct ip *)ip).ip_ttl);
    }
    
    int main() {
    
    	struct ip tmp;
    	memset(&tmp, 0, sizeof(struct ip));
    	tmp.ip_ttl = 0x4;
    
    	char buf[sizeof(struct ip)];
    	memset(buf, 0, sizeof(struct ip));
    	memcpy(buf, &tmp, sizeof(struct ip));
    
    	printf("ip_ttl main: %d\n", tmp.ip_ttl);
    	printf("ip_ttl main: %d\n", (*(struct ip *)buf).ip_ttl);
    
    	func((struct ip *)&tmp);
    	func((struct ip *)buf);
    
    	return 0;
    }
    and the output for it:
    # ./tester
    ip_ttl main: 4
    ip_ttl main: 4
    ip_ttl func: 4
    ip_ttl func: 4


    now modifying it to:
    file: tester.c
    Code:
    #include <stdio.h>
    #include <string.h>
    #include "z.h"
    /*#include "/usr/include/netinet/ip.h"
    
    void func(struct ip *ip) {
    	printf("ip_ttl func: %d\n", (*(struct ip *)ip).ip_ttl);
    }*/
    
    int main() {
    
    	struct ip tmp;
    	memset(&tmp, 0, sizeof(struct ip));
    	tmp.ip_ttl = 0x4;
    
    	char buf[sizeof(struct ip)];
    	memset(buf, 0, sizeof(struct ip));
    	memcpy(buf, &tmp, sizeof(struct ip));
    
    	printf("ip_ttl main: %d\n", tmp.ip_ttl);
    	printf("ip_ttl main: %d\n", (*(struct ip *)buf).ip_ttl);
    
    	func((struct ip *)&tmp);
    	func((struct ip *)buf);
    
    	return 0;
    }
    file: z.h
    Code:
    #ifndef Z_H
    #define Z_H
    
    #include "/usr/include/netinet/ip.h"
    
    void func(struct ip *ip);
    
    #endif
    file: z.c
    Code:
    #ifndef Z_C
    #define Z_C
    
    #include "z.h"
    #include <stdio.h>
    
    void func(struct ip *ip) {
    	printf("ip_ttl func: %d\n", (*(struct ip *)ip).ip_ttl);
    }
    
    #endif
    the output now is the same as above.
    but, when i try to do the same thing around other program of mine. it gives me the "dereferencing pointer to incomplete type".

    SHOULD I PAST MY ORIGINAL CODE?
    WILL SOMEONE BE ABLE TO HELP?

  4. #4
    Registered User
    Join Date
    Apr 2009
    Posts
    66
    If you want to get using struct
    then try this
    Code:
     printf("x func: %d\n",*(struct s *)buf);
    This will work when the structure has only one member ,

    since we are dealing with the reference we must use this one,
    Code:
            printf("x func: %d\n",((struct s *)buf)->x);
    The main issue is if you want to dereference in the function you must be defining the structure outside the main
    function , since you are actually facing a scope related issues.
    Last edited by Alexander jack; 03-01-2010 at 04:59 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > You can convert directly that character pointer into integer,.
    Look up what an "alignment exception" is some time, otherwise known as a bus error.

    > it gives me the "dereferencing pointer to incomplete type".
    So include some header file that declares struct s1
    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. error: dereferencing pointer to incomplete type
    By kiros88 in forum C Programming
    Replies: 4
    Last Post: 09-16-2009, 02:43 PM
  2. error: dereferencing pointer to incomplete type
    By surlyTomato in forum C Programming
    Replies: 10
    Last Post: 08-22-2009, 08:04 AM
  3. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  4. Replies: 5
    Last Post: 04-04-2009, 03:45 AM
  5. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM