fork download
  1. #include <stdio.h> // printf, scanfを使うため
  2. #include <stdlib.h> // malloc, freeを使うため
  3.  
  4. int main(){
  5. int n, i; // n:配列の要素数, i:ループ用変数
  6. int *a; // int型のポインタ
  7. scanf("%d",&n); // 配列の大きさを入力
  8. a = (int *)malloc(sizeof(int) * n);// n個分のint型メモリを確保
  9. if(a == NULL){printf("ERROR\n");return 0;} // メモリ確保に失敗した場合
  10. for(i = 0; i < n; i++){a[i] = i + 1;} // 配列に値を代入
  11. for(i = 0; i < n; i++){
  12. printf("%d ", a[i]); // 配列の中身を表示
  13. }
  14. free(a); // 確保したメモリを解放
  15. return 0;
  16. }
  17.  
  18.  
Success #stdin #stdout 0.01s 5320KB
stdin
4
stdout
1 2 3 4