-2

I have this nested loops working but I am printing extra char at the end because I don't know how to terminate the null char in 2D array. Here is the code:

char arr[100];
char twoDArray[100][100];
int y = 0, x = 0,  h= 10, w = 10,j=0;
for(y = 0; y <= h; y++)
{
  for(x = 0; x <= w; x++)
  {
    twoDArray[y][x] = arr[j];
    printf("%c", twoDArray[y][x]);
    j++;
  }
} 
Kenneth
  • 37
  • 2
  • 4
  • Where is your data being initialized? This example shows only empty arrays being used. Also, did you mean to use `<=` in your for-loops? Usually that results in "extra" data being used because arrays in C are 0-based, and the max you'd be able to use is 0 to length-1. – Ryan J Sep 18 '14 at 16:36
  • For `twoDArray` since that's what I am trying to print – Kenneth Sep 18 '14 at 16:37

1 Answers1

2

For null character in c following character is used.

'\0'

Check out following link.

http://www.tutorialspoint.com/cprogramming/c_strings.htm

String termination - char c=0 vs char c='\0'

Just compare in loop.

if(twoDArray[y][x]=='\0')
{
     break;
}
Community
  • 1
  • 1
Nirav Kamani
  • 3,192
  • 7
  • 39
  • 68