-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

これはあなたのプログラムの問題ではありません。Microsoft は scanf 関数を非推奨にし、代わりに scanf_s 関数を導入しました。これは、セキュリティを導入したことを意味します。コードをコンパイルするには、2 つのオプションがあります。

  1. scan の代わりに scanf_s 関数を使用します。( http://msdn.microsoft.com/en-us/library/w40768et.aspx )
  2. または、マクロ「_CRT_SECURE_NO_WARNINGS」をコンパイラ設定に入れます。
于 2014-11-25T16:39:26.940 に答える