cpp_references

Description

Use C++ references to define the character pointer char *p in the main function, then malloc to request space (size 100 bytes) within the subfunction, read the string via fgets(), and finally output it in the main function; note: you must use C++ References in the subfunction, and to read the string from standard input in C++, you need to use fgets(p,100,stdin).

Input

Enter a string, such as I love C language.

Output

If the input is I love C language, then the output is I love C language.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <bits/stdc++.h>
using namespace std;

void allocateAndReadString(char*& str)
{
str = static_cast<char*>(malloc(100));
fgets(str,100,stdin);
}

int main()
{
char *p;
allocateAndReadString(p);
cout << p << endl;
free(p);
return 0;
}