[英]printing a 2d array of string in c
我正在嘗試打印2d字符串數組作為練習(我是新手),但沒有成功我已經嘗試了每種組合,但我什至都沒想到我確定我在一個我可以做的地方犯了一個愚蠢的錯誤這里沒有一些示例:使用指針:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define lim 10
#define maxx 25
void print(char *);
int main()
{
int i = 1;
char input[lim][maxx];
char *ps = input;
printf("type the list of %d names or type quit to leave \n", lim);
while (i<lim && gets(input[i]) != NULL && strncmp(input[i], "quit", 4)!=0 ) {
i++;
}
printf("i've counted %d names\n", i);
print("\n");
print(ps);
return 0;
}
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a) != '\0') {
printf("%s\n", *(a+i));
i++;
}
}
這是輸出:
type a list of %d names or type quit to leave :
bla
bli
blo
quit
i've counted 4 names
the list of names include :
segmentation fault (core duped)
打印功能的另一個版本是這樣的:
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
for(j = 0; j < maxx; j++){
puts(aray[i][j]);
//printf("%s\n", aray[i][j]);
}
}
}
我得到相同的輸出,有人可以幫我調試嗎? 和提前
您將i添加為1,這在二維數組的情況下將無濟於事,因為下一個元素將位於maxx位置,因此您可以執行類似的操作//此處,lim和max在程序中定義
void print(char *a){
int i=0;
printf("the list of names include : \n");
while(i<(lim*maxx)){
printf("%s\n",a );
i += maxx;
a = a + maxx;
}
}
第二個變體應該是
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
cout<<aray[i]<<"\n";
}
}
簡而言之,您似乎需要重新梳理指針。 使用原始打印功能:
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a) != '\0') {
printf("%s\n", *(a+i));
i++;
}
}
您每次迭代都在+ i處打印該值。 這可能聽起來像您想要的,但實際上傳遞給print
是指向char數組數組的指針(您的編譯器應發出有關不兼容的指針類型的警告)。 也就是說, ps
的“適當”類型是(char *)[]
。 因此,在print
功能中,每次迭代僅使內存地址增加sizeof(char),而實際需要的是將內存地址增加sizeof(char)* maxx(數組條目的大小)。 要實施此更改,請執行以下操作:
print
聲明
void print(char (*)[maxx]);
char (*ps)[maxx] = input;
最后,將打印功能更改為:
void print(char (*a)[maxx]){
printf("the list of names include : \n");
int i;
for (i = 0; i < lim; i++){
printf("%s\n",*a);
a++;
}
}
您無需使用(a + i)語法,因為每次迭代僅將a向前推進即可完成相同的操作,並且對於大i而言可能更快。 當然,正如其他人提到的,仔細檢查您的新行打印,我相信您需要printf('\\n')
。
您從2d數組的索引1開始,應該從索引0開始
int i=1;
您的打印函數需要一個字符數組,然后對每個字符執行一個printf字符串,這沒有任何意義
void print(char *a)
{
int i=0;
printf("the list of names include : \n");
while(*(a)!='\0')
{
printf("%s\n",*(a+i));
i++;
}
}
而是讓它看起來像這樣
void print(char *a[], int strings)
{
int i = 0;
for (; i < strings; ++i)
{
puts( a[i] );
}
}
並用您讀取的字符串數調用它
print(ps,i);
使用fgets()而不是gets()也會更好,特別是因為您的字符串最多為25個字符,因此很容易給出更長的字符串。 fgets()可讓您指定字符串的最大大小fgets(input[i],maxx,stdin)
您的其他功能
void print(char aray[lim][maxx])
{
int i,j;
printf("the list of names include : \n");
for(i = 0; i < lim; i++) {
for(j = 0; j < maxx; j++){
puts(aray[i][j]);
//printf("%s\n", aray[i][j]);
}
}
}
對間接級別做了類似的錯誤假設
arra [i] [j]是一個字符,但是puts帶有字符串參數,因此puts(arra [i] [j]); 不正確,您可以嘗試使用fputc( arra[i][j], stdout )
因為fputc需要一個字符
修復到
void print(char (*)[maxx]);
int main()
{
int i = 0;//int i = 1;
char input[lim][maxx] = { {'\0'}};
char (*ps)[maxx] = input;
printf("type the list of %d names or type quit to leave \n", lim);
while (i<lim && gets(input[i]) != NULL && strncmp(input[i], "quit", 4)!=0 ) {
i++;
}
printf("i've counted %d names\n", i);
printf("\n");//print("\n");
print(ps);
return 0;
}
void print(char (*a)[maxx])
{
int i=0;
printf("the list of names include : \n");
while(i<lim && a[i][0] != '\0') {
printf("%s\n", a[i]);
i++;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.