1

So I have a 2D array that I want to use later. Right now I just want to fill the empty spots.

So far I've just been messing around with array types and different default values. From my understanding a new array is filled with '0', I have tried NULL aswell.

int r = 5;
int c = 5;
int i;
int j;
int k = 0;
int area = r*c;

const char *array[r][c]; //tried char array[r][c] and other types

Setup my initial values and array here.

while(k< area){
    for (j = 0; j < c; j++){
        for (i = 0; i<r; i++){
            if (array[i][j] == 0){
                board[i][j] = ".";
            }
            printf("%c",aray[i][j]); 
            if (i = r - 1){
                printf("\n");
            }
            k++;

        }
    }

}

This is where I try replacing all non filled values (all of them at this point) with ".", so the output should be a row of 5x5 dots. Instead I get weird letters and numbers. I have tried %s insead of %c, and no luck there but the output was different. Where I do %s I get some dots, but still not on a grid and the weird values show up.

Also Im pretty sure printf in a for loop, by default does it on a new line so I won't get the grid, so is there a better way of doing this?

gsamaras
  • 71,951
  • 46
  • 188
  • 305
Yee Kee
  • 137
  • 6
  • You can skip the `if(i = r - 1)` check by just doing the `printf("\n")` outside the `i` loop. Also, that is not checking equality but assigning a new value to `i` – lxop Feb 07 '19 at 09:16

3 Answers3

1

What you have is an array of pointers. This would be suitable for a 2D array of strings, but not for a 2D array of characters. This isn't clear from your question, so I'll assume that you actually want a 2D array of characters. The syntax is: char array [r][c];.

Notably, since you used r and c which are run-time variables, this array is a variable-length array (VLA). Such an array cannot be placed at file scope ("global"). Place the array inside a function like main().

In order to use VLA you must also have a standard C compiler. C++ compilers and dinosaur compilers won't work.

Since you will have to declare the VLA inside a function, it gets "automatic storage duration". Meaning it is not initialized to zero automatically. You have to do this yourself, if needed: memset(array, 0, sizeof array);. But you probably want to initialize it to some specific character instead of 0.

Example:

#include <stdio.h>
#include <string.h>

int main (void)
{
  int r = 5;
  int c = 5;
  char array [r][c];
  memset(array, '#', sizeof array);

  for(size_t i=0; i<r; i++)
  {
    for(size_t j=0; j<c; j++)
    {
      printf("%c", array[i][j]);
    }
    printf("\n");
  }
}

Output:

#####
#####
#####
#####
#####
Lundin
  • 195,001
  • 40
  • 254
  • 396
0

From my understanding a new array is filled with '0'

const char *array[r][c];

No*, you have fill it yourself in a double for loop, like this:

for(int i = 0; i < r; ++i)
  for(int j = 0; j < c; ++j)
    array[i][j] = 0

since your structure is a variable sized array.

Instead I get weird letters and numbers

This happens because your code invokes Undefined Behavior (UB).

In particular, your array is uninitialized, you then try to assign cells to the dot character, if their value is already 0.

Since the array is not initialized, its cells' values are junk, so none satisfied the condition of been equal to 0, thus none was assigned with the dot character.

You then print the array, which still contains garbage values (since it was never really initialized by you), and the output is garbage values.


* As stated by @hyde, this is true for local non-static arrays (which is most probably your case). Statics and globals are default initialized (to zero if that was the case here).

gsamaras
  • 71,951
  • 46
  • 188
  • 305
  • Note: this only applies to local variables. Since question lacks MCVE, we can only assume `array` is a local variable. – hyde Feb 07 '19 at 09:18
  • 1
    @hyde I updated my answer already with your spot-on comment. I am almost sure it's a local array, since it explains the situation perfectly. – gsamaras Feb 07 '19 at 09:19
  • @gsamaras Thanks for the replies. I had already tried that but I had already tried that and got an issue while compiling. "error: variable-sized object may not be initialized" Also, yes it is local – Yee Kee Feb 07 '19 at 09:36
  • You can't initialize a VLA. – Lundin Feb 07 '19 at 09:50
  • @YeeKee you are welcome, and I am sorry for my mistake. The error is right (of course), variable sized arrays cannot be initialized like this, I updated my answer with a solution that uses a double for loop (different than the answer of Lundin, but effectively the same thing). – gsamaras Feb 07 '19 at 12:27
  • @Lundin thank you for downvoting, my answer was wrong, now I updated, please review it! – gsamaras Feb 07 '19 at 12:29
  • @gsamaras I would use simple `setmem(array, 0, sizeof array);` for zeroing a freshly defined VLA. – hyde Feb 07 '19 at 12:44
  • @hyde it seems that this is not Standard C, it's maybe from Embarcadero? However, I see your point, by the analogous solution is already given by Lundin with `memset()`. – gsamaras Feb 07 '19 at 14:24
-1

You have several problems:

  1. You are declaring a pointer to the array you want, not the array
  2. Whenever R and C are not compile time known, you can't use a built in array. You might can however use VLAs (C99 as only C standard has VLAs mandatory, C11 made them optional again), which seems like a built in array with a size not known at compile time, but has very important implications, see : https://stackoverflow.com/a/54163435/3537677
  3. Your array is only zero filled, when declared as a static variable.
  4. You seem to have mistake the assign = operator with the equal == operator

So by guessing what you want:

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

#define R 5
#define C 5
int r = R;
int c = C;
int i;
int j;
int k = 0;
int area = R*C;

const char array[R][C];


int main() {
    while(k< area){
        for (j = 0; j < c; j++){
            for (i = 0; i<r; i++){
                if (array[i][j] == 0){

                }
                printf("%c",array[i][j]);
                if (i == r - 1){
                    printf("\n");
                }
                k++;

            }
        }

    }

    //or

    char** dynamic_array = malloc(r * c);
    if (dynamic_array == NULL) {
        perror("Malloc of dynamic array failed");
        return EXIT_FAILURE;
    }
    memset(dynamic_array, '0', r*c);

    k = 0;

    while(k< area){
        for (j = 0; j < c; j++){
            for (i = 0; i<r; i++){
                if (dynamic_array[i][j] == 0){

                }
                printf("%c",dynamic_array[i][j]);
                if (i == r - 1){
                    printf("\n");
                }
                k++;

            }
        }

    }

    return 0;
}
Superlokkus
  • 4,731
  • 1
  • 25
  • 57