pointer to a structure
From: Ravi kumar.N (ravikumar.n_at_sunlux-india.com)
Date: 11/21/04
- Previous message: Nick Landsberg: "Re: A critique of test-first..."
- Next in thread: Siddharth Taneja: "Re: pointer to a structure"
- Reply: Siddharth Taneja: "Re: pointer to a structure"
- Reply: Martijn: "Re: pointer to a structure"
- Reply: Joe Wright: "Re: pointer to a structure"
- Reply: Dave Neary: "Re: pointer to a structure"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 20 Nov 2004 22:21:57 -0800
Respected Experts,
Iam Ravikumar.N, working as a juniour software
Engineer.I have a query regarding pointer to a structure.
1) Memory will be allocated to a structure when a variable is
declared to it.
2) Consider the following example.
struct a{
int c;
int d;
}
struct a e; (When we declare 'e' as a structure variable,
memory will be allocated to structure. Here no need of malloc for
memory allocation)
3)When we use a pointer to a structure . eg. struct a *g;
The following things has to be done to access the structure
member.
3.1) Once we declare a pointer to the structure, memory
allocation has to be done using malloc.
e.g. g = (struct a*)malloc(sizeof(struct a));
3.2)If we try to access the structure member without the above
step done, during run-time an error saying "Memory could not be
referenced" will be displayed.
My query is as follows
4)Since memory to the structure will be allocated as soon as we
declare a structure variable, why malloc has to be done for pointer to
the structure and why not for the structure variable like "struct a
e";
- Previous message: Nick Landsberg: "Re: A critique of test-first..."
- Next in thread: Siddharth Taneja: "Re: pointer to a structure"
- Reply: Siddharth Taneja: "Re: pointer to a structure"
- Reply: Martijn: "Re: pointer to a structure"
- Reply: Joe Wright: "Re: pointer to a structure"
- Reply: Dave Neary: "Re: pointer to a structure"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|