#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.
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.....
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
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!
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 ...
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!