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

typedef struct{

    int number;
    int value;
    int suit;

} CardT;

CardT initCard(CardT, int);
int compareCard(CardT, CardT);
void displayCard(CardT);

int main(void){
    CardT cardB;
    printf("hey man\n");
    initCard(cardB, 32);
    printf("%i\n", &cardB.number);
    printf("%i\n", &cardB.value);
    printf("%i\n", &cardB.suit);
    return(0);
}

CardT initCard(CardT Card, int x){
    Card.number = x;

    if (x%13 == 8){ Card.value = 'T';}
    else if (x%13 == 9){ Card.value = 'J';}
    else if (x%13 == 10){ Card.value = 'Q';}
    else if (x%13 == 11){ Card.value = "K";}
    else if (x%13 == 12){ Card.value = "A";}
    else {Card.value = x%13;}

    Card.suit = x/4;
    return Card;
}

So things compile, but the output is the memory location of the number. I can't figure out how to output the actual number without an error. Any clues?

3
  • 5
    Remove the & in the printf, you are passing a pointer to the value you just want the value. Commented Nov 26, 2013 at 18:51
  • You may also want to change the %i to %c for cardB.value, assuming you want to see K instead of 75. Commented Nov 26, 2013 at 18:53
  • 3
    Also the call to initCard needs to either take a pointer to a card struct to modify or use the returned card structure. Commented Nov 26, 2013 at 18:53

3 Answers 3

2

You seem to have your pointers confused. When you declare a CardT in main(), that's not a pointer... so you don't need to dereference anything in the printf()s (if that's what you're trying to do... if so it's not the correct way to do it anyway). Instead of:

printf("%i\n", &cardB.number);

you just need:

printf("%i\n", cardB.number);

...but that's not the only problem. If you do that (for all three), you'll print the values of the struct members instead of the addresses, but they'll be uninitialized because of the other problem -- your initCard() function is operating on a copy of the struct you pass it (passed by value) and isn't modifying the contents of the struct in main(). You need to pass it a pointer instead, and use the -> operator instead of . inside (to dereference and access the struct members via a pointer rather than directly). So:

CardT initCard(CardT Card, int x){

becomes:

CardT initCard(CardT *Card, int x){

...and the lines inside the function change from eg.:

    Card.number = x;

...to:

    Card->number = x;
Sign up to request clarification or add additional context in comments.

3 Comments

Referring the 2nd issue the OP alternativly could do cardB = initCard(cardB, 32);. Doing so the code does not need to be adjusted as by your proposal.
Okay, this makes sense. I am going to be making an array of these cards, to create a deck. I think i will need to do this in order to initialized each card of the array. Thanks so much, to everyone. I'll probably have a few more questions before the day is done, but this has helped!
@alk He could, yes... it's not necessarily a better way to do it though. But noticing that he returns the struct at the end of the function anyway, he may as well either follow your suggestion instead or not bother returning the struct.
1

The problem is the way you are using printf function. You pass as a parameter an adress, not the actual variable (& before a variable means its adress). This is needed for scanf, as it must put data in a memory location, but not for printf too. So, your statements should be:

   printf("%i\n", cardB.number);
   printf("%i\n", cardB.value);
   printf("%i\n", cardB.suit);

Comments

0

Unlike scanf() (which needs the address of each variable since it _assigns the value), printf() requires passing variables by value. You pass them by address which is the cause of this problem.

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.