R
redefined.horizons
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
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