# Thread: more help...2d arrays in structures - sscanf

1. ## more help...2d arrays in structures - sscanf

Code:
```0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0
0  3  3  3  3  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0 15  0
0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0
0  3  0  0  0  0  0  7  0  0  0  0  0 11  0  0  0  0  0 15  0  0  0  0
0  3  0  0  0  0  0  7  7  7  7  0  0 11 11 11 11  0  0 15  0  0  0  0
0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0  0```
And i need to output it to my image.coords[][] 2d array. For example image.coords[0][k] would all be 0. image.coords[1][1] should be 3. Here's my code to do so, why is it outputting all 0's? I've been looking at it for a while, and the longer I look at it, the farther away I get to figuring it out. I'm guessing its some simple little error, but i can't figure it out.

Code:
```    k = j = 0;
while( fgets(buffer, MAXLINE, in) )
{
k=i=0;
while(1)
{
while(!isspace(buffer[i]))
{
sscanf(buffer, "%d", &image.coords[j][k]);
k++;
i++;
}
while(isspace(buffer[i]))
{
i++;
}
if(buffer[i]=='\0')
{
break;
}

printf("%d ", image.coords[j][k]);

}

printf("\n");
j++;
}```

2. Why are you incrementing both row and column any time you find a number?

Quzah.

3. Originally Posted by quzah
Why are you incrementing both row and column any time you find a number?

Quzah.
I'm incrementing the buffer (i) and the column (k) whenever I find a number. I increment the row (j) at the very end, when the first while loop has to go to the top again.

4. Ah, right. I must have missed that due to your overly descriptive variable names. At any rate:
Code:
```while(!isspace(buffer[i]))
{
sscanf(buffer, "%d", &image.coords[j][k]);```
While we don't have a space at this particular character, read the first entry of the buffer over and over again into various spots in my 'coords' array.

Quzah.

5. Originally Posted by quzah
While we don't have a space at this particular character, read the first entry of the buffer over and over again into various spots in my 'coords' array.
Yeah that's my problem, I dunno how to assign say buffer[0], buffer[1], and buffer[2] to one array value instead of three. What should I do?

6. ...a hint?...
Code:
```#include <stdio.h>
#include <ctype.h>

int main(void) {
int coords[7][24];
char buffer[] = "0  3  3  3  0  0  0  7  7  7  0  0  0 11 11 11  0  0  0 15 15 15 15  0";

char *pbuffer = buffer;
int j,k;

/* for row "j" */
j = 3;
for ( k=0 ; k<24 ; k++) {
while(isspace(*pbuffer)) pbuffer++;
sscanf(pbuffer, "%d", &coords[j][k]);
while(isdigit(*pbuffer))  pbuffer++;
}

for ( k=0 ; k<24 ; k++) {
printf(" %d",coords[j][k]);
}

return 0;
}```

7. Originally Posted by cdave
...a hint?...
no b/c i'm going to be reading in different files, so i can't initialize the array to anything