1

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

6
  • What does your debugger say? Commented Dec 9, 2013 at 10:57
  • beside the output of segmentation duped it doesn't complain of anything else Commented Dec 9, 2013 at 10:59
  • Don't type the result yourself....post your input, and what you expect for output, and the output more clearly. Commented Dec 9, 2013 at 11:00
  • A debugger doesn't complain, it is used to trace through your program and look at the variables. Did you do that? Simply starting a program in a debugger and waitiing for it to show all errors doesn't work. Commented Dec 9, 2013 at 11:00
  • Not sure if your problem, but did you mean printf("\n"); instead of print("\n"); Commented Dec 9, 2013 at 11:00

4 Answers 4

2

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').

Sign up to request clarification or add additional context in comments.

Comments

2

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

3 Comments

thx it worked it displayed the names but at the end i'm getting weird ouput here's a screenshot : imageshack.us/photo/my-images/585/grgy.png
that is because we are trying to display all the elements in the array..even though you have entered only five elements it will print all the 10 elements which will be some garbage value after number of entered elements...you can store the number of elements input by user and use it in print() function instead of while(i<limmaxx) you have to use while(i<nummaxx)..try this
... or just add a proper initialisation as per BLUEPIXY's answer: char input[lim][maxx] = { {'\0'}};
1

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

Comments

1

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

4 Comments

Note that "quit" even lead to print.
Good answer. However (I hate to be that guy) I not only answered first, but I actually offered an explanation...
yes i noticed i'm still not done with this,@surfreak i appriciate ur efforts too <3
hint "quit" --> "\0uit".

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.