please help me initialize structures!!

Discussion in 'C' started by sam, Jun 4, 2011.

  1. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    #include<iostream.h>
    struct cards
    {
    char loc;
    int pay;
    };
    void main()
    {
    cards prop;
    prop.loc="bumper cars";
    cout<<prop.loc;
    }

    i get 3 syntax errors.helpppppp!!!!!!!got a proj coming up!thanks in advance.
     
  2. NewsBot

    NewsBot New Member

    Joined:
    Dec 2, 2008
    Messages:
    1,267
    Likes Received:
    2
    Trophy Points:
    0
    char loc is not same as string loc.
     
  3. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    sir can you please kindly explain what i have done wrong here?i have tried but it just wont get initialized like that.i am new to c++ as you might have guessed.please,i shall be grateful for a quick reply.....
     
  4. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    You should declare as
    struct cards
    {
    char* loc;
    int pay;
    };

    'char' is a single byte - in fact it is just an integer with range -128 to +127.

    'char *' is 4 bytes (in Win 32) and is capable of holding the address of a string.

    when you assign :
    prop.loc="bumper cars";

    - the compiler takes care of creating the string "bumper cars" in memory and assigning the start of the string ie location (in RAM) of the first character 'b' to prop.loc
     
  5. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    thanks so much for the explanation eriyer!!
    are all pointers 4 bytes or just character pointers??
     
  6. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Pointers need not necessarily be 4 bytes - since pointers hold addresses they should be large enough to address the maximum memory that the OS supports. With 4 bytes i.e. 32 bits the pointer can reference memory address in the range 0 to (2 ^ 32 - 1) i.e. 2 ^ 32 different values. In other words you can address 4GB of RAM which is the maximum memory in WIN 32.

    I have never worked on 64bit systems and it is possible that the size of a pointer (I dont know for certain) is larger in 64 bit systems.

    You can have different types of pointers -
    char * , int *, long * , double *
    and even
    cards * - pointer to the struct you declared in your example.

    You can have pointers to functions (one of the most powerful features of C) and these (pointers to functions) are the key to callbacks (event responders), multi threading and so much more. By the time you reach this stage you will have covered almost the rest of C.

    Since you are new to the subject make yourself comfortable with simpler pointers like 'char *', 'int *' - you will surely cover pointers to structures (like cards*) when you come across linked lists. Take it all, one at a time!
     
  7. eriyer

    eriyer New Member

    Joined:
    Jan 22, 2011
    Messages:
    32
    Likes Received:
    0
    Trophy Points:
    0
    Sorry, I did not understand your query correctly!

    Yes, since pointers hold numbers which are memory addresses the size of a pointer is the same within the system whether the pointer is a char pointer , integer pointer, struct pointer, function pointer ...
     
  8. sam

    sam New Member

    Joined:
    Jun 4, 2011
    Messages:
    12
    Likes Received:
    0
    Trophy Points:
    0
    sir thankyou so much for the explanation!now that was all that was left for me to complete my project.i could'nt initialize structures correctly.i have made monopoly on c++ :) ...(without graphics).
    thanks again!:)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice