0

I need to use a full 2D array inside a function. I can't use for, while, or do-while. I'm using CodeBlocks for Windows (C Language) Here is an example of code (this isn't the real code, just an example):

#include <stdio.h>
void reload(char* reload[2][6])
{
    printf(" _________________\n");
    printf("|%c|%c|%c|%c|%c|%c|\n", array[0][0], array[0][1], array[0][2], array[0][3], array[0][4], array[0][5]);
    printf("|%c|%c|%c|%c|%c|%c|\n", array[1][0], array[1][1], array[1][2], array[1][3], array[1][4], array[1][5]);
    printf(" -----------------\n");
}
int main()
{
    char* array[2][6];
    printf(" _________________\n");
    printf("|%c|%c|%c|%c|%c|%c|\n", array[0][0], array[0][1], array[0][2], array[0][3], array[0][4], array[0][5]);
    printf("|%c|%c|%c|%c|%c|%c|\n", array[1][0], array[1][1], array[1][2], array[1][3], array[1][4], array[1][5]);
    printf(" -----------------\n");
    reload(array);
    return 0;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
NickOlder
  • 37
  • 1
  • 3
  • so you have a 2 dimension array of char*, so technically a char***. For this case a fixed size array works(just check Jayram answer) otherwise concider malloc and don't forget to check your parameters. – Platypus Mar 09 '20 at 12:42
  • @Platypus: A two-dimensional array of `char *` is not `char ***`. Arrays are not pointers. – Eric Postpischil Mar 09 '20 at 12:43
  • @EricPostpischil So tell me how would you malloc a 2 dimensional array of char* ? – Platypus Mar 09 '20 at 12:47
  • @Platypus: `char *(*x)[Rows][Columns] = malloc(sizeof *x);` or `char *(*x)[Columns] = malloc(Rows * sizeof *x);`. – Eric Postpischil Mar 09 '20 at 12:51
  • @NickOlder: Do not edit questions in ways that render answers invalid or that negate the original question. When you edit the code to be “correct”, it no longer makes sense to readers that a question asks what is wrong with the correct code. If the question you wanted to ask was not about the error in the parameter name, then enter a new question instead of editing this one. – Eric Postpischil Mar 09 '20 at 12:53
  • @EricPostpischil so what if you want to pass it as parameter without knowing Rows and Colums? – Platypus Mar 09 '20 at 12:55
  • `char* array[2][6];` is a 2D array of _pointers_ not of characters. And you don't even initialize the array so what do you expect this to print? – Lundin Mar 09 '20 at 12:56
  • @Platypus: Then you need something other than a two-dimensional array. – Eric Postpischil Mar 09 '20 at 12:56
  • @Lundin A char* is technically a pointer to the first character of an array, so even though it's not optimal it works. – Platypus Mar 09 '20 at 12:58
  • @Platypus It works if you want 2x6 _strings_ yes, not if you want 2 strings with 6 characters each. Which isn't clear... – Lundin Mar 09 '20 at 12:59
  • @EricPostpischil Why? a char*** if you malloc it correctly, is a pointer to an array of array of char*. You can tell me it's not pretty. You can't tell me it's wrong. – Platypus Mar 09 '20 at 12:59
  • @Platypus *A char\* is technically a pointer to the first character of an array, so even though it's not optimal it works.* How can you define printing a `char *` with `%c` as "works"? It's undefined behavior to use an incorrect format specifier in a `printf()` call. – Andrew Henle Mar 09 '20 at 13:00
  • 1
    @Platypus: No. A `char ***` may be used with three subscripts to get a `char` or two subscripts to get a `char *`, but it is a pointer to a pointer to a pointer; it is not an array of arrays or an array of arrays of arrays. Many people conflate them because arrays are automatically converted to pointers in many expressions, but they **are different** because arrays are **not converted** to pointers in some expressions. Differences arise in `sizeof` and taking the address and in declarations. – Eric Postpischil Mar 09 '20 at 13:02
  • @Platypus *a char\*\*\* if you malloc it correctly, is a pointer to an array of array of char\*.* No, it's not a pointer to an array of `char *` It's a pointer to an array of pointers to arrays of pointers to arrays of `char`. – Andrew Henle Mar 09 '20 at 13:02
  • @Platypus See [Correctly allocating multi-dimensional arrays](https://stackoverflow.com/questions/42094465/correctly-allocating-multi-dimensional-arrays), it explains the topic of pointer-to-pointer vs array and why the former might be unsuitable and slow in many cases. – Lundin Mar 09 '20 at 13:06
  • @AndrewHenle Mb i said it wrong, when I say pointer to array of array it means in my head pointer to array of pointers to an array of pointers, since everything is adresses i just assume than from the point you malloc it an array is it's adress. – Platypus Mar 09 '20 at 13:08
  • @Lundin I'll check that out thank you. – Platypus Mar 09 '20 at 13:09
  • change `void reload(char* reload[2][6])` to `void reload(char reload[2][6])` –  Mar 14 '20 at 22:40

3 Answers3

1

You have a mistake in the parameter name for the reload function; it should be array, not reload:

#include <stdio.h>
void reload(char* array[2][6])
{
    printf(" _________________\n");
    printf("|%c|%c|%c|%c|%c|%c|\n", array[0][0], array[0][1], array[0][2], array[0][3], array[0][4], array[0][5]);
    printf("|%c|%c|%c|%c|%c|%c|\n", array[1][0], array[1][1], array[1][2], array[1][3], array[1][4], array[1][5]);
    printf(" -----------------\n");
}
int main()
{
    char* array[2][6];
    printf(" _________________\n");
    printf("|%c|%c|%c|%c|%c|%c|\n", array[0][0], array[0][1], array[0][2], array[0][3], array[0][4], array[0][5]);
    printf("|%c|%c|%c|%c|%c|%c|\n", array[1][0], array[1][1], array[1][2], array[1][3], array[1][4], array[1][5]);
    printf(" -----------------\n");
    reload(array);
    return 0;
}
Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Jayram
  • 18,820
  • 6
  • 51
  • 68
  • @EricPostpischil the original post was edited with this answer, check the unedited version of the original post, you'll see there is a problem with the parameters. – Platypus Mar 09 '20 at 12:51
  • This answer isn't helpful and doesn't make any more sense: you are printing the contents of a _pointer_ as if it was a character. That is, if I write `char* array[2][6]={(void*)0x00000041};` it starts printing `A` when I run it on my little endian PC. – Lundin Mar 09 '20 at 13:02
1
  • If you want and actual 2D array of characters (that is, 2 strings), then it's simply: char array[2][6];.
  • Initialize that array before using it. For example: char array[2][6] = {"hello", "world"};
  • Change the function accordingly and get rid of the nonsense parameter name:
    void reload(char array[2][6]).

It will then print:

 _________________
|h|e|l|l|o| |
|w|o|r|l|d| |
 -----------------

Assuming that the character presentation of the null terminator is "print nothing" on your system.

Lundin
  • 195,001
  • 40
  • 254
  • 396
  • OP does state that the code in the question is an “example,” not their real code, and does not state what problem they are asking about. So we do not know whether they are asking about a compiler error message caused by the `reload` parameter name or a compiler error message caused by mismatched types or whether either or both of these are caused by their defective construction of an “example” and the real question they wanted to ask about is something else. The question was already answered based on the first premise and ought to be closed. The OP should enter a new, better composed question. – Eric Postpischil Mar 09 '20 at 13:15
0

You have also another problem here: you pass the array of pointers and you print the characters.

0___________
  • 60,014
  • 4
  • 34
  • 74