I understand I want to use malloc, but How do I have it hold pointers? If I am given a number for the size of the array and I want each one of those indexes to point to another array.
Some input on how to start it would be helpful. I know its not too many lines of code but the concept is abstract to me making it hard to know where to start in the code.
-
Pointers to what? How many?Eric Postpischil– Eric Postpischil2020年05月27日 01:30:44 +00:00Commented May 27, 2020 at 1:30
1 Answer 1
Well malloc allocates an area of memory the size of so many bytes. So if you want to hold pointers in that memory, you'll need to multiply the size of a single pointer by how many you want in that area.
Here's a little example for an array of strings (which are just pointers)...
const char ** array_of_strings;
array_of_strings = malloc(2 * sizeof (const char *));/* size for two pointers */
array_of_strings[0] = "first string";
array_of_strings[1] = "second string";