Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#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)
{
    // printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)
    {
        printf("Enter student name:\n");
        scanf("%s",&ptr[i]->name);
        printf("Enter student roll no.\n");
        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:\n",ptr[i]->name);
        printf("Student roll no. is %d:\n",ptr[i]->rollno);
    }
}
void main()
{
    accept(&s1,3);
    print(&s1,3);
}
Posted
Updated 2-Jul-15 21:33pm
v2

You do not need all those addressof (&) operators in front of your array names.
[edit]
Alse removed dereference asterisks on method declarations, and replaced -> by . for structure refs.
[/edit]
Try:
C++
#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) // removed *
{
    // printf("Students details are:\n");
    int i;
    for(i=0;i<n;i++)>
    {
        printf("Enter student name:\n");
        scanf("%s", ptr[i].name);  // no &, name is an array
        printf("Enter student roll no.\n");
        scanf("%d", &(ptr[i].rollno));  // need & here, rollno is simple variable
    }
}
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:\n", ptr[i].name);
        printf("Student roll no. is %d:\n", ptr[i].rollno);
    }
}
void main()
{
    accept(s1,3);
    print(s1,3);
}
 
Share this answer
 
v4
Comments
Member 11372005 3-Jul-15 4:48am    
Hi Richard,

Issue is not resolved after taking off & operator from array names. I'm getting below output when i try to compile.
====================================================================================
a1.c: In function ‘accept’:
a1.c:16:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf("Enter student name:\n");
^
a1.c:17:9: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
scanf("%s", ptr[i]->name); // no &, name is an array
^
a1.c: In function ‘print’:
a1.c:24:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf("Students details are:\n");
^
a1.c: In function ‘main’:
a1.c:34:5: warning: passing argument 1 of ‘accept’ from incompatible pointer type [enabled by default]
accept(s1,3);
^
a1.c:10:6: note: expected ‘struct student **’ but argument is of type ‘struct student *’
void accept(struct student *ptr[], int n)
^
a1.c:35:5: warning: passing argument 1 of ‘print’ from incompatible pointer type [enabled by default]
print(s1,3);
^
a1.c:22:6: note: expected ‘struct student **’ but argument is of type ‘struct student *’
void print(struct student *ptr[], int n)
^
=================================================================================
After running the exe i'm getting below output

Enter student name:
keshav
Enter student roll no.
Enter student name:
Enter student roll no.
Enter student name:
Enter student roll no.
Students details are:
Student name is (null):
Segmentation fault (core dumped)
Richard MacCutchan 3-Jul-15 5:27am    
Try the modified version. BTW all those warnings above, suggest that you have a #include statement missing.
The problem is the parameter declaration of ptr:
C++
void accept(struct student *ptr[], int n) // wrong

You are telling the compiler that you are passing an array of pointers to student structures. But in main you are not passing an array of pointers, but simply an array of student structures. So change you declaration to:
C++
void accept(struct student *ptr, int n)

Same for the print function.

And as Richard said in his post, you can do away with all those ampersands, because an array name is at the same time a pointer to the first element.
 
Share this answer
 
Comments
Member 11372005 3-Jul-15 4:54am    
Hi,

After taking off [] from ptr and removing & operator i'm getting below error:

a1.c: In function ‘accept’:
a1.c:16:9: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf("Enter student name:\n");
^
a1.c:17:9: warning: incompatible implicit declaration of built-in function ‘scanf’ [enabled by default]
scanf("%s", ptr[i]->name); // no &, name is an array
^
a1.c:17:27: error: invalid type argument of ‘->’ (have ‘struct student’)
scanf("%s", ptr[i]->name); // no &, name is an array
^
a1.c:19:29: error: invalid type argument of ‘->’ (have ‘struct student’)
scanf("%d", &(ptr[i]->rollno)); // need & here, rollno is simple variable
^
a1.c: In function ‘print’:
a1.c:24:5: warning: incompatible implicit declaration of built-in function ‘printf’ [enabled by default]
printf("Students details are:\n");
^
a1.c:28:47: error: invalid type argument of ‘->’ (have ‘struct student’)
printf("Student name is %s:\n", ptr[i]->name);
^
a1.c:29:51: error: invalid type argument of ‘->’ (have ‘struct student’)
printf("Student roll no. is %d:\n", ptr[i]->rollno);
^
Jochen Arndt 3-Jul-15 5:03am    
When changing the parameters as suggested you must also change the usage inside the functions by replacing "ptr[i]->name" with "ptr[i].name". Note that the '&' must be left in front for the scanf parameters.
Member 11372005 3-Jul-15 5:09am    
Thanks, i was able to compile the code successfully. But i have a question.

if i do this, can it be a proper example of passing array of structure as pass by reference?. Or could you please share a simple example of how to pass an array of structure as reference.
Jochen Arndt 3-Jul-15 5:19am    
If you do this, your program should work.
But you are not passing the array be reference. You are passing a pointer (and have passed in your original example).

It is uncommon to use references for code likes yours. To get a reference to your array you can use:
struct student (&array_ref)[3] = s1;

The size of the array must be specified so that such references can't be used with dynamic arrays. Therefore, pointers are better here.
Member 11372005 3-Jul-15 5:01am    
Above mentioned warnings have be resolved after including #include stdio.h. But still errors are there.
The correct code is:
C++
#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)
{
    // printf("Students details are:\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.
 
Share this answer
 
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900