私はCを少し実験してきました。私は通常PHPとjavascriptを使用します。
「HelloWorld」を実行してから、これを入力しました。これは、どこかのWebサイトからコピーしたものです...
#include <stdio.h>
#include <stdlib.h>
#define MAX 20
int intcmp(const void*v1, const void *v2){
return (*(int *)v1 - *(int *)v2);
}
main(){
int arr[MAX], count, key , *ptr;
printf("Enter %d integer values; press enter after each\n", MAX);
for (count = 0; count < MAX; count++)
scanf("%d", &arr[count]);
puts("Press a key to sort the values");
getc(stdin);
qsort(arr, MAX, sizeof(arr[0]), intcmp);
for(count=0; count < MAX; count++)
printf("\narr[%d] = %d.", count, arr[count]);
puts("\nPress a key to continue");
getc(stdin);
printf("Enter a val to search for");
scanf("%d", &key);
ptr = (int * )bsearch(&key, arr, MAX, sizeof(arr[0]), intcmp);
if(ptr != NULL){
int fred = (ptr - arr);
printf("%d found at arr[%d]", key ,fred);
}else{
printf("%d not found", key);
}
}
ここまでは順調ですね。私はすべての星が何をしているのかを理解しようとしていますが、それは所定の位置に落ちています(ハハ-流れ星:)
ただし、20個の整数を要求されたときにfloat(21.45など)を入力すると、20個の配列値に奇妙な数値が入力されて、「検索する値を入力してください」と急いで進みます。
ある種のバッファオーバーフローを作成しましたか?入力をチェックする必要があることはわかっていますが、自分が何をしたのか知りたいです。プログラムを使用して任意のコードを実行できますか?(まあ、いや、私の知識ではありません...しかし誰かができますか?)