简体   繁体   English

在c中打印字符串的二维数组

[英]printing a 2d array of string in c

i'm trying to print a 2d array of string as practice(i'm a newbie) with no success i've tried every combination i could think of still nothing i'm sure i'm doing a silly error somewhere i just can't see it here some of the example: using a pointer : 我正在尝试打印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++;
  }
}

here's the output: 这是输出:

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)

another version of the print function is like this : 打印功能的另一个版本是这样的:

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 get the same output, can anyone help me debug this ? 我得到相同的输出,有人可以帮我调试吗? and thx in advance 和提前

You are adding i as 1 which will not help in case of your two dimensional array as the next element will be at maxx location,so you can do something like this //here lim and max are defined in your program 您将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;
    }
}

and the second variant should be 第二个变体应该是

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";
  }
}

In short, it looks like you need to brush up on your pointers. 简而言之,您似乎需要重新梳理指针。 With your original print function: 使用原始打印功能:

void print(char *a)  
{
  int i=0;
  printf("the list of names include : \n");
  while(*(a) != '\0') {
    printf("%s\n", *(a+i));
    i++;
  }
}

You are printing the value at a + i every iteration. 您每次迭代都在+ i处打印该值。 This might sound like what you want, but what you actually pass to print is a pointer to an array of arrays of char (your compiler should be throwing a warning about incompatible pointer types). 这可能听起来像您想要的,但实际上传递给print是指向char数组数组的指针(您的编译器应发出有关不兼容的指针类型的警告)。 That is, the "proper" type of ps is (char *)[] . 也就是说, ps的“适当”类型是(char *)[] So in the print function you are only advancing the memory address by sizeof(char) with each iteration, whereas what you actually want is to increment it by sizeof(char) * maxx (the size of your array entries). 因此,在print功能中,每次迭代仅使内存地址增加sizeof(char),而实际需要的是将内存地址增加sizeof(char)* maxx(数组条目的大小)。 To implement this change, do the following: 要实施此更改,请执行以下操作:

  1. change declaration of print 更改print声明
    • void print(char (*)[maxx]);
  2. change to proper pointer type 更改为正确的指针类型
    • char (*ps)[maxx] = input;

And finally, change print function to something like: 最后,将打印功能更改为:

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++;
    }
}

You need not use the (a+i) syntax, as just advancing a by one each iteration accomplishes the same thing, and is possibly faster for large i. 您无需使用(a + i)语法,因为每次迭代仅将a向前推进即可完成相同的操作,并且对于大i而言可能更快。 And of course, as others have mentioned, double check your new line printing, I believe you want printf('\\n') . 当然,正如其他人提到的,仔细检查您的新行打印,我相信您需要printf('\\n')

You start on index 1 in your 2d array, you should start with index 0 您从2d数组的索引1开始,应该从索引0开始

int i=1;

Your print function takes an array of characters and then does a printf string of each character which makes no sense 您的打印函数需要一个字符数组,然后对每个字符执行一个printf字符串,这没有任何意义

void print(char *a)
{
  int i=0;
  printf("the list of names include : \n");
  while(*(a)!='\0')
  {
    printf("%s\n",*(a+i));
    i++;
  }
}

instead make it look like this 而是让它看起来像这样

void print(char *a[], int strings)
{
  int i = 0;
  for (; i < strings; ++i)
  {
    puts( a[i] );
  }
}

and call it with the number of strings you read 并用您读取的字符串数调用它

print(ps,i);

You would also be better off using fgets() instead of gets(), especially since your strings are max 25 chars so its easy to give a longer string. 使用fgets()而不是gets()也会更好,特别是因为您的字符串最多为25个字符,因此很容易给出更长的字符串。 fgets() lets you specify the max size of the string fgets(input[i],maxx,stdin) fgets()可让您指定字符串的最大大小fgets(input[i],maxx,stdin)

Your other function 您的其他功能

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]);
    }
  }
}

does a similar wrong assumption about the level of indirection 对间接级别做了类似的错误假设

arra[i][j] is one character but puts takes a string argument, so puts( arra[i][j] ); arra [i] [j]是一个字符,但是puts带有字符串参数,因此puts(arra [i] [j]); is not correct, you could try fputc( arra[i][j], stdout ) instead since fputc takes one character 不正确,您可以尝试使用fputc( arra[i][j], stdout )因为fputc需要一个字符

fix to 修复到

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM