Need help calling function from header file

Tags: c
By : John
Source: Stackoverflow.com
Question!

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.

By : John


Answers

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.

By : Schwern


How about

random_string_from("123456789", 1) . random_string_from("1234567890", $n-1)
By : redneb


Disable attribute only associates button and input filelds, the thing you want to do here is , event.preventDefault . It will work like disable, but it will depend upon your coding, if you want to show it disabled, you have to add add via jquery



This video can help you solving your question :)
By: admin