-1

I have this code written in C:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print_array(char** array, int height, int width);

int main()
{
    int height, width;
    char **array = 0;

    printf("Give height board size:");
    scanf("%d", &height);
    printf("Give width board size:");
    scanf("%d", &width);

    print_array(array, height, width);


    return 0;
}

void print_array(char** array, int height, int width)
{
    int i, j;

    printf("\n |"); for (j = 0; j < width; j++) printf("-");
    printf("|\n");
    for (i = 0; i < height; i++)
    {
        printf(" |");
        for (j = 0; j < width; j++) printf("%c", array[i][j]);
        printf("|\n");
    }
    printf(" |"); for (j = 0; j < width; j++) printf("-");
    printf("|");
}

The expected result was this for 10x10

|----------|
|          |
|          |
|          |
|          |
|          |
|          |
|          |
|          |
|          |
|          |
|----------|

But the actual result for whatever number I give to Height is

E.g. Height 10 and Width 20

|--------------------|
|

If I run with Visual Studio I actually get and error code on line 32 which is the following

Exception thrown: read access violation. array was 0x1110112.

Line 32

for (j = 0; j < width; j++) printf("%c", array[i][j]);

3 Answers3

4

Since you are declaring array as a pointer to a pointer to char, but not defining it in read-only memory in the same line as declaration, you have to use the functions malloc to first allocate the array, and then later free it:

int i;
array = malloc(sizeof(char *) * height);
for(i = 0; i < height; i++)
    array[i] = malloc(width);

// Use array....

for(i = 0; i < height; i++)
    free(array[i]);

free(array);

That's if you actually need a 2-dimensional array. For your use case of printing out a square of arbitrary width and height, you don't.

Also note that the choice of allocating height as the first dimension, and then width as the second dimension, is completely arbitrary and it could just as easily be the other way around.

Govind Parmar
  • 20,656
  • 7
  • 53
  • 85
1

array is an uninitialized pointer-to-pointer. Attempting to dereference that array via the [] operator invokes undefined behavior.

You don't need an array here at all. Just print a space:

printf(" |");
for (j = 0; j < width; j++) printf(" ");
printf("|\n");
dbush
  • 205,898
  • 23
  • 218
  • 273
  • 2
    I suspect the expectation was the (nonexistent) "array" was loaded with spaces already and the goal of the function was to exhibit that misinformed fact. Otherwise the function would have been more aptly named, `print_box`. =O – WhozCraig Nov 19 '18 at 21:05
1

This answer is probably helpful to you. It gives an answer similar to @Govind Parmar's, but with this solution the array won't allocate a contiguous region of memory, which could give problems with functions that assume this.

Instead, you could do:

// ConsoleApplication1.cpp : Defines the entry point for the console application.
//


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

void print_array(char** array, int height, int width);

int main()
{
    int height, width;
    char** array;
    char* temp;

    printf("Give height board size:");
    scanf_s("%d", &height);
    printf("Give width board size:");
    scanf_s("%d", &width);

    array = malloc(height * sizeof(char*));
    temp = malloc(height * width * sizeof(char*));

    for (int i = 0; i < height; i++) {
        array[i] = temp + (i * width);
    }

    print_array(array, height, width);

    free(temp);
    free(array);

    return 0;
}

void print_array(char** array, int height, int width)
{
    int i, j;

    printf("\n |"); for (j = 0; j < width; j++) printf("-");
    printf("|\n");
    for (i = 0; i < height; i++)
    {
        printf(" |");
        for (j = 0; j < width; j++) printf("%c", array[i][j]);
        printf("|\n");
    }
    printf(" |"); for (j = 0; j < width; j++) printf("-");
    printf("|");
}
pooh17
  • 64
  • 4