Sign in to follow this  
nutz

C void pointer question [code]

Recommended Posts

I am having trouble getting this code to work. Is this correct ?
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4
5 typedef struct x
6 {
7    void **vp;
8    struct x *next;
9 }dev;
10
11 int main()
12 {
13    dev d;
       /* i want to allocate 2X2 array if ints, */
14    d.vp = (void **)malloc(2*sizeof(int *));
15    d.vp[0] = (void *)malloc(2*sizeof(int));
16    d.vp[1] = (void *)malloc(2*sizeof(int));
17   /* are these assignment correct *?
18    *(int*)(*(d.vp+0)+0) = 100;
19    *(int*)(*(d.vp+0)+1) = 34;
20    *(int*)(*(d.vp+1)+0) = 1134;
21    *(int*)(*(d.vp+1)+1) = 2134;
22      /* say if i have a int **i, populated as 2X2 array as above can i do a :  d.vp = &i[0]; to make void ** point to int** */
22      free(d.vp);
23       return 0;
24 } 
The comments in the code indicate what I am confused about/what I am trying to achieve. Any help?

Share this post


Link to post
Share on other sites
Okay, you might be having a small problem in using 'void'.


#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct x
{
//void **vp;
int **vp;
struct x *next;
}dev;

int main()
{
dev d;

/* i want to allocate 2X2 array if ints, */
d.vp = (int**)malloc(2*sizeof(int*));
d.vp[0] = (int*)malloc(2*sizeof(int));
d.vp[1] = (int*)malloc(2*sizeof(int));

/* are these assignment correct */
d.vp[0][0] = 100;
d.vp[0][1] = 34;
d.vp[1][0] = 1134;
d.vp[1][1] = 2134;

printf("\n %d %d \n %d %d \n", d.vp[0][0], d.vp[0][1], d.vp[1][0], d.vp[1][1]);

// N Free is wrong!!! have to free every row, then free d.vp
free(d.vp[0]);
free(d.vp[1]);
free(d.vp);
return 0;
}



Run that.

The problem I was talking about is that when trying to compile your code, I was getting a specific compiler error: C2069 (VC++). And this error said that:

"cast of 'void' term to non-'void'
A term of type void was cast to a different type.
Type void cannot be cast to any other type.

So, as you see it might not be possible to use 'void' as the type.

If anyone else knows how to do this, feel free to correct me. But the code as I have it should be fine, and hopefully you can adapt it to what you need.

Share this post


Link to post
Share on other sites
thanks for your help but that won't work. The data structure has the void pointer so I can't really make it an int. And this was written in linux using gcc 3.3.1 - don't know how VC would react to it :)

thanks anyway

Share this post


Link to post
Share on other sites
The typing on layers of indirection aren't quite right in your assignments.

((int *)(d.vp[0]))[0] = 100;
((int *)(d.vp[0]))[1] = 34;
((int *)(d.vp[1]))[1] = 1134;
((int *)(d.vp[1]))[1] = 2134;

d.vp is a void ** so the first level in dereferencing gives a void *, this then can be cast to an int *, which than can be then dereferenced as integers.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
The typing on layers of indirection aren't quite right in your assignments.

((int *)(d.vp[0]))[0] = 100;
((int *)(d.vp[0]))[1] = 34;
((int *)(d.vp[1]))[0] = 1134; //<--- MM: Fixed line.
((int *)(d.vp[1]))[1] = 2134;

d.vp is a void ** so the first level in dereferencing gives a void *, this then can be cast to an int *, which than can be then dereferenced as integers.


Line 3 has a typeo. Quote is edited with comment indicating line fixed.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this