# 2D Array's, assigning chars.

This is a discussion on 2D Array's, assigning chars. within the C Programming forums, part of the General Programming Boards category; I've become quite frustrated trying to assign input into 2D Arrays. What's the best way to do it? I have ...

1. ## 2D Array's, assigning chars.

I've become quite frustrated trying to assign input into 2D Arrays. What's the best way to do it?

I have a 2D array of char type that is initially empty. I want to print out the array once the user has entered 5 rows of input.

I've tried using fgets to assign each character to each position:

Code:
```#define ROWS 5
#define COLMNS 5

array[5][5];

while(y < COLMNS) {
fgets(board[i], ROWS, stdin);
i++;
y++  ;
}```
I think when the user enters 'abcd' for example, fgets assigns the whole input to [0,0] (during the first loop) instead of each character to [1, 0], [2,0], ..., [5,0] ???

I also tried making a temporary 1D array like so:

Code:
```#define ROWS 5
#define COLMNS 5

int y = 0;
int i = 0;

array[5][5];
line[5];

while(y < COLMNS) {
for(i = 0; i < 5; i++) {
line[i] = getchar();
line[i] = array[i][y]
}
i = 0;
y = y + 1;

}```
The logic behind the above was to assign each row individually in the for loop and when it reached 5 it would switch to the next row.

I also tried a method similar to above but using strcpy to copy the 1D string into the first row of the 2D array but I wasn't sure how to do it correctly.

I really would appreciate help, sorry if some of these questions seem stupid, I am a bit of a n00b. Kind regards.

2. array[5][5]; - you miss the type

fgets assigns the whole input to [0,0] - you are wrong if array[0][0] is a char it cannot hold 5 chars

fgets(board[i], - you do not have board defined

while(y < COLMNS) - you are reading the whole line - get rid of this column loop

second example has even less sence in it...

3. Sorry, I actually didn't copy the code from the program, I just rushed it quickly. This is how it was:

Code:
```#define ROWS 5
#define COLMNS 5

char array[5][5];

while(y < COLMNS) {
fgets(array[i], ROWS, stdin);
i++;
y++  ;
}```
Yeah the second example is quite stupid, but what I want is just the user to enter input, hitting return for a new line(I'm actually trying to avoid using fgets to be honest). Example :

Enter chars:
1 . 4 a g
1 2 3 4 5
. . ! t x
a b c d e
5 4 3 2 1

1 . 4 a g
1 2 3 4 5
. . ! t x
a b c d e
5 4 3 2 1

4. Maybe something like:
Code:
```#define ROWS 5
#define COLMNS 5

char array[ROWS][COLMNS];
char line[6];

y = 0;
while(y < ROWS) {
fgets(line, sizeof line, stdin);
memcpy(array[y], line, COLMNS);
y++  ;
}```
Or using getchar():
Code:
```#define ROWS 5
#define COLMNS 5

char array[ROWS][COLMNS];
int ch, r, c;

for (r=0; r<ROWS; r++)
{
c = 0;
while ((ch = getchar()) != '\n')
{
if (c < COLMNS) array[r][c++] = ch;
}
}```

5. > I think when the user enters 'abcd' for example,
Don't forget the \n and \0

fgets will store the abcd\0 on the first round, but will then immediately return with \n\0 for the second round.

char buff[BUFSIZ];
fgets( buff, sizeof buff, stdin );

Then you validate the input in buff (is it too long say), then copy that data to where you really want it to be.

6. Thanks for the help guys, but I'm still having problems. So far I've got this:

Code:
```
int i, j;
char input_line[6];
char array[5][5]

for(i = 0; i < 5; i++) {
fgets(input_line, 6, stdin);
for(j = 0; j <= 5; j++) {
if(input_line[i] != '\n') {
array[i][j] = input_line[j];
}
}
}```
But the output I'm getting from:
1
2
3
4
5

Is this:

1
(weird characters)
3
4
5

I'm assuming it's a problem with the \n or \0 but not sure. Is there anyway I can use 'strcpy' to copy the 1D array 'input_line' to the first line of the 2D array? So I could then loop it to get the 5 rows.

7. Should that not be if(input_line[j] != '\n')?

8. In addition to tabstop's comment, and based on Salem's comment, I think you might be better making the array size input_line larger, so that the newline also gets stored.
Code:
```char input_line[20];
.
.
fgets(input_line, 20, stdin);```
This ensures the newline also gets read, and isn't read by the next call to fgets().

9. There are FAQ entries on using fgets() and stripping the \n from the input.

10. Heh... good ol' Salem. Sometimes I think Salem just logs in and does four searches then logs off.

Search one: Salem inside a thread
Search two: void main()
Search three: fgets()
Search four: fflush(stdin)

Then on the other hand is Kermi... who I strongly believe only logs in when one dares mention Kermi... That and mass hysteria or perhaps catastrophic forum failure.