-3

我使用动态内存分配编写了这个简单的冒泡排序程序。我正在使用 VC++ 编译器。

// bubble_sort.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include <stdio.h>
#include <stdlib.h>
void bubble_sort(int a[],int n);
int main()
{
    int *p,i;
    int n;
    printf("Enter number of array elements\n");
    scanf("%d",&n);
    p=(int*)malloc(sizeof(int)*n);
    for(i=0;i<n;i++)
        scanf("%d",(p+i));
    bubble_sort(p,5);
    printf("Sorted elements\n");
    for(i=0;i<n;i++)
        printf("%d ",p[i]);
    free(p);
    system("pause");
    return 0;
}
void bubble_sort(int a[],int n)
{
    int i,j,temp;
    for(i=0;i<n;i++)
    {
        for(j=0;j<n-1-i;j++)
        {
            if(a[j]>a[j+1])
            {
                temp=a[j];
                a[j]=a[j+1];
                a[j+1]=temp;
            }
        }
    }
}

上面的程序有什么问题?编译器显示以下警告。这是什么意思?

Warning 1   warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.  

Warning 2   warning C4996: 'scanf': This function or variable may be unsafe. Consider using scanf_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.  

请帮我。

4

1 回答 1

3

这不是您的程序的问题。微软弃用了 scanf 函数,而是引入了 scanf_s 函数,这意味着他们引入了安全性。要编译您的代码,有两个选项。

  1. 使用 scanf_s 函数而不是 scan。( http://msdn.microsoft.com/en-us/library/w40768et.aspx )
  2. 或将宏“_CRT_SECURE_NO_WARNINGS”放在编译器设置中。
于 2014-11-25T16:39:26.940 回答