1

So I'm writing a chess program from the ground up to work on my coding, and I've run into a problem I can't figure out. Here's the important bits for my question:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int printboard(char board[8][8])
{
    int i, x, y;
    char piece;
    char *clrptr, *nmptr;

    printf("\n");

    for (x=0; x<=40; x++)
    {
        if (x%5 == 0)
        {
            for (i=0; i<8; i++)
            {
                printf("|---------|");
            }
            printf("\n");
        }
        if (x%5 == 1 || x%5 == 4)
        {
            for (i=0; i<8; i++)
            {
                printf("|         |");
            }
            printf("\n");
        }
        if (x%5 == 2)
        {
            for (i=0; i<8; i++)
            {
                piece = board[x/5][i];
                int colorstr(char piece, char* clrptr);
                printf("|   %c%c%c   |", *clrptr, *clrptr+1, *clrptr+2);
            }
            printf("\n");
        }
    }
    return 0;
}

int colorstr(char piece, char* clrptr)
{
    char black[4] = "Blk", white[4] = "Wht", empty[4] = "000";

    if (isupper(piece) != 0)
        clrptr = black;
    if (piece == '0')
        clrptr = empty;
    else
        clrptr = white;
    return 0;
}

The idea is that piece is defined as an alphanumeric character from my board array, while the colorstr function determines what piece character makes what color, and then assigns clrptr to point to the first character in the relevant string array (black, white, or empty). Back in the main function, clrptr, clrptr+1, and clrptr+2 are printed, which in effect prints the string that colorstr determines. Or at least, that's my intent. I'm getting ▒▒▒ instead. The nmstr function does the same thing, so solving this problem should fix both.

(My compiler is cygwin-gcc.)

2
  • On which operating system, and with which locale and charset ? Also, compile with all warnings & debug info (e.g. gcc -Wall -g), and learn to use the debugger (e.g. gdb). Maybe ncurses could be useful. BTW, I would make the board a struct Commented Mar 23, 2014 at 18:29
  • 1
    in the function int colorstr you need to declare the strings as static otherwise, they will be just local variables on stack only as long as you are in that function call, can't just return them. Commented Mar 23, 2014 at 18:32

3 Answers 3

1

Your colorstr is a noop. Also, for a proper working function you should adjust parameters and return type. Try:

char* colorstr(char piece)
{
    return piece == '0' ? "000" : (isupper(piece) ? "Wht" : "Blk");
}

When you call it:

for (i=0; i<8; i++)
{
    piece = board[(unsigned char)x/5][i];
    clrptr = colorstr(piece); /* clrptr is returned
      (was a local declaration before) */
    printf("|   %s   |", clrptr);
}
printf("\n");
Sign up to request clarification or add additional context in comments.

3 Comments

Worked like a charm. Should've been using the ? operator from the get-go, and this set me on the right path to using functions properly. Derpderpderp. Thanks for the help! As a side note, when I compile this with all warnings, I get "warning: array subscript has type 'char'". Is this something I should be careful about, or just the compiler keeping me on my toes?
Very good: Always try to make your code compile cleanly with all warnings, but do not forcibly silence them. As to this warning: Negative array indices are bad (negative pointer offsets might be useful though), and char has implementation defined signedness (so, better cast to unsigned char before use as array index).
I think I understand, but I'm still not sure why it's popping up. The same warning is occurring in two other places (now that I'm populating the board), and each of the three warnings references a line with either the tolower or isupper fxns involved, so I expect they're the cause.
0

The colorstar function doesn't work at all Try this:

void colorstr(char piece, char** clrptr) /* get a pointer to the orignal pointer */
{
    static char black[4] = "Blk", white[4] = "Wht", empty[4] = "000";

    if (isupper(piece) != 0)
    {
        *clrptr = black;
    }
    if (piece == '0')
    {
        *clrptr = empty;
    }
    else
    {
        *clrptr = white;
    }
}

When you call it:

        for (i=0; i<8; i++)
        {
            piece = board[x/5][i];
            colorstr(piece, &clrptr); /* pass the address of clrptr, not just a copy of its value */
            printf("|   %s   |", clrptr);
        }
        printf("\n");

1 Comment

Thanks! All useful information... I'll keep this in mind the next time I try to do something similar. Deduplicator's answer seems simplest for what I need this function to do.
0

The snippet

            piece = board[x/5][i];
            int colorstr(char piece, char* clrptr);
            printf("|   %c%c%c   |", *clrptr, *clrptr+1, *clrptr+2);

is wrong. Right in the center, you are not calling colorstr() function, but instead declaring it!

Try this instead

            piece = board[x/5][i];
            colorstr(piece, clrptr);
            printf("|   %c%c%c   |", *clrptr, *clrptr+1, *clrptr+2);

This is just the start, there is a lot more things wrong in here. For example, you don't even allocate memory for clrptr!

1 Comment

Yeah, I'm just getting back into coding after a long hiatus... I had it as a call before, guess I changed it to a declaration when I was trying random things to fix it. Thanks for pointing that out!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.