Pointer To Structure
- As Pointer are the variable which stores the address of other variable.
- So , Now we will learn how store the address of structure variable.
- As you know pointer can point to a variable,only if their data type are same.
- For example:int type of pointer can only point to int type of variable or array,etc
- Because of that ,for pointing a structure you will need to make a pointer of structure datatype. So now we will learn how to do all that things
- So , let start doing it.
- for learning that we will define a structure student as name , rollno ,div as it's member.
Definition:
struct student //structure name
{
int rollno; //member1 ,integer variable which will store,
//student roll no.
char name[20]; //member2 a character array of 20 character
//which will store name of student.
char div; //member3 a character variable,
//which will store division of student.
};
struct student //structure name { int rollno; //member1 ,integer variable which will store, //student roll no. char name[20]; //member2 a character array of 20 character //which will store name of student. char div; //member3 a character variable, //which will store division of student. };
As we have define a structure now we will declare a pointer of structure student data type.
Syntax:declaring pointer of structure student data type
struct structure_name *pointer_variable_name;
struct structure_name *pointer_variable_name;
Declaring:pointer of structure student datatype.
struct student *s;
struct student *s;
Let's see memory view of structure pointer,by looking it you will get an great understanding of pointer of structure variable
Now we will access the member of pointer of structure variable
Syntax:Accessing the member
pointer_variable_name -> member_name;
- We use arrow operator for accessing the member of structure, when structure is point by a pointer.
- -> Arrow operator.
pointer_variable_name -> member_name;
Accessing the members:
//accessing rollno
s -> rollno;
//Accessing Name
s -> name;
//Accessing div
s -> div
//accessing rollno s -> rollno; //Accessing Name s -> name; //Accessing div s -> div
Now we will take members as input from the user.
void main()
{
int i;
//declaration of pointer variable of
//structure student data type.
struct student *s,s1;
s = &s1;
//Assigning address of struct
//variable s1 to pointer variable s
//Taking student roll no as input
printf("\nEnter the roll no of student:");
scanf("%d",&s->rollno);
//Taking student name as input
printf("\nEnter the name of student:");
scanf("%s",s->name);
//Taking student Division as input
printf("\nEnter the student div:");
(s->div) = getche();
//Printing the values which is taking from the user.
printf("student roll no and name is %d and %s and studies in %c Division",s->rollno,s->name,s->div);
}
void main() { int i; //declaration of pointer variable of //structure student data type. struct student *s,s1; s = &s1; //Assigning address of struct //variable s1 to pointer variable s //Taking student roll no as input printf("\nEnter the roll no of student:"); scanf("%d",&s->rollno); //Taking student name as input printf("\nEnter the name of student:"); scanf("%s",s->name); //Taking student Division as input printf("\nEnter the student div:"); (s->div) = getche(); //Printing the values which is taking from the user. printf("student roll no and name is %d and %s and studies in %c Division",s->rollno,s->name,s->div); }
Output:
Enter the roll no of student: 1
Enter the name of student: Roy
Enter the student div:A
student roll no and name is 1 and Roy and studies in A Division.
Enter the name of student: Roy
Enter the student div:A
student roll no and name is 1 and Roy and studies in A Division.
Dynamic memory allocation
- Sometimes, the number of structure variables you declared may be insufficient. You may need to allocate memory during run-time. Here's how you can achieve this in C programming
In this program we will use malloc() function
Syntax:
pointer_name = (structure _name *) malloc(sizeof(structure_name));
pointer_name = (structure _name *) malloc(sizeof(structure_name));
Allocating memory dynamically:to pointer of student structure datatype
void main()
{
//declaration of pointer variable of
//structure student data type.
struct student *s;
s = (struct student*)malloc(sizeof(struct student)*1);
//allocating memory dynamically using malloc()
}
void main() { //declaration of pointer variable of //structure student data type. struct student *s; s = (struct student*)malloc(sizeof(struct student)*1); //allocating memory dynamically using malloc() }
Let's take input from the user
printf("\nEnter the roll no of student:");
scanf("%d",&s->rollno);
//Taking student name as input
printf("\nEnter the name of student:");
scanf("%s",s->name);
//Taking student Division as input
printf("\nEnter the student div:");
(s->div)=getchar();
printf("\nEnter the roll no of student:"); scanf("%d",&s->rollno); //Taking student name as input printf("\nEnter the name of student:"); scanf("%s",s->name); //Taking student Division as input printf("\nEnter the student div:"); (s->div)=getchar();
Let's print the data on the screen
//printing rollno
printf("\n%d",s->rollno);
//printing name
printf("\n%s\N",s->name);
//printing div
printf("\n%c",s->div);
//printing rollno printf("\n%d",s->rollno); //printing name printf("\n%s\N",s->name); //printing div printf("\n%c",s->div);
- This how we can point to a structure variable dynamically.
- So ,do more practice on pointers and structures for understanding the concept more better.
Further Topics:
- How to initialize an structure variable?
- How to Access Structure member?
- What are Nested Structure?
- How to built an Nested Structure?
- How to Access members of Nested Structure?
- How to initialize Nested structure?
- Functions and Structure
- How to pass a structure variable to function?
- How to make function which will take structure variable as arguments?
- How to make function which will return structure variable?
- Array and Structure
- How to make array of structure variable?
- How to initialize array structure variable?
- How to Access members of an array of structure?
- How to take members of array structure as input from the user?
- Pointer and Structure
- How to Point a structure variable?
- How to declare a pointer of structure variable?
- How to Initialize an pointer of structure variable?
- How to access member of structure variable which is point by an pointer?
- What is '->' Operator?
- How to allocate memory for structure variable Dynamically
- How to allocate memory of structure variable using malloc() and calloc() function
- Self Referential Block
- What is Referential Structure?
- How to Create self Referential Structure?
- Applications of Self Referential Structures
- Practice Programs
People Also Searched:
- Array and Pointer
- What is array of pointer?
- how to initialize an array of pointers?
- How to Access elements of an Array Of Pointers?
- Functions and Pointers
- How to pass a Pointer To a Function?
- Call By reference
- Functions In C
- Why Functions?
- What are Functions?
- how to write our functions?
- What are programmer define functions?
- How to declare function?
- How to define a function?
- What is function Prototype?
- how to declare a function?
- what are Actual Parameters?
- What are Formal Parameters?
- Function Call?
- how to call a Function?
- What are different types of calling function?
- Call by value
- call by reference
- Recursion
- Array and Functions
- How to pass an array to a function?
- What is Character array
- Functions for Characters
- Functions from #include<ctype.h> Header File
- what are isdigit(), islower() , isupper(),etc
- what are various string handling functions
- Functions from #include<string.h> Header File
- what are strcpy(), strcmp(), strncmp(), strcmpi(),etc
- strcmp() VS strncmp() VS strcmpi()
- Input And Output Functions
- How to take strings As Input
- What is gets() functions
- What is puts() function
0 Comments
Post a Comment