0

I've made a C program that involves multi-dimensional arrays (it prints a multi-dimensional array of characters) to review a little, but I came across a bug.

My expected output of the program is:

.
.
.
A
.
.
.
.
.

However the output I get is:

.
.
A     //Not meant to be 'A' but rather a '.'
A
.
.
.
.
.    

I am wondering how I get that extra 'A' in position [0][2], and how I can fix this problem.

Here is my code:

#include <stdio.h>

void initArray(char shape[][2]);

main()
{
      char shape[2][2];
      int i, j;

      initArray(shape);
      shape[1][0] = 'A';
      printf("%c\n%c\n%c\n", shape[0][0], shape[0][1], shape[0][2]);  
      printf("%c\n%c\n%c\n", shape[1][0], shape[1][1], shape[1][2]);
      printf("%c\n%c\n%c\n", shape[2][0], shape[2][1], shape[2][2]);

      getchar();
}

void initArray(char shape[][2])
{
      int  i, j;

      for(i = 0; i < 3; i ++)
      {
            for(j = 0; j < 3; j++)
            {
                  shape[i][j] = '.';                  
            }
      }
}

Thank you very much =D

2 Answers 2

3

Because you need to loop till < 2 as shape is declared as shape[2][2]

 for(i = 0; i < 2;i++)
   for(j = 0; j < 2; j++)

This will iterate over the shape on row & columns 0-1 both inclusive

Sign up to request clarification or add additional context in comments.

3 Comments

This makes the program print only 4 characters. I've edited the program above, please check the edit
@C_Beginner_Learner i = 0; i < 3; i ++ will loop for 0,1,2 accessing shape[i][2] or shape[2][j] is an undefined behavior when shape is just declared as char shape[2][2]; If you need total 9 elements just use char shape[3][3]; and the current code will work fine
@POW so even in the edited program, the problem is that it should be declared as char shape[3][3]; rather than char shape[2][2];?
1

A possible way would be to avoid multi-dimensional arrays, and use plain arrays. Then instead of char shape[2][2]; you would declare

 char shape[4];

and code shape[2*i+j] instead of shape[i][j]. (BTW the compiler transforms the later into the former).

Use a debugger (like gdb) to see if i and j have meaningful values. And add assert(i>=0 && i<2) at appropriate places.

Remember that an array declared char arr[4]; (like my shape above) accepts only index from 0 to 3 (i.e. 4-1) i.e. use arr[0], ... arr[3] or arr[i] with the integral i between 0 and 3. Accessing arr[4] (or arr[17] or arr[i+1] when i is 3) is an undefined behavior (anything could happen per the C standard, including the collapse of the universe, which would be standard compliant). This particular error is quite common and called a buffer overflow. It is used in malware.

3 Comments

upvote, because multidimension arrays have another problem. If you think of multidimensional arrays as single arrays you like sometimes to do something like int array[5][5]; array[0][6] = x;whats adressing a suitable adress, but is leading into undefined behavior
Thank you, but the idea of this review was to review multi-dimensional arrays in particular
I still believe you should not use them. In 30+ years of C programming, I almost never used them.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.