简体   繁体   中英

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 :

#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

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. 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). That is, the "proper" type of ps is (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). To implement this change, do the following:

  1. change declaration of 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. And of course, as others have mentioned, double check your new line printing, I believe you want printf('\\n') .

You start on index 1 in your 2d array, you should start with index 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

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() lets you specify the max size of the string 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] ); is not correct, you could try fputc( arra[i][j], stdout ) instead since fputc takes one character

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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