Question:
Can we use functions within a structure?
Answer:
No,You cannot use functions within structure,it is compilation error but in c++ you can do it. Source: CoolInterview.com
Answered by: Vijay | Date: 1/23/2008
| Contact Vijay
No,You can't have any function in structure and union. Source: CoolInterview.com
Answered by: Akila | Date: 1/27/2008
| Contact Akila
In 'C' we can't use functions within a Structure, where as in C++ we can use. Source: CoolInterview.com
Answered by: Anuradha | Date: 2/26/2008
| Contact Anuradha
Yes we can use in inside the structure. Source: CoolInterview.com
Answered by: Navanee | Date: 2/26/2008
| Contact Navanee
We can't declare functions in side a structure. but we can take function pointers in structs; Ex:
struct ex { int i; void (*fun)(); }; void fun1() { printf(" This is a function "); } main() { struct ex e; e.fun=fun1; e.fun(); } Source: CoolInterview.com
Answered by: wizards | Date: 2/27/2008
| Contact wizards
we can't declare functions in side a structure. but we can take function pointers in structs; Ex:
struct ex { int i; void (*fun)(); }; void fun1() { printf(" This is a function "); } main() { struct ex e; e.fun=fun1; e.fun(); } Source: CoolInterview.com
Answered by: wizards | Date: 2/27/2008
| Contact wizards
In Case of C we can't use that but in case of c++ We can. Source: CoolInterview.com
Answered by: Ram | Date: 3/20/2008
| Contact Ram
/*Yes we can use functions inside structures.*/
#include<stdio.h> #include<conio.h> struct student { int name[25],fathername[25]; int rollnumber; void showstudent() { printf("The Student name is %s"); printf("The student's father is %s); printf("His roll number is %d"); } }s; /* s is a single element of the structure student */ void main() { printf("enter the name,fathername,rollnumber "); gets(s.name); gets(s.fathername); scanf("%d",&s.rollnumber); s.showstudent(); getch(); } /*Here in the above example void showstudent is a function which is inside the structure student.*/ Source: CoolInterview.com
Answered by: Kalav Nagarjuna | Date: 3/21/2008
| Contact Kalav Nagarjuna
A structure cannot hold the functions. Since by default structure access is public an Object can access any of the functions declared outside. Source: CoolInterview.com
Answered by: Archana | Date: 4/9/2008
| Contact Archana
one option is; we can place "pointer to a function" as a part of the structure to serve the purpose. Source: CoolInterview.com
Answered by: samarth | Date: 5/6/2008
| Contact samarth
If you have the better answer, then send it to us. We will display your answer after the approval.
Rules to Post Answers in CoolInterview.com:-
- There should not be any Spelling Mistakes.
- There should not be any Gramatical Errors.
- Answers must not contain any bad words.
- Answers should not be the repeat of same answer, already approved.
- Answer should be complete in itself.
|