LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-17-2015, 06:49 AM   #1
findnerd2
LQ Newbie
 
Registered: May 2015
Location: India
Posts: 11

Rep: Reputation: 0
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);

}
 
Old 06-17-2015, 07:27 AM   #2
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
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
 
Old 06-17-2015, 07:36 AM   #3
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
Crossposted here

Last edited by gdejonge; 06-17-2015 at 07:38 AM.
 
Old 06-17-2015, 07:41 AM   #4
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,862
Blog Entries: 1

Rep: Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869Reputation: 1869
> But I am not getting correct output as the c program is showing some error.

Then please do something.
 
Old 06-17-2015, 07:48 AM   #5
Keith Hedger
Senior Member
 
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Void, Linux From Scratch, Slackware64
Posts: 3,150

Rep: Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856Reputation: 856
Quote:
Originally Posted by gdejonge View Post
Crossposted here
Does this count as cross posting after all that is a completly different forum?
 
Old 06-17-2015, 11:52 PM   #6
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
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.

Cheers
 
1 members found this post helpful.
Old 06-18-2015, 12:07 AM   #7
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194
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?

Last edited by astrogeek; 06-18-2015 at 12:10 AM.
 
Old 06-20-2015, 03:04 AM   #8
unSpawn
Moderator
 
Registered: May 2001
Posts: 29,415
Blog Entries: 55

Rep: Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600Reputation: 3600
Quote:
Originally Posted by gdejonge View Post
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 View Post
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.
 
2 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Error in passing parameters in function in Qt creator fright Programming 1 01-25-2013 03:31 AM
C passing a Array of Structuers as an argument in a function dman777 Programming 5 08-22-2011 06:16 PM
Passing data from interrupt handler function to tasklet function in kernel programmin double-out Programming 2 05-18-2010 10:10 PM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM
PHP Script argument passing error... lokee Linux - Software 5 04-24-2003 09:42 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:35 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration