struct element {
int val;
int z;
};
typedef struct element ELEM;
Look at this example:
int main()
{
ELEM z;
z =
The pointer is only the address of the object in C/C++, not the object itself. In a 32-bit system, its length is always 4 bytes. When you create a pointer, it will refer to a invalid address if you don't initialize it or allocate memory for it. So you must dynamically create the object by calling malloc
(in C++, you can use new
keyword), then it can refer to the address of the created object.
ELEM elem; //This will create the object at stack.
ELEM* pElem; //This just create an invalid poiter which point to unknown address
pElem = &elem; //This initialize the pointer which point to the address if "elem" above
pElem = (ELEM*)malloc(sizeof(ELEM)); //This create a new memory which contain the object "ELEM" and pElem will point to the address of the object