C question (memory allocation and access)

Mark A. Hampson mhampson at wpi.wpi.edu
Tue Nov 7 12:45:53 AEST 1989


I have a question that I have not been able to find an answer that I feel 
comfortable with regarding the following situation:

I am attempting to allocate a block of memory as follows:

int   m = 10;
int   n = 10;
void *block;
int   block_siz;

block_siz = 2*sizeof(int) + m*n*sizeof(double);
block = malloc(block_siz);

simple enough...

I now wish to put two integers at the begining of this block of memory:

block = m;
(block+sizeof(int)) = n;   <---  here is where I am running into probs.

My intention is to store m in the first int sized space in block and to 
store n in the int sized space after it.

The compiler that I am using tells me that it does not know the size of 
block and therefore cannot perform the operation block+sizeof(int).  This
makes some sence as block is of type void *.  That being the case, how can 
I perform the operation that I want to?

Thanks in advance:
Mark A. Hampson

-- 
/----------------------------------------------------------------------------\
|      We are not in an energy crisis, we are having an entropy crisis...    |
\----------------------------------------------------------------------------/



More information about the Comp.std.c mailing list