A few questions regarding memory management (C language)

Hello there! I’ve been studying C during this week and I came across a few (with a few I mean a lot) of questions regarding memory management.

So if I write the following code:

int main(void) {
string p;
print("p: ");
scanf("%s", &p);
}

When I create the variable p, the computer assigns a piece of memory to store whatever string we put into p and the operator &p seeks the memory address where p is located and stores the value we have given to p.

First question: is my interpretation correct?

Second question: if I declare p as a pointer, can I “get rid” of the assignment (&) operator?

e.g.:

int main(void) {
string *p;
print("p: ");
scanf("%s", p);
}

And lastly, how does scanf() differ from get_string(), get_int() etc? Essentially, I believe they do the same, which is getting input from the user, but when talking about memory, how different are their behaviors?

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.

markdown_Forums

In C there is no such type as a string. Are you doing CS50 course which supplies a library that implements a string data type?

Here’s some good reading on this subject:

https://www.programiz.com/c-programming/c-strings