Hi there I am currently teaching myself C and I have to create a program with a 2d array that receives upto 5 names and prints them out. No pointers should be used :(. So I am wondering can anybody help me with this.

#include <stdio.h>

int main(int argc, const char * argv[])
{

    char i2DArray[1][5];//1 Row and 5 columns

    int x;
    int y;


    //Initializing the 2d array
    for ( x = 0; x < 1 ; x++ ){
        for ( y = 0; y < 5; y++){
            printf("Enter the name of your friend: ");

            scanf("%s", &i2DArray[x][y]);

        }
    }


    //For loop for printing out the input
    for ( x = 0; x < 1; x++ ){
        for ( y = 0; y < 5; y++){

            printf("\t%s", *i2DArray);
        }
    }

            return 0;
}

This is the output I get Bleh :(. How can I print all the words in full? Please help I have searched everywhere

Enter the name of your friend: hello
Enter the name of your friend: there
Enter the name of your friend: people
Enter the name of your friend: I 
Enter the name of your friend: know
    htpIknow    htpIknow    htpIknow    htpIknow    htpIknowProgram ended with exit code: 0

what you want is something like this:

char i2DArray[5][20]

That will hold 5 names, and each name can be up to 20 characters long.

Then on lines 13-17: Note that you only need one loop, not two loops.

 for ( y = 0; y < 5; y++){
     printf("Enter the name of your friend: ");
     scanf("%s", i2DArray[y]);
 }   

Think about what you are doing, inputing a 1 x 5 array of strings.

The first question is is that what you really meant because an array index with a size of 1 is quite odd and rarely used? For example this char i2DArray[1][5]; and this char i2DArray[5]; allocate exactly the same amount of data, 5 char. So were you trying for an array of 5 names or a matrix 1 x 5 also, as it happens, containing 5 names?

If the answer is an array of 5 names then what is a name? it's a string and what is a string? it's an array of char. Assuming that we can set a maximum size for a string containing a name, to say 50 characters, we could define a type for a name string

typedef char namestring[50];

If we want an array of those then it would look something like

namestring i2DArray[5];

This is a 2d array because namestring is an array, we can combine these to produce the array without using a typedef, subsitute i2DArray[5] into the typedef line in place of namestring and remove the typedef giving

char i2DArray[5][50];

If you actually wanted a matrix 1 x 5 of names the same argument applies and you would end up with

// 2 dimensional array of strings
namestring i2DArray[1][5];

// substitute i2DArray[1][5] into namestring typedef
char i2DArray[1][5][50];

a dimension for each dimension of the matrix plus one for the actual size of the string.

You get the output you are getting because each entry you write to is only 1 character past the previous entry so the second character of i2DArray[0][0] is the same character as i2DArray[0][1] so when you entered there it put the t in place of the e of hello.

At line 27 you should be using x and y as your array indexes, your current code just outputs the same thing 5 times.

commented: Excellent :) +14

Thanks guys I managed to solve this

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.