two dimensional char to char pointer array

passing the two dimensional array to the function which receives an array of pointers is being a problem. i can't figure it out.

this is my code so far:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>

void printTable(char *tacToe[], int row, int col){
    int i, x, y;

    for(i = 0; i < col; i++){
        printf("%3d", i+1);
    }

    printf("\n");

    for(x = 0; x < row; i++){
        printf("%2d", x+1);
        for(y = 0; y < col; y++){
            if(y == 0){
                printf("%c", tacToe[x][y]);
            }
            printf("%3c", tacToe[x][y]);
        }
    }
}

void initTable(char *tacToe[], int row, int col){
    int x, y;
    for(x = 0; x < row; x++){
        for(y = 0; y < col; y++){
            tacToe[x][y] = '_';
        }
    }
}

int main(void){
    int i;
    char tacToe[3][3];
    initTable(tacToe[3][3], 3, 3);
    printTable(tacToe[3][3], 3, 3);
    scanf("%d", &i);
}


also, i get warnings when i include the size of the array and a compile error when i don't. either way it doesn't work.

i tried playing with the index of the array i will pass but i get warnings and the program doesnt run.
Last edited on
This always seems to get new guys. Search for Arrays as parameters here: http://www.cplusplus.com/doc/tutorial/arrays/
Pay attention to how you pass an array as a variable to the function. Also the bounds declration in the function.
i know how to pass arrays to other arrays in the parameter list. well, at least i think i do. my problem is when passing it to an array of pointers.

so yeah i tried having the first bracket on the passing being empty while the second not. i get a compiler error. i really dont know why. regardless, it should still work since the compiler, even though reads the number within the first bracket, it doesnt really do anything.
Last edited on
Line 35 in your code is actually the issue I was refering to, check that against the example in the Tutorial:
1
2
3
4
5
6
7
8
int main ()
{
  int firstarray[] = {5, 10, 15};
  int secondarray[] = {2, 4, 6, 8, 10};
  printarray (firstarray,3);
  printarray (secondarray,5);
  return 0;
}


"printarray(...)" is a user defined function in this case.
Last edited on
it does not run when simply passing the array name. there are warnings as well. well, it compiles but the program does not run.
Last edited on
Ok, I seem to be dancing around this too much so I'll open myself up to potential crisism and just say:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 #include<stdio.h>

void printTable(char tacToe[][3], int row, int col){ /*See What I Did Here*/
    int i, x, y;

    for(i = 0; i < col; i++){
        printf("%3d", i+1);
    }

    printf("\n");

    for(x = 0; x < row; i++){
        printf("%2d", x+1);
        for(y = 0; y < col; y++){
            if(y == 0){
                printf("%c", tacToe[x][y]);
            }
            printf("%3c", tacToe[x][y]);
        }
    }
}

void initTable(char tacToe[][3], int row, int col){ /*And Here*/
    int x, y;
    for(x = 0; x < row; x++){
        for(y = 0; y < col; y++){
            tacToe[x][y] = '_';
        }
    }
}

int main(void){
    int i;
    char tacToe[3][3];
    initTable(tacToe, 3, 3); /*And Here*/
    printTable(tacToe, 3, 3); /*And Here*/
    scanf("%d", &i);
}


You also have an issue with an infinite loop, but this will at least compile. If you need assitance with the loop ask away I'll be here for a while.

EDIT: See line 12 for your infinite loop problem, this gets me when I copy paste stuff to.
Last edited on
the loop was a typo. i did a change in variables, i must have forgotten to change that. also, i was trying to make the function as 'reusable' as possible and so i wanted to use an array of pointers. it was not simply about making the program work for me since i can pretty much do that.
Ok so... It works now? I'm confused it sounds like you're done right?
it works when i use the array as the parameter, but not when i use a pointer.
That's because it kind of is a pointer, which is why the error you get probably says something about converting a "pointer" to a "pointer to a pointer". Could you copy paste the error so that we know for sure?
expected 'char **' but argument is of type 'char (*) [3]'

yes, i do realize that arrays are also pointers. but i dont know why it doesnt work. it should work is what my head is saying. two dimensional arrays are also pointers to pointers but are constant and have already saved space. what i want it to do is just to point to that place in memory.

now, i dont know but maybe assigning a single pointer to a single row of my two dimensional array will work but i dont want that. i want it to be able to accept two dimensional arrays of any size and not just a 3x3 array. for seat reservations perhaps.
oh snap, i got it to work. i found a thread at another forum asking an almost similar question.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include<stdio.h>

void printTable(char (*tacToe)[3], int row, int col){
    int i, x, y;

    for(i = 0; i < col; i++){
        printf("%3d", i+1);
    }

    printf("\n");

    for(x = 0; x < row; x++){
        printf("%d", x+1);
        for(y = 0; y < col; y++){
            if(y == 0){
                printf("%2c", tacToe[x][y]);
            }
            printf("%2c", tacToe[x][y]);
        }
        printf("\n");
    }
}

void initTable(char (*tacToe)[3], int row, int col){
    int x, y;
    for(x = 0; x < row; x++){
        for(y = 0; y < col; y++){
            tacToe[x][y] = '_';
        }
    }
}

int main(void){
    int i;
    char tacToe[3][3];
    initTable(tacToe, 3, 3);
    printTable(tacToe, 3, 3);
    scanf("%d", &i);
}


my question now is, why and how did it work?

i tried it without the parenthesis and with the bounds on the parameter list but it didnt work. what magic did the parenthesis do?

i thought about it and i searched a little and found that the braces have higher precedence than the dereference operator. and i thought about it more and figured that without the parenthesis, i was making a pointer to an array and when with the parenthesis i was making an array of pointers. now i thought about it some more and i was wondered why is that so different? they should be the same really, right?

i think i just pretty much answered my own question. going *ary[] is making basically a pointer to a pointer i think although i dont know who you would use that. while going (*ary)[] makes an array of pointers. what i want to pass is simply an address of an array, rather than an address of a pointer to a pointer(?). hmm the quirks are fun. :D <3
Last edited on
Topic archived. No new replies allowed.