我想為每個陣列分配它的索引,這是我的代碼。
#include <stdio.h>
#include <cs50.h>
int main(void) {
int length = get_int("How many arrys do you need? ");
int s[length];
for (int i = 0; i < length; i ) {
s[i] = i;
printf("s[i] = %d\n", i);
}
}
運行時
s[i] = 0
s[i] = 1
所以我想i用它的索引號替換代碼,如下所示:
s[0] = 0
s[1] = 1
uj5u.com熱心網友回復:
關于陣列的初始化,您的代碼很好。
printf應該更改陳述句以列印索引和存盤在索引中的值:
printf("s[%d] = %d\n", i, s[i]);
這是修改后的版本:
#include <cs50.h>
#include <stdio.h>
int main() {
int length = get_int("How many array elements do you need? ");
if (length <= 0 || length > 1024) {
printf("invalid length: %d\n", length);
return 1;
}
int s[length];
for (int i = 0; i < length; i ) {
s[i] = i;
printf("s[%d] = %d\n", i, s[i]);
}
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/490678.html
上一篇:有沒有辦法比較兩個字串,一個是父字串,另一個是子字串,并制作粗體相似的子字串?
下一篇:如何將字串回傳到主函式?
