The correct code is:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct student
{
char name[20];
int rollno;
}s1[3];
void accept(struct student ptr[], int n)
{
int i;
for(i=0;i<n;i++)>
{
printf("Enter student name:");
scanf("%s",ptr[i].name);
printf("Enter student roll no.");
scanf("%d",&(ptr[i].rollno));
}
}
void print(struct student ptr[], int n)
{
printf("Students details are:\n");
int i;
for(i=0;i<n;i++)>
{
printf("Student name is \"%s\" - roll no.=%d\n", ptr[i].name, ptr[i].rollno);
}
}
int main(int argc, char *argv[])
{
accept(s1,3);
print(s1,3);
return 0;
}
Please note that
in C an array name is a pointer to the first element of the array.
So in you case, because you're using an array of structures, you have to pass the name of the array.
scanf() want pointers to arguments, once again when reading the student name you are using an array (an array of chars). So you don't need to get the address because the name of the array
ptr[i].name, is an array and is already a pointer. But when you get the roll number you must use the '&' operator to get the address of the integer in
ptr[i].rollno that is not an array.
Same procedure for the print() function.