Thread: "invalid use of void expression" error

  1. #1
    Registered User
    Join Date
    Aug 2012
    Posts
    11

    "invalid use of void expression" error

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    
    struct me
    {
            void **ptr;
    
    }*smith;
    
    
    int main()
    {
            int i,j;
    
            smith = malloc(sizeof(struct me));
            smith->ptr=malloc(sizeof(void *) * 4);
    
            for(i=0;i<4;i++)
            {
                    smith->ptr[i] = malloc(sizeof(void) * 5);
            }
    
            for(i=0;i<4;i++)
            {
                    printf("address of ptr[%d] is %p and the value inside it is %p \n",i,&smith->ptr[i],smith->ptr[i]);
            }
    
    
            for(i=0;i<4;i++)
            {
                    for(j=0;j<5;j++)
                    {
                            printf("address of ptr[%d][%d] is %p and the value inside it is %d \n",i,j,&smith->ptr[i][j],(int)smith->ptr[i][j]);
                    }
            }
    
    return 0;
    }
    I got "invalid use of void expression" error.
    Please help to fix this error.
    Thank you
    Last edited by smith283; 08-31-2012 at 08:21 AM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Basically, the sizeof(void) expression is invalid. You need to figure out what the pointer to void should point to, then allocate space for objects of that type.
    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
    Sep 2006
    Posts
    8,868
    Welcome to the forum, Smith283! (void)

  4. #4
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Thnx laserlight for your reply but i didn't get it, you need to explain more please as i'm a beginer in C.
    And actually the problem is in fetching the value inside ptr[i][j] i.e smith->ptr[i][j] . I have allocated the space for void type (size is 5) and now if i want to write into it say a string(characters), i have to just give a starting address of allocated space(&ptr[0][0]). It will successfully write in the allocated space. But if i want to fetch each character from it using "smith->ptr[i][j]" , it is giving an error "invalid use of void expression".

    So did you get my actuall question?????

    Thnx, hope you help.

  5. #5
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    The compiler complains about this line
    Code:
    smith->ptr[i] = malloc(sizeof(void) * 5);
    You cannot get the size of void. void represents an unknown type and that has no size.
    if you want to allocate 5 bytes (as stated in the previous post ) then use
    Code:
    smith->ptr[i] = malloc(sizeof(char) * 5);
    because sizeof(char) is guaranteed to be 1.
    or simply
    Code:
    smith->ptr[i] = malloc( 5 );
    This loops don't make any sense to me
    Code:
    for(i=0;i<4;i++)
        {
            for(j=0;j<5;j++)
            {
                printf("address of ptr[%d][%d] is %p and the value inside it is %d \n",i,j,&smith->ptr[i][j],(int)smith->ptr[i][j]);
            }
        }
    }
    It seems that you are trying to print 5 ints but there is only space for 5 uninitialized chars

    NOTE:
    somehow I have the feeling that you wanted to declare struct me as
    Code:
    struct me {
        int ** ptr;
    };
    to have a 2 dim array of ints.

    Kurt

  6. #6
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Your mechanic wants to know what model and year of car you will be bringing in for service, later today.

    You keep saying "a vehicle".

    Your mechanic is not able to use that kind of non-specific information.

  7. #7
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    With sizeof(void) you may as well be asking the compiler for space for 5 unicorns, as far as it's concerned. void has no size.

    sizeof(void*) on the other hand is perfectly fine. It's like saying how many bytes do I need to store the GPS coordinates of those unicorns. The compiler basically doesn't care what the pointer points to, it just knows that you want the size of a pointer, to something.

    Note that you'll also have an issue with the printf that contains this:
    Code:
    smith->ptr[i][j]
    You can index the array of void* pointers with the [i] part, but then at the j part you're asking it to index into an array of voids after that. Since it doesn't know how big voids are, that wont work.
    You need to change the pointer type at that point in the expression (or earlier) from void* to int* before the last array indexing. It's too late to cast it to int afterwards.
    I suggest breaking it up and storing the smith->ptr[i] part into a separate variable first, then your printf statements will be much easier.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  8. #8
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    One question for all of you, if i comment the following lines as shown in the code below, it would work or not????


    Code:
    #include<stdio.h>
    #include<stdlib.h>
     
    struct me
    {
            void **ptr;
     
    }*smith;
     
     
    int main()
    {
            int i,j;
     
            smith = malloc(sizeof(struct me));
            smith->ptr=malloc(sizeof(void *) * 4);
     
            for(i=0;i<4;i++)
            {
                    smith->ptr[i] = malloc(sizeof(void) * 5);
            }
     
            for(i=0;i<4;i++)
            {
                    printf("address of ptr[%d] is %p and the value inside it is %p \n",i,&smith->ptr[i],smith->ptr[i]);
            }
     
     
    /*        for(i=0;i<4;i++)
            {
                    for(j=0;j<5;j++)
                    {
                            printf("address of ptr[%d][%d] is %p and the value inside it is %d \n",i,j,&smith->ptr[i][j],(int)smith->ptr[i][j]);
                    }
            }
    */
    
    return 0;
    }

  9. #9
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Answer yes or no?? Please......

  10. #10
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    We've all been telling you that sizeof(void) cannot be used and yet you've shown code where you still have exactly that on line 20.
    So what do you think?*...
    People will stop responding when you stop bothering to pay attention to what they are saying!

    *Of course that wont "work". It wont compile for the reasons we've told you.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  11. #11
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Extremly sorry mate, i should have told you all in the begining that i'm using GCC compiler (fedora 14, linux).
    And iMalc, i think the program in the above lastest post of mine will work fine (without errors). The problem here is not the "sizeof(void)" bt error comes from the printf line that i've commented.
    And it would allocate the memory for void type succcessfully:-

    Code:
    for(i=0;i<4;i++)        {
    
                    smith->ptr[i] = malloc(sizeof(void) * 5);
    
            }
    The error "invalid use of void expression" which the compiler is giving is for this line:-

    Code:
    printf("address of ptr[%d][%d] is %p and the value inside it is %d \n",i,j,&smith->ptr[i][j],(int)smith->ptr[i][j]);
    If i try to print "sizeof(void)" it wont work, but how it is allocating memory "void" type in this line:-

    Code:
        smith->ptr[i] = malloc(sizeof(void) * 5);
    I need a perfect reason for this.

    Also i've done writting into void type allocated space, if you people are right then how this is working:-

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    
    struct me
    {
            void **ptr;
    
    }*smith;
    
    
    int main()
    {
            int i,j;
            char buff[5]="hello";
    
            smith = malloc(sizeof(struct me));
            smith->ptr=malloc(sizeof(void *) * 4);
    
            for(i=0;i<4;i++)
            {
                    smith->ptr[i] = malloc(sizeof(void) * 5);
                    memset(smith->ptr[i],'\0',sizeof(void) * 5);
            }
    
            for(i=0;i<4;i++)
            {
                    printf("address of ptr[%d] is %p and the value inside it is %p \n",i,&smith->ptr[i],smith->ptr[i]);
            }
    
            strncpy(smith->ptr[0],buff,5);
    //              or
    //      strcpy(&smith->ptr[0][0],buff);
    
    
            printf("the string is : %s\n",smith->ptr[0]);
    //              or
    //      printf("the string is : %s\n",&smith->ptr[0][0]);
    
    
    /*      for(i=0;i<1;i++)
            {
                    for(j=0;j<5;j++)
                    {
                            printf("address of ptr[%d][%d] is %p and the value inside it is %c \n",i,j,&smith->ptr[i][j],(char )smith->ptr[i][j]);    ****Compiler give error on this line not on the sizeof(void)****
                    }
            }
    */
    
    return 0;
    }
    Thnk You

    smith

  12. #12
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    Does in linux "void" size is 1byte????

  13. #13
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    The problem here is not the "sizeof(void)" bt error comes from the printf line that i've commented.
    Like so many newbies who think they know what they are doing you are only looking for confirmation of a self-inflicted misunderstanding.

    I need a perfect reason for this.
    You've been given the reason. You rejected that reason.

    You want a "perfect" reason for this that meshes with your poor understanding of C.

    Also i've done writting into void type allocated space, if you people are right then how this is working:-
    It isn't; it doesn't.

    "GCC" is letting you do this without complaining because an extension relevant to the history of C; the compiler doesn't care if you use this extension incorrectly by default.

    Add "-pedantic" to your command line and the compiler will start complaining.

    Soma

  14. #14
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    I have'nt rejected anything, the problem is i'm not getting it

    sizeof(void) is invalid to use, i get it.

    but would it allocate memory for void type or not in this line???????????????

    "smith->ptr[i] = malloc(sizeof(void) * 5);"

  15. #15
    Registered User
    Join Date
    Aug 2012
    Posts
    11
    If "yes" :This is your answer:-

    " "GCC" is letting you do this without complaining because an extension relevant to the history of C; the compiler doesn't care if you use this extension incorrectly by default.

    Add "-pedantic" to your command line and the compiler will start complaining. "

    if "no" :then your answer is:-
    " You've been given the reason. You rejected that reason. "
    ????
    ????

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 10
    Last Post: 08-09-2012, 12:48 PM
  2. Replies: 9
    Last Post: 03-31-2009, 04:23 PM
  3. Replies: 2
    Last Post: 09-12-2006, 04:50 AM
  4. Replies: 24
    Last Post: 09-06-2006, 06:17 PM