Using function with Struct user defined type also pointer

code:

#include<stdio.h>
typedef struct
{
    int roll;
    char name[20];
    double gp;
} sTudent;

void inputStudent(sTudent *sp)
{

    printf("enter roll: ");
    scanf("%d",&sp->roll);
    printf("enter name:");
    scanf(" %[^\n]",sp->name);
    printf("enter grade point: \n");
    scanf("%lf",&sp->gp);
}

void printStudent(sTudent sp)
{
    printf("Roll: %4d, Name: %-20s %10.2lf\n",sp.roll,sp.name,sp.gp);
}

int main()
{
    sTudent s;
    inputStudent(&s);
    printStudent(s);

    return 0;
}

why we used *sp in one place and sp in another place ?there s a reason behind this
*sp means address is passing where after the local input takeup it will go but if not use  dereferencing then it will not go outside. So to write we need with address.

and in sp it is read only. so to print sp is enough to show.

vid:

It would be a great help, if you support by sharing :)
Author: zakilive

Leave a Reply

Your email address will not be published. Required fields are marked *