我對 C 編程相當陌生,并決定嘗試一些問題。在我嘗試它們時,我遇到了一個問題,并且不確定我是否做對了。
該問題要求創建一個函式,該函式使用用戶按照建議提供的整數值串列初始化陣列引數。我想我設法做到了。
此外,大小需要由用戶輸入確定,并且最大可能接受的串列大小設定為 200。當涉及到讓用戶輸入大小時,我進行了管理,但是當涉及到可能的最大大小時,它不是作業。
在這里你可以找到我的代碼:
#include <stdio.h>
void init_array(int ar[], int size) {
int i;
printf("Please enter %d values: \n");
for (i = 0; i < size; i ) {
scanf("%d", &ar[i]);
}
printf("Elements in array are:");
for (i = 0; i < size; i ) {
printf("%d, ", ar[i]);
}
}
int main() {
int size;
printf("The size of list:");
scanf("%d", &size);
int marks[] = {};
init_array(marks, size);
if(size >=200){
printf(" Limit exceeded");
}
return 0;
}
謝謝你的幫助
uj5u.com熱心網友回復:
您的代碼中有幾個主要錯誤和一些會產生警告的方面(請參閱:為什么我應該總是啟用編譯器警告?)并且應該改進。
首先,你必須省略size引數(對應于給定%d在你第一次呼叫格式符),以printf在該init_array功能。
其次,您的宣告marks是錯誤的——就目前而言,它宣告了一個長度為零的陣列(或者,更確切地說,試圖這樣做)。因此,假設您有一個支持可變長度陣列的 C 編譯器(VLA – 大多數支持,但有些,特別是 MSVC,不支持),您應該int marks[size]; 在讀取 的值之后宣告它size(并且,最好是在您驗證了的給定值size是可以接受的)。另請注意,您不能使用初始化串列( = {0, 1, }語法)初始化 VLA 。如果你不希望使用VLA,那么你可以指定一個固定的(很可能是200)最大的陣列大小。
一些會產生警告和/或可以改進的問題:
在 C 中,通常更好的做法(恕我直言)為不帶引數的函式添加顯式 void作為正式引數串列。相關閱讀:C 的 main() 函式的有效簽名是什么?雖然,在函式定義中(與原型相反),空()是有效代碼。
你真的應該養成檢查呼叫scanf(和相關函式)回傳的值的習慣,以確保給出了有效的輸入。在下面的代碼中,我為main函式中的呼叫添加了這樣的檢查,但您也應該在init_array函式中添加類似的檢查。
當 的值超過 200 時size,您的程式可能不應該運行該init_array函式;因此,您應該在初始化marks陣列之前檢查該值。
這是您的代碼的一個版本,其中包含我上面概述的更改:
#include <stdio.h>
void init_array(int ar[], int size)
{
int i;
printf("Please enter %d values: \n", size); // You need to give "size" as an argument
for (i = 0; i < size; i ) {
scanf("%d", &ar[i]); // You should add a check for valid input here, as I have done in main
}
printf("Elements in array are:");
for (i = 0; i < size; i ) {
printf("%d, ", ar[i]);
}
}
int main(void) // Better with the explicit "void" argument list specifier
{
int size;
printf("The size of list:");
if (scanf("%d", &size) != 1 || size < 1) { // You should ALWAYS check the return value of a scanf call
printf("Invalid size input.\n");
return 1;
}
int marks[size]; // Need to specify the size and note that you cannot initialize a VLA
// int marks[200] = { 0, }; // Alternative if not using VLA.
if (size >= 200) {
printf(" Limit exceeded.\n");
return 2;
}
init_array(marks, size);
return 0;
}
請隨時要求任何進一步的澄清和/或解釋。
uj5u.com熱心網友回復:
您正在創建一個零長度陣列,您應該將主要功能更改為如下所示。
int main() {
int size;
int marks[200];
printf("The size of list:");
scanf("%d", &size);
if(size < 200){
init_array(marks, size);
} else {
printf(" Limit exceeded");
}
return 0;
}
uj5u.com熱心網友回復:
首先,您需要檢查大小是否有限制,然后創建所需大小的陣列或停止執行程式。
int main() {
int size;
printf("The size of list:");
scanf("%d", &size);
int marks[size];
if(size >=200){
printf(" Limit exceeded");
return 1;
}
init_array(marks, size);
return 0;
}
uj5u.com熱心網友回復:
在第一個 printf 陳述句中,您需要添加size作為第二個引數。您還需要在宣告陣列之前檢查限制,并且需要在函式 main 中指定陣列的大小(C 中的陣列不會根據需要自動增長)。最后,您還應該通過檢查scanf的回傳值來驗證輸入是否為整數。這是修改后的版本:
#include <stdio.h>
#include <stdlib.h>
void read_int(int *i) {
int n = scanf("%d", i);
if (n != 1) {
fprintf(stderr, "Integer expected\n");
exit(EXIT_FAILURE);
}
}
void init_array(int ar[], int size) {
int i;
printf("Please enter %d values:\n", size);
for (i = 0; i < size; i ) {
read_int(&ar[i]);
}
printf("Elements in array are: ");
for (i = 0; i < size; i ) {
if (i > 0) {
printf(", ");
}
printf("%d", ar[i]);
}
putchar('\n');
}
int main(void) {
const int max_size = 200;
int size;
printf("The size of list: ");
read_int(&size);
if ((size >= 1) && (size <= max_size)) {
int marks[size];
init_array(marks, size);
} else {
fprintf(stderr, "Array size must be between 1 and %d\n", max_size);
exit(EXIT_FAILURE);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/407561.html
標籤:
上一篇:將無符號整數與負文字進行比較
