-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++;
  }
} 
2
  • 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. Commented Sep 18, 2014 at 16:36
  • For twoDArray since that's what I am trying to print Commented Sep 18, 2014 at 16:37

1 Answer 1

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;
}
Sign up to request clarification or add additional context in comments.

1 Comment

I know @RyanJ I just had to wait 1 min for some reason. Thanks for the help anyway..!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.