1

I'm trying to change the fields inside of a struct, I'm using the -> to do so, but my compiler gives me an error that suggests for me to use the ->. I'm not exactly sure why. Have I instantiated an instance of my struct incorrectly perhaps?

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

typedef enum suit_s {DIAMONDS, CLUBS, HEARTS, SPADES} Suit;
enum suit_s nCard;

int rankR;
int suit1;

typedef struct card_s {
  int rank;
  Suit suit;
} *Card;

int randCard(Card* rCard);
int getSuitName(Card* rCard);

int main(int argc, char *argv[]){
  Card *rCard;
  srand( (unsigned)time( NULL ) );
  randCard(rCard);
  getSuitName(rCard) ;
  return 0;
}


int randCard(Card* rCard) {
  int max_number = 10;
  int minimum_number = 2;
  rankR =  rand() % (max_number + 1 - minimum_number) + minimum_number;
  suit1 =  rand() % (4 + 1 - 1) + 1;
  if (suit1 == 1){
    nCard = DIAMONDS;
  }else if (suit1 == 2){
    nCard = CLUBS;
  }else if (suit1 == 3){
    nCard = HEARTS;
  }else if (suit1 == 4){
    nCard == SPADES;
  }
  rCard->rank = rankR; // error here
  rCard->suit = nCard; // error here

}

Compiler log:

main.c: In function 'randCard':
main.c:42:8: error: '*rCard' is a pointer; did you mean to use '->'?
   rCard->rank = rankR;
        ^~
        ->
main.c:43:8: error: '*rCard' is a pointer; did you mean to use '->'?
   rCard->suit = nCard;
        ^~
        ->
2
  • "Have I instantiated an instance of my struct incorrectly perhaps?" Well, you haven't initialized anything really. Also, your typedef includes the pointer so rCard which is a Card * is a pointer to a pointer to a struct. That's why your compiler is complaining, -> dereferences it one level — you've still got a pointer in hands. Commented Sep 15, 2018 at 2:08
  • Thank you, I appreciate your clarification. Commented Sep 15, 2018 at 2:12

2 Answers 2

1

You are getting these errors because rCard is of type Card * and Card is typedef to pointer to struct card_s:

typedef struct card_s {
  int rank;
  Suit suit;
} *Card;
  ^^

In the randCard (), the type of parameter rCard is Card * which makes rCard pointer to pointer to struct card_s.

To fix this problem, either declare rCard parameter of type Card or modify the typedef like this:

typedef struct card_s {
  int rank;
  Suit suit;
} Card;

Card is now alias to struct card_s.

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

1 Comment

Ohhh, thank you. Pointers are tricky, but this really helps.
1

Since you now should have the allocated object of Card in the main block you will need to pass the address of rCard to the function so need to update your call to randCard and getSuitName as well.

Your original reference:

randCard(rCard);

Becomes:

randCard(&rCard);

Minor point, but something to keep in mind.

1 Comment

Thank you, I actually was just troubleshooting that. You are a lifesaver!

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.