0

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.

asked May 27, 2020 at 1:26
1
  • Pointers to what? How many? Commented May 27, 2020 at 1:30

1 Answer 1

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";
answered May 27, 2020 at 2:42
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.