Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My program aims to

Quote:
function to insert data in structure at certain index and other function to display that data




ERROR

Quote:
incompatible type conversion error c


C++
#include<stdio.h>
struct student{
  char name[50];
  int id;
}; 

struct student printstudent()
 {
   int index=0;
   struct student e;
   clrscr();
	printf("plz insert index \n");
	scanf("%d",&index);
         if(index >2) printf("index out of range ");return 0;
	printf("plz insert name\n");
	  scanf("%s",e[index].name);
	  printf("plz insert age\n");
	  scanf("%d",&e[index].id);
   return e; //error at this line

 }
  main()
   {

	  printstudent() ;
	  clrscr();
	 getch();
	 return 0;
   }
Posted
Updated 18-Oct-14 4:08am
v3
Comments
George Jonsson 18-Oct-14 9:43am    
Why do you define an array of students, you only use the first position?
(struct student e[3])
Your return value is struct student and you return an array of students, so of course it fails.
TheSniper105 18-Oct-14 9:45am    
so how to insert data at index 1 ,index 2,index 3 without array ?
[no name] 18-Oct-14 9:56am    
You don't. Your array only has 3 elements allotted to it so if you use e[3] you will get another failure. What does this completely different question have to do with your original question?
TheSniper105 18-Oct-14 10:00am    
ok can you post your solution to achieve what i want ?
[no name] 18-Oct-14 10:05am    
Write your code for you? Uhm.... no. What you say you want and what your code does are completely different things. So even if I had the time or inclination to write your code for you I could not because what you want is not clear. Why are you trying to return an array from your function when you function signature says that you are going to return a single struct?

1 solution

You have defined the function as returning a student:
C#
struct student printstudent()
   {
But you try to return an array of students:
C#
struct student e[3];
...
return e;

You can't do that - they need to be exactly the same type.
Probably, you don't want to declare an array, just a single item, but since you don;t actually use it in the main function, you might be better changing it so that the printstudent function accepts a student to print and returns nothing (a void).

Mind you, I'd make printstudent print something instead of creating an instance myself anyway... :laugh:
 
Share this answer
 

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