-2

I have received the following two errors as well as an additional one "'return': cannot convert from 'link *' to 'link'" for these lines of code. The other two errors are listed in the subject of the question. I am not sure how to fix the problem. Any suggestions are helpful.

I am trying to code the card game War, and this is what I have so far. I am testing the code at each interval and have ran into problems at the code lines listed below

link NEWnode(Card card, link next) {
    link x;
    x = malloc(sizeof *x); //allocate memory
    if (x == NULL) {
        printf("Out of memory. \n");
        exit(EXIT_FAILURE);
    }
    x->next = next;
    x->card = card;
    return x;
}

The entire code is as follows:

#include <stdio.h>
    
#define DECKSIZE 52

typedef int Card;

int rank(Card c) {
    return c % 13;
}

// allow for multi-deck war
int suit(Card c) {
    return (c % 52) / 13;
}

// representing the cards

void showcard(Card c) {
    switch (rank(c)) {
        case 0: printf("Deuce of "); break;
        case 1: printf("Three of "); break;
        case 2: printf("Four of "); break;
        case 3: printf("Five of "); break;
        case 4: printf("Six of "); break;
        case 5: printf("Seven of "); break;
        case 6: printf("Eight of "); break;
        case 7: printf("Nine of "); break;
        case 8: printf("Ten of "); break;
        case 9: printf("Jack of "); break;
        case 10: printf("Queen of "); break;
        case 11: printf("King of "); break;
        case 12: printf("Ace of "); break;
    }

    switch (suit(c)) {
    case 0: printf("Clubs\n"); break;
    case 1: printf("Diamonds\n"); break;
    case 2: printf("Hearts\n"); break;
    case 3: printf("Spades\n"); break;
    }
}
// testing the code


// representing the deck and hands (with linked lists because need direct access to top and bottom cards, draw cards from top, won cards go to bottom)

typedef struct node* link;
struct node {
    Card card;
    link next;
};

link Atop, Abot;
link Btop, Bbot;

// showing a hand

void showpile(link pile) {
    link x;
    for (x = pile; x != NULL; x = x->next)
        showcard(x->card);
}

int countpile(link pile) {
    link x;
    int cnt = 0;
    for (x = pile; x != NULL; x = x->next)
        cnt++;
    return cnt;
}

// Creating the 52 card Deck

#include <stdlib.h> //for malloc()

link NEWnode(Card card, link next) {
    link x;
    x = malloc(sizeof *x); //allocate memory
    if (x == NULL) {
        printf("Out of memory. \n");
        exit(EXIT_FAILURE);
    }
    x->next = next;
    x->card = card;
    return x;
}

link makepile(int N) {
    link x = NULL;
    Card c;

    for (c = N - 1; c >= 0; c--)
        x = NEWnode(c, x);

    return x;
}

// testing the code

int main(void) {
    link deck;
    deck = makepile(DECKSIZE);
    showpile(deck);
    return 0;
}
  • 4
    Don't `typedef` pointers. It confuses everyone, including yourself. – dbush Oct 29 '21 at 18:24
  • 1
    What is `link`? – SergeyA Oct 29 '21 at 18:27
  • OT: Instead of `printf("Out of memory. \n"); ` consider `perror("malloc");`, it will give you more information. – David Ranieri Oct 29 '21 at 18:28
  • Edit the question to provide a [mre]. – Eric Postpischil Oct 29 '21 at 18:35
  • 1
    I question whether the code shown is the same code that produced the reported error messages. Both the function return type and `x` are declared as `link`, so `return x;` would not produce “'return': cannot convert from 'link *' to 'link'”. – Eric Postpischil Oct 29 '21 at 18:38
  • `perror` will also print to the correct file handle (stderr, not stdout) – ikegami Oct 29 '21 at 18:49
  • It is possible that the error is somewhere else in the code, but visual studios states that the error comes from the '=' in the 'x = malloc()' sections. – Derek Osik Oct 29 '21 at 18:50
  • The code now in the question compiles without error or warning (in Apple Clang 11.0 with `-Wmost`). – Eric Postpischil Oct 29 '21 at 18:56
  • Looks to me you are compiling it as C++ instead of C... Indeed, C++ requires to add a cast in front of `malloc`, like `(link) malloc(sizeof *x)` -- but then you should avoid using `malloc` in C++. Make sure to use a C compiler for this C code. See [Why does C++ require a cast for malloc() but C doesn't?](https://stackoverflow.com/questions/3477741/why-does-c-require-a-cast-for-malloc-but-c-doesnt) – trincot Oct 29 '21 at 19:17
  • Issue resolved by using a C compiler, thank you @trincot. I thought visual studios C++ would also compile C code. – Derek Osik Oct 29 '21 at 20:16

1 Answers1

0

This happens because you make use of a C++ compiler instead of a C compiler. In C++ a cast is necessary on that line:

x = (link) malloc(sizeof *x)

...while in C that cast is not needed -- the cast happens implicitly -- and it is actually considered best practice to not add such a cast in C.

On the other hand, in C++ you would better avoid malloc in favor of new. But since your code is intended to be C and not C++, make sure you choose the C compiler.

trincot
  • 317,000
  • 35
  • 244
  • 286