'unlegitimate' dereferencing void* pointer warning

This is a discussion on 'unlegitimate' dereferencing void* pointer warning within the C Programming forums, part of the General Programming Boards category; Hello people ! I just subscripted to submit my question, but I'm not new to this forum. Actually I've been ...

  1. #1
    Registered User
    Join Date
    Aug 2010
    Posts
    5

    'unlegitimate' dereferencing void* pointer warning

    Hello people !

    I just subscripted to submit my question, but I'm not new to this forum. Actually I've been a regular visitor for the last weeks as I'm actively writing/debugging (my) C/C++ for my internship.
    That being so, I come to you guys after having perused the threads of this forum coming up at my query, and ALL the Google results I found (... yeah there were four of them). I found parts of the answer I'm looking for, but I still can't debug my code for I don't understand the issue.

    Here are the basics: I have a structure that contains basic types and a generic void* pointer. That pointer in my case points to a table of pointers to another structure (that contains pointers, but that's not the point (er...) here). I allocate the space for that table through a malloc(n*sizeof(void*)) instruction that to my understanding will perform the desired table the size of n times the size of a void* pointer. Anyways, if you followed that, here it is: I want to set all the pointers in that table, pointed to from my structure, to NULL values. Last, I do not know yet the type of the pointers in the table.

    Here comes the code (to help...) :

    Code:
    // said structure is in the header :
    typedef struct rbn
    {
      /* the other members ... */
      void * dat; // the pointer that will become a pointer to a table of void*
    } rbn;
    And in the mainload.c file (but not in the main):
    Code:
    // 'ribbon' is the name of the pointer to my rbn structure
    
    ribbon = malloc( sizeof(rbn) ); // memory allocation for the ribbon structure
    ribbon->dat = malloc( n*sizeof(void*) ); // memory allocation for the table of pointers
    
    // : initialization of the pointers in the dat table of pointers to NULL (table allocated just above)
    for (i = 0; i < n; i++)
    {
      ribbon->dat[i] = NULL;  // THE INSTRUCTION that protests (line 76)
      // I also tried to write (ribbon->dat)[i] = NULL but to no avail
    }
    Upon compilation, I get a wonderful :
    Code:
    mainload.c:76: warning: dereferencing 'void *' pointer
    mainload.c:76: error: invalid use of void expression
    Really I don't get it. I thought that summing it up for this post was going to clarify and solve the problem, but I still don't get why I this is refused (gcc compiler under Linux SuZe distro).
    I would understand if I couldn't increment a void* pointer, but gcc seems to have other bothers.

    Well, there it is, 'plain and simple' ! I would greatly appreciate some help.
    I leave it to your expert hands/eyes/minds to tell me what I am doing wrong.

    Meeshkah


    [Edit]: Problem solved, see posts #4 and #6.
    Last edited by Meeshkah; 08-20-2010 at 08:11 AM. Reason: Problem solved

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    74
    you have to cast the void pointer into some other type to get its value.

  3. #3
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    I'l try that, but... to which type should I cast ?

    There: so if I understand correctly, I can't use a void* pointer to access the values in a table, the pointer must be typed, is that it ? I would be okay with that, but since it is a void* pointer to a table of POINTERS -and if I'm not mistaken, the size of a pointer is not type-dependent (now is it ?)- then
    1) the type is that of a pointer (the size of the data to access, which I believe is the important information) and is the same whatever the type of data pointed, so to which type should I make the cast?;
    2) at the time the function is used, I don't know the type of data that will be pointed by a pointer in the table of pointers (o_O), so even that information I don't have.
    Anyway, that last information should not be necessary. Or you tell me I a bit too confused ?

    Thank you noobiept for your answer.

  4. #4
    a_capitalist_story
    Join Date
    Dec 2007
    Posts
    2,648
    See here, under void pointers

  5. #5
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    Thank you for this reference (sic!) in the matter, I bookmarked it, still I have questioning:

    > I so understand I have to cast the pointer to a type that can be used to dereference to, but that is exactly my problem, since the value pointed to is a pointer itself ! So what, I cast the pointer like this
    Code:
    void ** ptr = ribbon->dat; // temp pointer for the initialization
    ptr[i] = NULL;
    and it will compile all right ? Or should/can I declare dat as void** in the structure definition ?

  6. #6
    Registered User
    Join Date
    Aug 2010
    Posts
    5
    OK ! Thank you so much rags_to_riches, it compiles halfway now, I just set dat to void** type in the structure definition, and that did it !

    My original problem is solved: to get the solution, see the link of #4:rags_to_riches.

    I have another issue now: I included the math.h library but at compile time on all the object files created, gcc signals undefined reference to the functions of that library (basic trigonometry).

    If you can help me out on this one too ?

    Thanks anyway

    [Edit]: I foung the solution: one just need to pass -lm option to gcc so the it fetches the math libraries correctly. In case someone need the tip...
    Last edited by Meeshkah; 08-20-2010 at 08:08 AM. Reason: Problem solved

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,427
    You must realize that the compiler only works from the type information. So if you have int*, then the compiler knows it's pointing to an int and will generate proper code for that. But if you have void*, then what is it pointing to? The compiler doesn't know, and therefore cannot dereference it.
    And no, all pointer sizes are not guaranteed to be the same size.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help starting a program with functions
    By Neotriz in forum C Programming
    Replies: 7
    Last Post: 11-01-2009, 04:45 PM
  2. Replies: 10
    Last Post: 07-10-2008, 03:45 PM
  3. Need some help...
    By darkconvoy in forum C Programming
    Replies: 32
    Last Post: 04-29-2008, 03:33 PM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Replies: 28
    Last Post: 07-16-2006, 11:35 PM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21