我想將這些字符串與它們各自的索引對齊。該字符串是這樣的:平等對齊字符串
char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};
和我的輸出是這樣的:
Fred 0
Augustine 1
Bob 2
Lenny 3
Ricardo 4
但我經過一些事情是這樣的:
Fred 0
Augustine 1
Bob 2
Lenny 3
Ricardo 4
從而使指標對準同。我只是想讓它更具可讀性。這是我的代碼看起來像到目前爲止:
#include <stdio.h>
#include <stdlib.h>
int
main(void) {
int i;
char *mystrings[] = {"Fred", "Augustine", "Bob", "Lenny", "Ricardo"};
for (i = 0; i < 5; i++) {
printf("%s %d\n", mystrings[i], i);
}
return 0;
}
有沒有辦法我可以做到這一點?像比較索引與最長的字符串,然後爲這些整數做空間?
請參見[printf手冊頁](http:// lin ux.die.net/man/3/printf) – kaylum
爲什麼在'char * mystrings [MAXSIZE]中使用'MAXSIZE'''讓編譯器執行這個工作,例如'char * mystrings [] ,,,' –
@EdHeal my壞。我會改變這一點。 – RoadRunner