Thread: Why when I tried to store values in allocated memory I keep getting an error?

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    12

    Post Why when I tried to store values in allocated memory I keep getting an error?

    I am writing a program that prints out the value of cards using a structure.

    I'm a beginner in data structure and I'm trying to advance my knowledge in this area.

    Values of Cards
    Face [1 - Ace, 2 - 2, ......, 11 - Jack, 12 - Queen, 13 - King]
    Shape [0 - Spades, 1 - Clubs, 2 - Diamonds, 3 - Hearts]
    Color [0 - Black, 1 - Red]

    I've used the malloc function to allocate memory for 3 cards, which was passed to a pointer name *myCards, with type struct cards.

    Code:
    struct Cards *myCards;
        myCards = (struct Cards *)malloc(3 * sizeof(struct Cards));
    I am trying to store 3 cards (Queen Spades, 4 Diamonds, Ace Clubs) in the space of the allocated memory, using the pointer myCards but I'm getting an error when I ran the code.

    error(incompatible types when assigning to type 'struct Cards' from type 'int')

    My structure

    Code:
    struct Cards {
        int face[13];
        int shape[3];
        int color[1];
    }Cards;
    Code for storing the 3 cards
    Code:
        *(myCards + 0) = 12, 1;
        *(myCards + 1) = 4, 2;
        *(myCards + 2) = 1, 1;






    Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,660
    > int face[13];
    > int shape[3];
    > int color[1];
    A single card has only a single face value, single shape and single colour.
    Also, shape implies colour - diamonds are always red for example.

    So
    Code:
    // Note the use of the singular, not plural
    struct Card {
      int face;
      int shape;
    };
    You then go onto create 3 cards.
    Code:
    struct Card *myCards;
    myCards = malloc(3 * sizeof(struct Cards));
    myCards[0].face = 10;
    myCards[0].shape = 1;
    myCards[0].color = 1;
    // etc
    > Enable GingerCannot connect to Ginger Check your internet connection
    Please turn off whatever it is that's cluttering all your posts.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Mar 2019
    Posts
    12
    Thanks man, I really appreciated your help!!
    The problem has been fixed.Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,323
    Quote Originally Posted by JonathanSimo View Post
    Thanks man, I really appreciated your help!!
    The problem has been fixed.Enable GingerCannot connect to Ginger Check your internet connection
    or reload the browserDisable in this text fieldEditEdit in GingerEdit in Ginger×
    Glad to hear that the C problem has been fixed, although it looks like your Ginger problem is still there
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 12-28-2012, 04:07 PM
  2. Replies: 10
    Last Post: 07-10-2012, 04:28 AM
  3. Allocate memory inside allocated memory block?
    By Heidi_Nayak in forum C Programming
    Replies: 14
    Last Post: 04-15-2009, 04:19 PM
  4. Error when freeing memory (allocated with realloc)
    By svdrjeug in forum C Programming
    Replies: 18
    Last Post: 01-03-2008, 11:16 AM
  5. Really strange, unexpected values from allocated variables
    By Jaken Veina in forum Windows Programming
    Replies: 6
    Last Post: 04-16-2005, 05:40 PM

Tags for this Thread