# Pointer to a structure example using C

P

#### Generate basic student results from students marks entry using structure and pointer in C.

If you are a computer science student or wants to start learning Data structure using practical hands-on then you are landing in the right place. This practice is part of the Data structure subject taught in GTU (Gujarat Technological University) Diploma course, but it is not limited to this. You can use this tutorial to have a better understanding of detail explanations. if you like to get more out of this practice then I highly recommend you to do code follow along is a best possible way. To know more about Author please visit here

Let’s take a brief about what is going to solve/code and design out solution step by step

Unit 1
Practical 1: Develop a simple program using a pointer to a structure

Problem statement:
User will input students marks entry along with name and roll number while C program will calculate total marks for each student and ranked them in ascending order.

Solution :
step 1: Define Structure / logical collection to hold records about each student.
step 2: Take marks entry, name and roll number from the user.
step 3: Calculate Total Marks for each student.
step 4: Sort students records by total marks obtained (descending order)
or ranked students.

Step1: Define Structure / logical collection to hold records about each student.

As we know, the student is a logical entity and details like Name, Roll number, marks etc represent data about students. what we can do is collect them together into a logical collection or a group called structure and use it as one entity wherever require.

So, For simplicity, we choose Name, Roll number and marks to represent student record which can be written as structure as follow

```/*
* @Author: abdulkaiyum
* @Date:   2019-07-08 10:36:53
*/

#include <stdio.h>
#include<stdlib.h>
#include <string.h>

typedef struct Result
{
int  RollNO;
char Name;
int  marks;
int  total;
} RESULT;

int main(int argc, char const *argv[])
{
return 0;
}```

Step 2: Take marks entry, name and roll number from the user.

To perform this step we will take reference our result pointer into the main method/function and using pointer we will insert data into it. let’s have a look code.

```/*
* @Author: abdulkaiyum
* @Date:   2019-07-08 10:36:53
*/

#include <stdio.h>
#include<stdlib.h>
#include <string.h>

int main(int argc, char const *argv[])
{
struct Result *result;
int resultCount,i;

//  Get result count / number of students
printf("Enter Number of results\n");
scanf("%d",&resultCount);

// Allocate memory for Number of students records, each record has type Result (structure)
result = (RESULT*) malloc(resultCount * sizeof(RESULT));

// Loop through Empty N student records and filling up data
printf("Enter students records [RollNO \t name \t sub1 \t sub2 \t sub3 ]\n");
for (i = 0; i < resultCount; ++i,result++)
{
printf("%s\n", "Enter Student Name(MAX 20 char)");
scanf(" %s",result->Name);
printf("%s\n", "Enter Roll Number");
scanf("%d",&result->RollNO);
printf("%s\n", "Enter Marks [sub1 sub2 sub 3] followed by Enter");
scanf("%d %d %d",&result->marks,&result->marks,&result->marks);

printf("%s\n", "Result Entered Successfully");
printf("*************************************\n");
}
return 0;
}
```

Step 3: Calculate Total Marks for each student.

Now we have all records, all is a need to do to calculate the total marks then use for sorting the records.

```#include <stdio.h>
#include<stdlib.h>
#include <string.h>

typedef struct Result
{
int RollNO;
char Name;
int marks;
int total;
} RESULT;

int main(int argc, char const *argv[])
{
struct Result *result,*temp;
int resultCount,i,j;

//  Get result count / number of students
printf("Enter Number of results\n");
scanf("%d",&resultCount);

// Allocate memory for Number of students records, each record has type Result (structure)
result = (RESULT*) malloc(resultCount * sizeof(RESULT));

// Loop through Empty N student records and filling up data
printf("Enter students records [RollNO \t name \t sub1 \t sub2 \t sub3 ]\n");
for (i = 0; i < resultCount; ++i,result++)
{
printf("%s\n", "Enter Student Name(MAX 20 char)");
scanf(" %s",result->Name);
printf("%s\n", "Enter Roll Number");
scanf("%d",&result->RollNO);
printf("%s\n", "Enter Marks [sub1 sub2 sub 3] followed by Enter");
scanf("%d %d %d",&result->marks,&result->marks,&result->marks);

printf("%s\n", "Result Entered Successfully");
printf("*************************************\n");
}

result = result - resultCount;

// calculating total marks
for (i = 0; i < resultCount; ++i,result++)
{
result->total = result->marks + result->marks+ result->marks;
}
return 0;
}```

here you can see we have used `result = result - resultCount` which were used to relocate our result pointer to the first record.

Step 4: Sort students records by total marks obtained (descending order)or ranked students.

```/*
* @Author: abdulkaiyum
* @Date:   2019-07-08 10:36:53
*/

#include <stdio.h>
#include<stdlib.h>
#include <string.h>

typedef struct Result
{
int RollNO;
char Name;
int marks;
int total;
} RESULT;

int main(int argc, char const *argv[])
{
struct Result *result,*temp;
int resultCount,i,j;

//  Get result count / number of students
printf("Enter Number of results\n");
scanf("%d",&resultCount);

// Allocate memory for Number of students records, each record has type Result (structure)
result = (RESULT*) malloc(resultCount * sizeof(RESULT));

// Loop through Empty N student records and filling up data
printf("Enter students records [RollNO \t name \t sub1 \t sub2 \t sub3 ]\n");
for (i = 0; i < resultCount; ++i,result++)
{
printf("%s\n", "Enter Student Name(MAX 20 char)");
scanf(" %s",result->Name);
printf("%s\n", "Enter Roll Number");
scanf("%d",&result->RollNO);
printf("%s\n", "Enter Marks [sub1 sub2 sub 3] followed by Enter");
scanf("%d %d %d",&result->marks,&result->marks,&result->marks);

printf("%s\n", "Result Entered Successfully");
printf("*************************************\n");
}

result = result - resultCount;

// calculating total marks
for (i = 0; i < resultCount; ++i,result++)
{
result->total = result->marks + result->marks+ result->marks;
}

result = result - resultCount;

// Shorting result based on total marks (ascending)

// Allocating space for temp
temp = (RESULT*)malloc(sizeof(RESULT));

for (i = 0; i < resultCount - 1; ++i)
{
for (j = i+1; j < resultCount; ++j)
{
if((result+i)->total < (result+j)->total)
{
// swap records/place
*temp       = *(result+i);
*(result+i) = *(result+j);
*(result+j) = *temp;
}
}
}
return 0;
}```

Now let’s just print the sorted result

```/*
* @Author: abdulkaiyum
* @Date:   2019-07-08 10:36:53
*/

#include <stdio.h>
#include<stdlib.h>
#include <string.h>

typedef struct Result
{
int RollNO;
char Name;
int marks;
int total;
} RESULT;

int main(int argc, char const *argv[])
{
struct Result *result,*temp;
int resultCount,i,j;

//  Get result count / number of students
printf("Enter Number of results\n");
scanf("%d",&resultCount);

// Allocate memory for Number of students records, each record has type Result (structure)
result = (RESULT*) malloc(resultCount * sizeof(RESULT));

// Loop through Empty N student records and filling up data
printf("Enter students records [RollNO \t name \t sub1 \t sub2 \t sub3 ]\n");
for (i = 0; i < resultCount; ++i,result++)
{
printf("%s\n", "Enter Student Name(MAX 20 char)");
scanf(" %s",result->Name);
printf("%s\n", "Enter Roll Number");
scanf("%d",&result->RollNO);
printf("%s\n", "Enter Marks [sub1 sub2 sub 3] followed by Enter");
scanf("%d %d %d",&result->marks,&result->marks,&result->marks);

printf("%s\n", "Result Entered Successfully");
printf("*************************************\n");
}

result = result - resultCount;

// calculating total marks
for (i = 0; i < resultCount; ++i,result++)
{
result->total = result->marks + result->marks+ result->marks;
}

result = result - resultCount;

// Shorting result based on total marks (ascending)

// Allocatting space for temp
temp = (RESULT*)malloc(sizeof(RESULT));

for (i = 0; i < resultCount - 1; ++i)
{
for (j = i+1; j < resultCount; ++j)
{
if((result+i)->total < (result+j)->total)
{
// swap records/place
*temp       = *(result+i);
*(result+i) = *(result+j);
*(result+j) = *temp;
}
}
}

// printing result
printf("---------------------------------------\n");
printf("[RollNO\tname\tsub1\tsub2\tsub3\t total]\n");

//result = result - resultCount;
for (i = 0; i < resultCount; ++i,result++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", result->RollNO, result->Name, result->marks,result->marks,
result->marks,result->total);
}
return 0;
}```

Here is a complete program

```/*
* @Author: abdulkaiyum
* @Date:   2019-07-08 10:36:53
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct Result
{
int  RollNO;
char Name;
int  marks;
int  total;
} RESULT;

int main(int argc, char const *argv[])
{
struct Result *result,*temp;
int resultCount,i,j;

printf("Enter Number of results\n");

scanf("%d",&resultCount);

result = (RESULT*) malloc(resultCount * sizeof(RESULT));

printf("Enter students records [RollNO \t name \t sub1 \t sub2 \t sub3 ]\n");
for (i = 0; i < resultCount; ++i,result++)
{
printf("%s\n", "Enter Student Name(MAX 20 char)");
scanf(" %s",result->Name);
printf("%s\n", "Enter Roll Number");
scanf("%d",&result->RollNO);
printf("%s\n", "Enter Marks [sub1 sub2 sub 3] followed by Enter");
scanf("%d %d %d",&result->marks,&result->marks,&result->marks);

printf("%s\n", "Result Entered Successfully");
printf("*************************************\n");
}

result = result - resultCount;

// calculating total marks
for (i = 0; i < resultCount; ++i,result++)
{
result->total = result->marks + result->marks+ result->marks;
}

result = result - resultCount;

// Shorting result based on total marks (ascending)

// Allocatting space for temp
temp = (RESULT*)malloc(sizeof(RESULT));

for (i = 0; i < resultCount - 1; ++i)
{
for (j = i+1; j < resultCount; ++j)
{
if((result+i)->total < (result+j)->total)
{
// swap records/place
*temp       = *(result+i);
*(result+i) = *(result+j);
*(result+j) = *temp;

// printf("%d\n", temp->total);
}
}
}

// printing result
printf("---------------------------------------\n");
printf("[RollNO\tname\tsub1\tsub2\tsub3\t total]\n");

//result = result - resultCount;
for (i = 0; i < resultCount; ++i,result++)
{
printf("%d \t %s \t %d \t %d \t %d \t %d \n", result->RollNO, result->Name, result->marks,result->marks,
result->marks,result->total);
}
return 0;
``` 