2

I have a header file called "ChessMoves.h" and a c file call ChessMoves with a bunch of different functions.

Header file

#ifndef __CHESSMOVES_H
#define __CHESSMOVES_H

typedef struct Location
{
    // the square's column ('a' through 'h')
    char col;

    // the square's row (1 through 8)
    int row;
} Location;

typedef struct Move
{
    // location where this piece is moving from
    Location from_loc;

    // location where this piece is moving to
    Location to_loc;

    // what type of chess piece is being moved
    char piece;

    // whether this move captures another piece
    short int isCapture;

    // the color of the piece being moved
    Color color;
} Move;

File I'm calling to

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

void parseNotationString(char *str, Move *whiteMove, Move *blackMove){

    int i, space = 0, j = 0, k = 0, l = 0;
    int white[10], black[10], move[10], to[2];

    whiteMove.color = WHITE;
    if(white[0] > 64)
        whiteMove.piece = white[0];
    if(white[0] < 64)
        whiteMove.from_loc.row = white[0];
    for(i = 0; i < 10; i++)
        if(white[i] == 'x')
            whiteMove.isCapture = 1;
    for(i = 0; j < 10; i++)
        if(white[i] == ' ')
            to[0] = white[i-2];
            to[1] = white[i-1];

    printf("%c %c", to[0], to[0]);
}

We were given a file to test the code and in that file he has:

whiteMove.color != WHITE

and if whiteMove.color does not equal WHITE it will display "FAIL" so I tried setting

whiteMove.color = WHITE

but I keep getting "request for member 'color' in something not a structure or union. The same thing goes for the other structs I try to call. I tried making it,

Move.color = WHITE

and that doesn't work either.

6
  • Why are you testing uninitialized white[0] ? Commented Sep 25, 2016 at 15:41
  • None of the syntax is C, given the lack of semicolons. Commented Sep 25, 2016 at 15:42
  • whiteMove.color = WHITE; should be whiteMove->color; It's a pointer. Commented Sep 25, 2016 at 15:43
  • Since whiteMove and blackMove are both pointers, you need to either use the -> operator (whiteMove->color) or dereference the pointer before applying the . operator ((*blackMove).color). Commented Sep 25, 2016 at 15:44
  • You have a bug in that last if statement. And now you know why braces are good style. :) Commented Sep 25, 2016 at 17:36

1 Answer 1

2

So we can have something that compiles I've put this all into a file, chopped off the irrelevant bits, and added the missing Color enum.

$ cat test.c

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

typedef enum { WHITE, BLACK } Color;

typedef struct Location
{
    // the square's column ('a' through 'h')
    char col;

    // the square's row (1 through 8)
    int row;
} Location;

typedef struct Move
{
    // location where this piece is moving from
    Location from_loc;

    // location where this piece is moving to
    Location to_loc;

    // what type of chess piece is being moved
    char piece;

    // whether this move captures another piece
    short int isCapture;

    // the color of the piece being moved
    Color color;
} Move;


void parseNotationString(char *str, Move *whiteMove, Move *blackMove){

    int i, space = 0, j = 0, k = 0, l = 0;
    int white[10], black[10], move[10], to[2];

    whiteMove.color = WHITE;

    if(white[0]>64)
        whiteMove.piece = white[0];
    if(white[0]<64)
        whiteMove.from_loc.row = white[0];
    for(i=0;i<10;i++)
        if(white[i] == 'x')
            whiteMove.isCapture = 1;
    for(i=0;j<10;i++)
        if(white[i] == ' ')
            to[0] = white[i-2];
            to[1] = white[i-1];

    printf("%c %c", to[0], to[0]);
}

Compiling it with clang gives the answer immediately.

$ make
cc -Wall -g    test.c   -o test
test.c:40:14: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean
      to use '->'?
    whiteMove.color = WHITE;
    ~~~~~~~~~^
             ->
test.c:43:18: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean
      to use '->'?
        whiteMove.piece = white[0];
        ~~~~~~~~~^
                 ->
test.c:45:18: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean
      to use '->'?
        whiteMove.from_loc.row = white[0];
        ~~~~~~~~~^
                 ->
test.c:48:22: error: member reference type 'Move *' (aka 'struct Move *') is a pointer; did you mean
      to use '->'?
            whiteMove.isCapture = 1;
            ~~~~~~~~~^
                     ->
4 errors generated.
make: *** [test] Error 1

whiteMove is a Move *, a pointer to a Move structure. As such it has to be dereferenced with ->. . is for direct access.

clang's error messages are excellent and even give you a suggestion how to fix it. I would strongly suggest you use it, or compiler with similarly good errors, while learning C.


Furthermore your code has a subtle bug.

    for(i=0;j<10;i++)
        if(white[i] == ' ')
            to[0] = white[i-2];
            to[1] = white[i-1];

This does not do what the indentation says it does. It's actually this.

    for(i=0;j<10;i++)
        if(white[i] == ' ')
            to[0] = white[i-2];

    to[1] = white[i-1];

And this is why we always use braces.

Sign up to request clarification or add additional context in comments.

Comments

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.