这个程序有什么问题?

时间:2014-11-25 16:30:47

标签: c++ dynamic-memory-allocation bubble-sort

我使用动态内存分配编写了这个简单的冒泡排序程序。我正在使用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.  

请帮帮我。

1 个答案:

答案 0 :(得分:3)

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

  1. 使用scanf_s函数而不是scan。(http://msdn.microsoft.com/en-us/library/w40768et.aspx
  2. 或放置宏&#34; _CRT_SECURE_NO_WARNINGS&#34;在编译器设置中。
相关问题