Thread: pointers and structures

  1. #1
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90

    pointers and structures

    hi

    what i wanna do is to insert an element in a binary search tree. this element is a structure with a field called key (which is a pointer to char). this shall be everytime the same. but i wanna be able to "atach" a body which can be anything i'd like to (int, struct, char ....). so i thought defining it like this would do the job:

    Code:
    struct streenode {
    	char 				*key;
    	void				*body;
    	struct streenode 	*nextl,*nextr;
    	};
    then i defined another structure for a body:

    Code:
    struct optionflagbody {
    		int status;
    	};
    now i wrote this code sequence:
    Code:
    struct options readcfg(char *cfgfile)
    {
    	struct optionflagbody *body;
    
    
    	struct options optns;
    	struct streenode keyword, *optionflags;
    	
    	[...]
    	
    	body = (struct optionflagbody *)malloc(sizeof(struct optionflagbody));
    	(*body).status = ON;
    
    	keyword.key = "Test";
    	keyword.body = body;
    
                    printf("%d",keyword.body->status);
    
    	optionflags = stinsert(optionflags,keyword);
    	
    	[...]
    }
    printf("%d",keyword.body->status);

    in the bold section the compiler aborts with the message: left operand of -> has incompatible type void
    operand of = have illegal types pointer to void and void

    any idea?

    stormbringer
    Last edited by stormbringer; 07-24-2002 at 12:20 AM.

  2. #2
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    well stormbringer, your problem is that you wanna print a pointer to void that doesn't make sense to the compiler. this will help you:

    Code:
    body = (struct optionflagbody *)malloc(sizeof(struct optionflagbody));
    	(*body).status = ON;
    	printf("%d",body->status);
    	keyword.key = "Test";
    	keyword.body = body;
    
    	body = NULL;
    
    	if(body == NULL)
    		printf("body is null\n");
    
    	body = keyword.body;
    	printf("%d",body->status);
    have fun

    stormbringer

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    keyword.key = "Test";
    You need to allocate space for this and use strcpy().

    keyword.key = malloc ( (sizeof char) * 5);
    strcpy(keyword.key, "Test");


    Oh but it has been so long since I used pointers. I may be wrong in some of the details.
    Last edited by Troll_King; 07-24-2002 at 01:20 AM.

  4. #4
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    printf("%d",keyword.body->status);
    And this might not be possible unless struct optionflagbody is embedded into struct streenode.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    39
    Originally posted by Troll_King

    You need to allocate space for this and use strcpy().

    keyword.key = malloc ( (sizeof char) * 5);
    strcpy(keyword.key, "Test");
    Actually, the compiler allocates space for it. Only, you wouldn't be able to modify the string.

    Just like saying:
    Code:
    char *ptr = "Hello World";
    printf( "string is: %s\n", ptr );
    As pointed out earlier, the problem is that the struct streenode has the member: "body" defined as pointer to void. So you need to typecast it before using it as a pointer to some other data-type (another structure in this case).

    Hope this helps.
    <Signature
    name="Ruchikar"
    quote="discussions are forgotten, only code remains"/>

  6. #6
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    same topic as above, but different problem

    as you see i give the structure which contains a body (void ptr) to a function (which creates a tree). now this function shall copy the structure so the user can't modify it (that means that all the nestings must be copyed too (the body, because the programmer that uses the function could still have a pointer to the body and manipulate it from outside the function.

    how can i doo that?

    stormbringer

  7. #7
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Apparently you can not dereference a void * as they say, (without casting).

    A pointer to a function returning what, a void pointer? Say again.

    void (* funct)() *;

  8. #8
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    What about the 'const' keyword? Makes the pointer read only. Still can't quite figure out what you are trying to do but nobody else seems to have any opinions.

  9. #9
    Registered User stormbringer's Avatar
    Join Date
    Jul 2002
    Posts
    90
    i have this struct:

    Code:
    struct streenode {
    	char 				*key;
    	void				*body;
    	struct streenode 	*nextl,*nextr;
    	};
    and i give it to a function. this function shall copy the structure with the content of body. it shall not just copy it and let body point to the same memory location.

    in other words it shall allocate space for body (which can be a int,struct, anything...). then copy body to newbody. then copy the old struct to the new and adjust the pointer to the new body location.

    i hope i made myself clear, or not?

  10. #10
    Sayeh
    Guest
    Why don't you alter your typedef-- it might help the compiler understand what you're trying to do better:

    Code:
    typedef struct streenodeStruc
       {
       char 				*key;
       void				*body;
       struct streenodeStruc 	                *nextl,*nextr;
       }streenode;
    
    typedef struct
       {
       int                                                         status;
       }optionflagbody;
    ---

    Other than that, your mode of speech is what has people confused. What your trying to do is simple enough. the problem is you are trying to avoid using coercion. You can't. You have to do type coercion in order to make the code pass the syntax checker in the compiler.

    'void*' is a special type, and not really meant to be used the way you're using it. It's no dereferencable, because it is 'void'-- unknown size/shape, so it cannot be dereferenced.

    If you want a generic pointer type, then create your own universal header with a typedef like this:

    Code:
    typedef ptr   *char;                          /* generic pointer */
    You could then use 'ptr' wherever you like and coerce it to whatever you need.

  11. #11
    Sayeh
    Guest
    Sorry, slight dislexic there, I got that backwards. Just reverse the two and it will be fine.

    I supposed, if I were to become an "official" member I could edit my posts, right?

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > I supposed, if I were to become an "official" member I could edit my posts, right?
    We would surely enjoy the vault of knowledge you seem to reach into each and every post.

    [EDIT]
    And yes, your assumption is correct to clarify.
    [/EDIT]
    The world is waiting. I must leave you now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. vector of arrays of pointers to structures
    By Marksman in forum C++ Programming
    Replies: 13
    Last Post: 02-01-2008, 04:44 AM
  2. Structures, and pointers to structures
    By iloveitaly in forum C Programming
    Replies: 4
    Last Post: 03-30-2005, 06:31 PM
  3. structures with pointers to structures
    By kzar in forum C Programming
    Replies: 3
    Last Post: 11-20-2004, 09:32 AM
  4. pointers to arrays of structures
    By terryrmcgowan in forum C Programming
    Replies: 1
    Last Post: 06-25-2003, 09:04 AM
  5. Freeing pointers in structures
    By jim50498 in forum C Programming
    Replies: 4
    Last Post: 03-08-2002, 12:53 PM