Skip to main content
gamedev.net
🔒 Locked

C void pointer question [code]

Started by nutz Nov 23, 2004 at 12:27 AM 3 replies 1.2k views
Original Post
nutz
nutz
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?
Endar
Endar
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.
[size="2"][size=2]Mort, Duke of Sto Helit: NON TIMETIS MESSOR -- Don't Fear The Reaper
nutz
nutz
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
SiCrane
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]))[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.
MaulingMonkey
MaulingMonkey
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.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.