Getting Error While Passing Structure as a function Argument in C ?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Getting Error While Passing Structure as a function Argument in C ?
Hello,
I was trying to pass structure as a function argument in c programming language, and for this I created the Object of the Structure and pass it as an argument to the function. But I am not getting correct output as the c program is showing some error. I hope this is one of the highly specific c programming questions and answers forum where I can get solutions to my query. Can anyone help me to resolve this issue?
Code:
#include<stdio.h>
#include<conio.h>
struct anp//Name of the structure
{
int rlno; //Structure elements
char name[15];
}p;//structure variable
void print(struct anp *);
void main()
{
clrscr();
printf("Enter the rlno and name\n");
scanf("%d%s",&p.rlno,p.name);
print(&p);//calling the function print and &p is structure Argument
getch();
}
void print(struct anp q)
{
printf("Entered ROLLNO =%d Entered Name= %s",q->rlno,q->name);
}
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150
Rep:
This:
Code:
#include <stdio.h>
#include <stdlib.h>
//#include <conio.h>
struct anp//Name of the structure
{
int rlno; //Structure elements
char name[15];
}p;//structure variable
void print(struct anp *);
void main()
{
//clrscr();
printf("Enter the rlno and name\n");
scanf("%d%s",&p.rlno,p.name);
print(&p);//calling the function print and &p is structure Argument
//getch();
}
void print(struct anp *q)// in original code you forgot the '*'
{
printf("Entered ROLLNO =%d Entered Name= %s\n",q->rlno,q->name);
}
Works like so:
Code:
keithhedger@LFSCerebro:/tmp-> ./CScript.c
Enter the rlno and name
100
aname
Entered ROLLNO =100 Entered Name= aname
For future refernce can you include how you are compiling the program, makefile/gcc and if so what args you are using and what errors you are getting, it just makes it easier, the more info you include the easier it is to spot the errors
It's more about the fact that the person is spamming a lot of different sites with the same questions and then link baiting the question with links to findnerd.com.
I'm not not sure if that's again forum rules, but i thought to mention it here.
Yes it is against the rules, not to mention disrespectful of all other users of those forums and especially inconsiderate of the time wasted by those well intentioned people who take the time to post a helpful reply that is actually of no use to the OP.
Maybe we should start a boycott of site being linked, and email them with our displeasure?
I'm not not sure if that's again forum rules, but i thought to mention it here.
If unsure next time use the "report" button on the (supposedly) offending post and ask a moderator as there's really no need to dilute attention to solving the problem with "meta" discussions here.
Quote:
Originally Posted by astrogeek
Yes it is against the rules
While LQ members presence elsewhere does help gauge things this behaviour is in NO WAY against the LQ Rules as 0) we have no authority or time to watch for events happening outside our sphere of influence and 1) changing behaviour on LQ based on nfo elsewhere could artifically limit a persons freedoms.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.