关于C语言g++ -Wall编译后的警告。。。。

//string reversal using stack
//#include <iostream>
//#include <stdlib.h>
#include <stdio.h>
#include <stack>
#include <cstring>
using namespace std;
void Reverse(char *C,int n)
{
	stack <char> S;
	//loop for push
	for (int i = 0; i<n; i++)
	{
		S.push(C[i]);
	}
	//loop for pop
	for (int j = 0; j<n; j++)
	{
		C[j] = S.top();//overwrite the character at index i
		S.pop();
	}
}
int main()
{
	char C[51];
	printf ("Enter a string : ");
	gets(C);
	Reverse(C,strlen(C));
	printf ("Output = %s",C);
	printf ("\n");
}

http://img.mukewang.com/592145d30001124a13660768.jpg

这几个警告是怎么回事?大神们帮我解析一下啊。。。。。

还有就是代码是不是有问题,Reverse的两个for循环并列关系,应该是同步执行吧,如果是同步执行,那么第二个for循环就错了呀。。。。我感觉有点蒙

asdhjhg
浏览 2514回答 3
3回答

onemoo

这几个都是警告 gets 函数不安全、不建议使用了。这个函数不判断缓冲区大小,实际使用很容易造成溢出。你自己做小练习用用也无所谓。

onemoo

啊,之前没注意到你问了两个问题。这两个 for 循环不是同步执行的啊。第一个 for 执行完,再执行第二个 for 的。第一个 for 中循环进行压栈,第二个 for 中取栈顶元素后再弹栈。你为什么会觉得是并列关系呢? 因为它们的 intent 一样??
打开App,查看更多内容
随时随地看视频慕课网APP