-1

I want to copy the string “Best School” into a new space in memory, which of these statements can I use to reserve enough space for it

A. malloc(sizeof(“Best School”))

B. malloc(strlen(“Best School”))

C. malloc(11)

D. malloc(12)

E. malloc(sizeof(“Best School”) + 1)

F. malloc(strlen(“Best School”) + 1)

I am still very new to C programming language so I really am not too sure of which works well. But I will love for someone to show me which ones can be used and why they should be used.

Thank you.

  • 1
    Since the purpose of learning is to think for yourself, please [edit] your question and provide your reasoning for each of the possibilities. Beside of having erroneous and good options, this is opinion-based. – the busybee Nov 10 '22 at 13:33
  • Are you per chance having a C test in school right now...? Should have studied harder in that case. – Lundin Nov 10 '22 at 13:38
  • I'll close this as a duplicate since the top answer in the target answers the question. – Lundin Nov 10 '22 at 13:39
  • @Lundin IMO it is a bit more general question not only about the string literals – 0___________ Nov 10 '22 at 13:49
  • 1
    @0___________ The dupe target explained what a null terminator is. The OP of this post doesn't know, hence their confusion. Also, we really don't need to answer questions that look as if they are potentially asked by someone trying to cheat on a live exam... – Lundin Nov 10 '22 at 13:52
  • Ask yourself a few questions (and answer them!): How much space do you need? What is the value of `sizeof(“Best School”)`? What is the value of `strlen("Best School")`? – William Pursell Apr 14 '23 at 12:08

3 Answers3

1

Literal strings in C are really arrays, including the null-terminator.

When you use sizeof on a literal string, you get the size of the array, which of course includes the null-terminator inside the array.

So one correct way for a literal string would be sizeof("Best School") (or sizeof "Best School").

You can also use strlen. If you don't have a string literal but another array or a pointer to the first character of the string, then you must use strlen. But now you have to remember that strlen returns the length of the string without the null-terminator. So you need to add one for that.

So another correct way would then be strlen("Best School") + 1.

Using magic numbers is almost never correct.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

Use of sizeof id limited to only this one case (string literal). Ti will not work if you will have a pointer referencing the string. Before you start to be more proficient in C language and "feel" the difference between arrays and pointers IMO you should always use strlen

Example:

char *duplicateString(const char *str)
{
    char *newstring = malloc(strlen(str) + 1);
    if(newstring) strcpy(newstring, str);
    return newstring;
}

In this case, sizeof(str) would give the size of the pointer to char (usually 2, 4 or 8) not the the length of the string referenced by the str

0___________
  • 60,014
  • 4
  • 34
  • 74
0

Addition to the above answer, malloc(12) will also work since it include the length of the literal string plus the null terminator.