lephleg
lephleg

Reputation: 1764

2-dimension array of chars

Ok i'm really confused with arrays and pointers here.

I'm trying to move a simple block of code to C, but unfortunately I'm failing misserably..

char tictactoe[3][6];

tictactoe[0]="-|-|-";
tictactoe[1]="-|-|-";
tictactoe[2]="-|-|-";

I get this compiler error:

incompatible types when assigning to type 'char[6]' from type 'char *'

Could someone assist me with the correct syntax here please?

Upvotes: 0

Views: 146

Answers (7)

BLUEPIXY
BLUEPIXY

Reputation: 40155

    {//by array
        char tictactoe[3][6] ={
            "-|-|-",
            "-|-|-",
            "-|-|-"
        };
        printf("%s\n%s\n%s\n", tictactoe[0], tictactoe[1], tictactoe[2]);
    }
    printf("\n");
    {//by pointer (in C99)
        char *tictactoe[3];
        tictactoe[0] = (char[]){ "-|-|-" };
        tictactoe[1] = (char[]){ "-|-|-" };
        tictactoe[2] = (char[]){ "-|-|-" };
        tictactoe[0][0]='X';//rewritable
        printf("%s\n%s\n%s\n", tictactoe[0], tictactoe[1], tictactoe[2]);
    }

Upvotes: 2

haccks
haccks

Reputation: 106112

tictactoe[0], tictactoe[1], tictactoe[2] all are of type char[6] and decays to the pointer to first element of their corresponding rows. You can't assign string to it. If you wanna use pointers then declare an array of pointers

char *tictactoe[3]; 

then assign the string as

tictactoe[0]="-|-|-";
tictactoe[1]="-|-|-";
tictactoe[2]="-|-|-";  

Upvotes: 0

Elias Van Ootegem
Elias Van Ootegem

Reputation: 76408

TL-TR?

The short answer to your problem is this, either use:

char tictactoe[3][6] = {"-|-|-","-|-|-","-|-|-"};

Or use this, to assign on-the-fly

#include <string.h>

void t_t_toe( void )
{
    int i;
    char tictactoe[3][6];
    for (i=0;i<3;++i) strncpy(tictactoe[i], "-|-|-", 6);
    tictactoe[1][0] = '+';//works fine now
}

Here's why and when to use what:

You're confused with the subtle, yet fundamental, distinction between arrays and char pointers. It's quite easy, really, but it's a common pitfall:

char *string = "foobar";
char string2[] = "foobar";

Now, both of these statements seem to do the same thing, but where the first declares a pointer to a char/chars, the latter declares an array of chars. What's the difference? simple:

char *string = "foobar";

creates a constant array of chars, and stores that in a section of read-only stack memory. The location where "foobar" resides in memory (the memory address) is then assigned to *string.
That means that

printf("%c", string[1]);

Will indeed print out o, but

string[1] = 'Q';

will fail, as it attempts to reassign a value in read-only memory. No can do.
As opposed to the second example, where you will create the same constant char array, but it's copied to the array in read-write stack memory. They will behave in exactly the same way, right up to the point where you'll attempt to change the strings. as I explained, using a pointer to a constant string won't work, but because the second example copies the string:

string2[1] = 'Q';

will work perfectly.

Now, your case: depending on your needs, this will work just fine:

char *ticktacktoe[3] = {"-|-|-","-|-|-","-|-|-"};

This initializes an array of 3 char pointers, and assigns them the addresses of the 3 char constants "-|-|-". The array will look like:

{ 0xabc123f4, 0xabc123f8, 0xabc123fb} //or something similar

What you won't be able to do, however is reassign the strings in the array, so you effectively declare a const char *ticktacktoe[3].
If you want to be able to change the values of the strings writing:

char ticktacktoe[3][10] = {"tick", "tack", "toe"};

works just fine... to assign strings to a 2D array, it's probably best to use strncpy:

#include <string.h>

void t_t_toe( void )
{
    int i;
    char tictactoe[3][6];
    for (i=0;i<3;++i) strncpy(tictactoe[i], "-|-|-", 6);
    tictactoe[1][0] = '+';//works fine now
}

Upvotes: 2

Hariprasad
Hariprasad

Reputation: 3640

Try this:

char tictactoe[3][6] = {"-|-|-","-|-|-","-|-|-"};

Upvotes: 1

Nirav Kamani
Nirav Kamani

Reputation: 3272

You can use the following form:

char tic[3][6]={{'-','|','-','|','-'},{'-','|','-','|','-'},{'-','|','-','|','-'}};

First of all you are trying to to use CHAR type so assigning in the above form will make the correct sense.

What i think if you want to assign in tictactoe[0]="-|-|-"; form it treats it as pointer of CHAR.

If you want to use same form you can make use of pointer.

Upvotes: 0

V-X
V-X

Reputation: 3029

1) zero terminated strings are zero terminated. You have 6 characters + terminator = you need 7 characters per row.

2) you cannot assign string literal to an array. you have to use strcpy.

Upvotes: -1

herohuyongtao
herohuyongtao

Reputation: 50717

You want to do this:

char tictactoe[3][6] = {"-|-|-", "-|-|-", "-|-|-"};

Upvotes: 0

Related Questions