Re: Casting a generic or void pointer to point to a struct...



MQ wrote:
MQ wrote:
redefined.horizons@xxxxxxxxx wrote:
First, I would thank all of those that took the time to answer my
question about creating an array based on a numeric value stored in a
variable.

I realize after reading the responses and doing some more research,
that what I really need is known in C as a "dynamic array". Basically,
you surpass the array notation and use pointers with memory obtained
with malloc() or calloc(). I think this will do just what I needed.

That has brought up another question though. I'm not sure what syntax I
would use to cast a gneeric or void pointer to a struct that I have
defined. For example, if I have defined the "new_data_type" struct
previously in the source code file, would the following code be valid?

/* Create a generic pointer to the first element in the block of
memory obtained with the calloc() function. */
void generic_pointer = calloc(number_of_elements, size_of_element);

/* If the memory has been sucessfully allocated, cast the generic
pointer to the correct data type. */
if(generic_pointer != NULL)
(struct new_data_type *)generic_pointer
else
/* We've got problems. */

Thanks again for the help everyone.

Scott Huey
struct new_data_type * ptr = calloc(number_of_elements,
size_of_element);
if(ptr == NULL)
{
/*error */
}
else
{
/*do something */
}

correction: size_of_element should be replaced by sizeof(struct
new_data_type)


In all probability neither is optimal. Firstly, the OP did not say that it needed clearing to all bits 0 (remember that floating point 0 and null pointers might not be all bits 0) so why pay the cost of zeroing the memory? Secondly, there are better ways to use sizeof.

If you really do want calloc:

T *ptr = calloc(number_of_elements, sizeof *ptr);
Then you only have to specify the type in one place, so maintenance is easier.

Or, for malloc
T *ptr = malloc(number_of_elements * sizeof *ptr);

Or, if mallocing some time after declaration:
ptr = malloc(number_of_elements * sizeof *ptr);

Although the OP should also look up the references to the struct hack others have posted and, if using a C99 compiler, the C99 sanctioned alternative.
--
Flash Gordon
Still sigless on this computer.
.



Relevant Pages

  • Re: Malloc code
    ... malloc() not return NULL? ... returned from malloc to an array of pointers, and then return this array of ... So I just returned a void pointer and assign the value to ... TCP_LOAD_MCB_X and I am assigning the values to the items of the strucutre ...
    (microsoft.public.vc.language)
  • Re: Can array[]=malloc()ed?
    ... > int main ... > in the above I have used array but derefered as a pointer since ... > first element in a array I wanted to malloc the array y. ...
    (comp.lang.c)
  • Re: Can array[]=malloc()ed?
    ... >> int main ... >> in the above I have used array but derefered as a pointer since ... >> first element in a array I wanted to malloc the array y. ...
    (comp.lang.c)
  • Re: char *arr[n] Vs. char **arr
    ... since the number of small strings is not ... the array format[], and copy the token to that address. ... The malloc statement gives a segfault. ... makes an integer from pointer" etc.. ...
    (comp.lang.c)
  • Re: Errors in calloc
    ... The size of an object is garnered by sizeof, ... Are you suggesting that the pointer ... returned by malloc (or calloc) doesn't point to an object? ...
    (comp.lang.c)