0

I want to allocate an array for 6 words of a maximum of 30 charachters for example.

My teacher says I should allocate 6 "+1" to count for a NULL that I have to set my self after inserting my words.

How exactly is a char **array is allocated and why is my teacher insisting ?

8
  • Did you do any research? There are many posts that ask the same/similar allocation question. Commented Mar 24, 2021 at 8:54
  • My teacher says I should allocate 6 "+1" to count for a NULL. You should ask the teacher that question. At a guess it is so you can iterate the array and know where it ends without knowing the size of the array beforehand. Commented Mar 24, 2021 at 8:55
  • @kaylum my teacher literally said " beacause i say so, and i won't accept the answer if you don't do what i say" which is better and whether i understand or not is not important clearly Commented Mar 24, 2021 at 8:57
  • In that case the full task requirements may tell us why that is needed. But you have not provided that so we can't say for sure. But as I said, it's likely to be for an end of array marker. Commented Mar 24, 2021 at 8:58
  • @kaylum about the linked post, no it doesn't help as I already know that a string should terminate with a '\0' but my question is regarding the first dimension. the number of cells i need to store n words and whether it is me that should add 1 more to terminate my first dim with NULL or whether it's already there Commented Mar 24, 2021 at 8:59

2 Answers 2

0

It sounds like your teacher wants you do use a null pointer at the end of the array, as a sentinel value. This is similar to null termination of individual strings and can sometimes be useful when the amount of strings is unknown.

When you allocate a single string you do char* str = malloc(length + 1); where the +1 is for the null terminator '\0. The extra +1 ensures that given length = strlen("hello");, we can safely do strcpy(str, "hello") to fill this string.

You can do something similar for an array of strings by
char** arr = malloc(sizeof(char*[size+1]));
If you then assign arr[size] = NULL; as sentinel value, it will be possible to iterate over this array in this way:

for(const char** ptr=arr; ptr!=NULL; ptr++)
{
  printf("%s\n", *ptr); // do something with each string
}

But if you know the size, then the preferred method is a normal loop:

for(size_t i=0; i<size; i++)
{
  printf("%s\n", arr[i]); // do something with each string
}
Sign up to request clarification or add additional context in comments.

2 Comments

Your malloc should have the +1 inside the square brackets, otherwise you're allocating only 1 byte for a pointer.
@interjay Yikes, of course, thanks. I made lots of other silly mistakes too. Note to self is not to write code from the top of my head when posting on SO, but always try it on a compiler even when it seems trivial :)
-1

char **arr = calloc(7, sizeof(char*)); So let's break this down, calloc basically makes an array, first argument is the size of the array and the second is how much memory to allocate to every index of the array.

4 Comments

This allocates an array of 7*6 chars and is not compatible with char**.
Why is it not compatible?
Because char** is supposed to point to an array of char*.
It should be char **arr = calloc(7, sizeof(char*));

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.