Hello All,
I want to pass a structure through a function. I am new in C programming. Help me.
I am getting an error as bad function type.
Hello All,
I want to pass a structure through a function. I am new in C programming. Help me.
I am getting an error as bad function type.
it would be better if you posted your program but here is an overview of how to pass a structure as an argument :
#include<stdio.h>
just make sure that a copy of the structure is passed in this case, if you want to change values in a structure inside a function you need to pass a pointer to that structure.Code:struct node { int item; }; void func1(struct node a); int main() { struct node b; b.item = 1; func1(b); return 0; } void func1(struct node b) { printf("%d", b.item); }
my car makes a clink-clack sound when i'm driving it. how do i fix it?
EDIT: oh, sorry. i thought the thread title said "Crappy Questions"
EDIT 2: how did ya manage to 'sneak one in' on me gemini?...2 minutes apart... how wierd
Last edited by misplaced; 05-04-2005 at 03:30 AM.
i seem to have GCC 3.3.4
But how do i start it?
I dont have a menu for it or anything.
please post your code so that we can find where u are going rough. any way here is sample code to how pass struct to the function
s.s.harishCode:#include<stdio.h> /* skeleton of struct student or the abstract datatype*/ struct student { int studno; char name[20]; }; /* fucniotn to print details of each student where it takes struct student as a parameter*/ void printdetails(struct student tempst) { printf("Student Id %d\n", tempst.studno); printf("name %s",tempst.name); } /*fucniotn to clear the input buffer*/ void clr_buf() { char ch; while((ch=getchar())!='\n' && ch!=EOF); } int main() { struct student s1; // instance of type struct student printf("Enter Stduent id\n"); scanf("%d",&s1.studno); clr_buf(); // clear the input buffer printf("Enter name\n"); fgets(s1.name,20,stdin); printdetails(s1); // call printdetails by sending the instance getchar(); }
hahahahahhahah I wonder myself
a better reply than mine though
Hello ,
I will type the code
#include<stdio.h>
Code:typedef struct { int id; char info[100]; int salary; } linklist; void addComission(linklist*,int); main() { int ids,salaries; char tempname[100]; linklist l1; printf("Enter the id of the person "); scanf("%d",&ids); printf("Enter the name of the person "); scanf("%s",tempname); printf("Enter the salary of the person"); scanf("%d",&salaries); l1.id=ids; strcpy(l1.info, tempname); l1.salary=salaries; if(l1.salary > 3000) { addComission(&l1,200); printf("Id is %d\n" , l1.id); printf("Name is %s\n",l1.info); printf("Salary is %d\n",l1.salary); } else{ printf("Id is %d\n" , l1.id); printf("Name is %s\n",l1.info); printf("Salary is %d\n",l1.salary); } getchar(); } void addComission(linklist *l1, int bonus) { int salarys; salarys = l1->salary; salarys = salarys + bonus; }.
Hey with your code I got the following error.
c:\program files\miracle c\structure.c: line 92: illegal dereference
'printdetails(s1)'
aborting compile
btw what is the problem you have in your code.
It worked fine when i compile and i dont get any compilation errors
In your code you missed including string.h file and in addCommision() you omitted l1->salary = salarys
rest and all is ok...
regards
Ravi
program works fine just change this
add string.h file and your addCommision() should look like this if I am correct :
value assignment in C is from right to left not from left to rightCode:void addComission(linklist *l1, int bonus) { int salarys; salarys = l1->salary; l1->salary = salarys + bonus; }
use fgets() to read string rather than scanf()
find out the reason fgets in FAQ
s.s.harish
> c:\program files\miracle c\structure.c: line 92: illegal dereference
Oh no, not another one
Miracle-C is a piece of ........ - just get rid of it now!
That compiler needs taking off the net so badly, it's such a piece of crap it's not true (and not funny either).
Do yourself a HUGE favour and get a real compiler (not some toy), like for example DEV-C++.
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
If at first you don't succeed, try writing your phone number on the exam paper.