how to display 2d string array

M

manish sahu

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char c[4][3]={
"msh",
"xyz",
"abc",
"CCC",
};
int i,j;
clrscr();
for(i=0;i<=3;i++)
{
for(j=0;c[j]!='\0';j++)
{
printf("%s",c);
}
printf("\n 1 ");
}
getch();
}



i want to print above given 2d string array but this is not giving
correct output plz see the code and explain me the coreect way to
print 2d char array.
 
J

Joachim Schmitz

manish said:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char c[4][3]={

change to
char c[4][4]={
to cater for the terminating \0 of the 4 strings
"msh",
"xyz",
"abc",
"CCC",
};
int i,j;
clrscr();
for(i=0;i<=3;i++)
{
for(j=0;c[j]!='\0';j++)
{
printf("%s",c);
}
printf("\n 1 ");
}
getch();
}



i want to print above given 2d string array but this is not giving
correct output plz see the code and explain me the coreect way to
print 2d char array.


Bye, Jojo
 
M

Morris Keesan

#include<stdio.h>
int main()
{
char c[4][4]={"msh","xyz","abc","CCC",};
int i,j;
for(i=0;i<=3;i++)
printf("%s\n",c);

getchar(); //wait for '\n'; Or you can use system("pause");

return 0;

}

Always follow standard while writing programs.


The question is, "which standard?" If it's the C89 or C90 standard,
there's
a syntax error following getchar();

Also, the array in question isn't actually a 2d string array, it's a 2d
char array,
and you're not accessing it as if it were a 2d array.

The OP's code attempts to access the array as a 2d array, but falls down
in the
printf. A minimal fix (ignoring all of the non-standard problems) is

int i, j;
for (i = 0; i <= 3; i++) {
for (j = 0; j < 3; j++ ) {
putchar(c[j]);
}
putchar('\n');
}

The original code used j as the index for the inner loop, then failed to
use it within the loop, printing the entire row of the array multiple
times.
 
B

Ben Bacarisse

Morris Keesan said:
The OP's code attempts to access the array as a 2d array, but falls
down in the
printf. A minimal fix (ignoring all of the non-standard problems) is

int i, j;
for (i = 0; i <= 3; i++) {
for (j = 0; j < 3; j++ ) {
putchar(c[j]);
}
putchar('\n');
}


Another fix is to remove the inner loop altogether and replace it with

printf("%.3s", c);

or even:

printf("%.*s", (int)sizeof c, c);

<snip>
 
N

Nick Keighley

#include<stdio.h>

it's clearer if you lay it out like this

#include said:
#include<conio.h>

non-standard header
#include<string.h>

unused header
void main()

int main (void)

is the correct declaration of main
{
        char c[4][3]={
                        "msh",
                        "xyz",
                        "abc",
                        "CCC",
                    };
        int i,j;
        clrscr();

non-standard function
        for(i=0;i<=3;i++)
        {
               for(j=0;c[j]!='\0';j++)
                {
                        printf("%s",c);
                }
                printf("\n 1 ");
        }
        getch();


non-standard function

main should return something

return 0;
 
N

Nick Keighley

#include<stdio.h>
int main()
{
             char c[4][4]={"msh","xyz","abc","CCC",};
             int i,j;
             for(i=0;i<=3;i++)
                     printf("%s\n",c);
             getchar();   //wait for '\n'; Or you can use system("pause");
 return 0;
}
Always follow standard while writing programs.

always?
The question is, "which standard?"  If it's the C89 or C90 standard,  
there's a syntax error following getchar();

I intended to follow C99.
Also, the array in question isn't actually a 2d string array, it's a 2d  
char array,
and you're not accessing it as if it were a 2d array.

Sorry,I didn't find any problem with it, will you please clarify ?


It's more a teminological problem. You don't have a 2D array of
strings
you have a 2D array of chars. You *want* a 1D array of strings.
A 2D array of strings would be

char *colour [2][3] =
{
{"red", "crimson", "vermillion"},
{"beige", "cream", "tope"}
};
Even on efficiency ground removing the inner loop & using printf()
once will do better.

why? printf() is probably quite expensive. How can printf() avoid
looking at each char one by one?
The OP's code attempts to access the array as a 2d array, but falls down  
in the
printf.  A minimal fix (ignoring all of the non-standard problems) is
   int i, j;
   for (i = 0; i <= 3; i++) {
       for (j = 0; j < 3; j++ ) {
          putchar(c[j]);
       }
       putchar('\n');
   }

The original code used j as the index for the inner loop, then failed to
use it within the loop, printing the entire row of the array multiple  
times.

I have nothing to say for this


you agree?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top