r4ys0n

Cybersecurity Artist

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;
}

Welcome to Hexo! This is your very first post. Check documentation for more info. If you get any problems when using Hexo, you can find the answer in troubleshooting or you can ask me on GitHub.

Quick Start

Create a new post

1
$ hexo new "My New Post"

More info: Writing

Run server

1
$ hexo server

More info: Server

Generate static files

1
$ hexo generate

More info: Generating

Deploy to remote sites

1
$ hexo deploy

More info: Deployment

0%