CoolInterview.com - World's Largest Collection of Interview Questions
Send Free SMS
 Interview Questions  
 Our Services  


INTERVIEW QUESTIONS LANGUAGES C DETAILS
Question :
Can we use functions within a structure?

Posted by: Deekshith on 11/19/2007

Contact Deekshith  Contact Deekshith
Category C Interview Questions
Rating (4.0) By 1 users
Added on 11/19/2007
Views 2136
Rate it!
Answers:

No,You cannot use functions within structure,it is compilation error but in c++ you can do it.



 Posted by: Vijay    

Contact Vijay  Contact Vijay

No,You can't have any function in structure and union.



 Posted by: Akila    

Contact Akila  Contact Akila

In 'C' we can't use functions within a Structure, where as in C++ we can use.



 Posted by: Anuradha    

Contact Anuradha  Contact Anuradha

Yes we can use in inside the structure.



 Posted by: Navanee    

Contact Navanee  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();
}



 Posted by: wizards    

Contact wizards  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();
}



 Posted by: wizards    

Contact wizards  Contact wizards

In Case of C we can't use that but in case of c++ We can.



 Posted by: Ram    

Contact Ram  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.*/



 Posted by: Kalav Nagarjuna    

Contact Kalav Nagarjuna  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.



 Posted by: Archana    

Contact Archana  Contact Archana

one option is; we can place "pointer to a function" as a part of the structure to serve the purpose.



 Posted by: samarth    

Contact samarth  Contact samarth


If you have the better answer, then send it to us. We will display your answer after the approval.
Name :*
Email Id :*
Answer :*
Verification Code Code Image - Please contact webmaster if you have problems seeing this image code Not readable? Load New Code
Process Verification  Enter the above shown code:*
Inform me about updated answers to this question

   
Related Questions
View Answer
void main()
{
float a= 0.7;
if (a < 0.7)
printf("c");
else
printf("c++");
}
Output of the above program is c. Why? Whereas the same program with 0.8 instead of 0.7 gives c++ as the output? Why explain?
View Answer
Code for swapping of two numbers without using temporary variable using C.
View Answer
code To draw a three dimensional graph using c graphics
View Answer
How to write a C program for displaying a sentence without output command?
View Answer
What is difference between the test effort and the test procedure?
View Answer
What are the disadvantages of using Pointers.
View Answer
How can I convert a number to a string?
View Answer
How to write a program for pascal triangle?
View Answer
write a program to remove comment lines and blank lines from an error free c program.
View Answer
What use of structure and union?
View Answer

Please Note: We keep on updating better answers to this site. Subscribe to our newsletter to get notified when better answer is posted.

Notify me when better answer is posted!
Email:

View ALL C Interview Questions

User Options
Sponsored Links


Copyright ©2003-2010 CoolInterview.com, All Rights Reserved.
Privacy Policy | Terms and Conditions
Page URL: http://www.coolinterview.com/interview/12491/default.asp?cachecommand=bypass


Download Yahoo Messenger | Placement Papers| FREE SMS | ASP .Net Tutorial | Web Hosting | Free SMS | Dedicated Servers | Joke of the Day

1.05