LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 02-04-2005, 05:05 PM   #1
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Rep: Reputation: 15
C question;how do I put struct in function?


Hi
I know this may sound very silly but I am newbie and this doubt seem to bother me much;
Here's my question :
I had been trying to write a function to print out the struction which hold a mailing list for starter and I could only do it in following way:
Code:
#include <stdio.h>
#include <string.h>
struct mailing {
        char name[60];/*First name ,last name*/
        char address[60];/*Two line of street address*/
        char city[40];
        char state[2];/*Two character abbreviation*/
        int zip;/*Numberic zip code*/
}list = {
        "something",
        "somethingl",
        "something",
        "something",
        70295404,};
void write(char names[60],char addresss1[60],char citys[40],char states[2],int zips){

        printf("The name is %s\n",names);
        printf("The first address is %s\n",addresss1);
        printf("The city name is %s\n",citys);
        printf("The state name is %s\n",states);
        printf("The zip code is %d\n",zips);
}

int main(){
        /*debug*/printf("The name is %s\n",list.name);
        /*debug*/write(list.name,list.address,list.city,list.state,list.zip);
        return (0);
}
And I was wondering if there are other way around to do this .Such as declaring struct mail in function parameter but my gcc compiler doesn't seem to allow me this.
Thanks for reading;
 
Old 02-04-2005, 05:16 PM   #2
Ephracis
Senior Member
 
Registered: Sep 2004
Location: Sweden
Distribution: Ubuntu, Debian
Posts: 1,109

Rep: Reputation: 50
I have also checked this code and it can compile and run. Just a note, I added an extra char to the state to hold the null-char. I don't know if you want this but unless you have a null-char you can't treat it as a string, just as a array of chars.

Code:
#include <stdio.h>
#include <string.h>
struct mailing {
        char name[60];          // full name
        char address[60];       // two line of street address
        char city[40];
        char state[3];          // two char abbr (+ null-char)
        int zip;                // zipcode
} list = {
        "something",
        "something",
        "something",
        "so",
        70295404,
};

void write(struct mailing list) {

        printf("The name is %s\n",list.name);
        printf("The first address is %s\n",list.address);
        printf("The city name is %s\n",list.city);
        printf("The state name is %s\n",list.state);
        printf("The zip code is %d\n",list.zip);
}

int main() {
        printf("The name is %s\n",list.name);
        write(list);
        return (0);
}
 
Old 02-04-2005, 06:03 PM   #3
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Original Poster
Rep: Reputation: 15
Thank U! This is How I wanted to write in the first place .I want to write a function
which could take struct as struct in function and print out the display .
I had tried to do something like :
Code:
void write(struct mailing{char name[60],char address[60] and so on}list;)
          printf("%s\n",name.list);
          /*and so on*/}
I never knew that I could declare as
Code:
void write(struct mailing list)
Thank man ,I am such a newbie in programming
 
Old 02-05-2005, 08:31 PM   #4
jonaskoelker
Senior Member
 
Registered: Jul 2004
Location: Denmark
Distribution: Ubuntu, Debian
Posts: 1,524

Rep: Reputation: 47
recommended reading

Hi.

I recommend that you read "The C Programming Language" [Kernighan (sp?) & Ritchie] -- the "C bible";

Jonas
 
Old 02-06-2005, 09:26 PM   #5
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Original Poster
Rep: Reputation: 15
Thanks man
I just download that one form internet ,I had been reading O'reaily 's Parctical C.
 
Old 02-06-2005, 10:13 PM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
FYI, for large structs and/or repeated calls in a loop, it's usual to pass a ptr to the struct instead; lower overhead (speed/mem) when running.
 
Old 02-07-2005, 04:57 PM   #7
JurajPsycho
Member
 
Registered: Sep 2004
Distribution: Debian, kernel 2.6.10
Posts: 50

Rep: Reputation: 15
why not to use typedef ?
Code:
typedef struct mailing
{
...
}list;
you wouldn't have to use
Code:
void write(struct mailing list)

but 

void write(mailing list)
J.
 
Old 02-07-2005, 05:06 PM   #8
itsme86
Senior Member
 
Registered: Jan 2004
Location: Oregon, USA
Distribution: Slackware
Posts: 1,246

Rep: Reputation: 59
I believe you mean void write(list list)
 
Old 02-07-2005, 05:24 PM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
If I could recommend a book to read, it would be
Linux Programming by Example : The Fundamentals

http://www.amazon.com/exec/obidos/AS...431031-0552630

This books uses the code from familiar commands like 'cat' and 'ls'.
It does a good job of showing how gnu commands are written and why.
 
Old 02-08-2005, 07:31 PM   #10
skoona
Member
 
Registered: Mar 2004
Location: Indiana, USA
Distribution: Fedora, CentOS, Ubuntu, OS/X, Raspbian
Posts: 90

Rep: Reputation: 18
Your code using a struct pointer

Code:
#include <stdio.h>
#include <string.h>

typedef struct mailing {
        char name[60];         /*First name ,last name*/
        char address[60];      /*Two line of street address*/
        char city[40];
        char state[3];         /*Two character abbreviation*/
        int zip;               /*Numberic zip code*/
} mlist, *P_list;

int fn_write( P_list plist )
{
  int  i_rc = 0;
  
  i_rc = fprintf ( stdout, 
                   "The name is %s\n"               /* notice careful use of comma's */
                   "The first address is %s\n"
                   "The city name is %s\n"
                   "The state name is %s\n"                   
                   "The zip code is %d\n",
                   plist->name,
  	           plist->address,
                   plist->city,
                   plist->state,
                   plist->zip
                 );
   
  return i_rc;
}

int main(int argc, char *argv[])
{
  int   i_rc = 0;
  mlist mylist = {
                  "something",
                  "somewhere",
                  "someplace",
                  "SW",
                   70295404
                 };

  printf ( "The name is %s\n", mylist.name );
  i_rc = fn_write ( &mylist );

  printf ( "The text buffer was %d in length\n", i_rc );

  return (0);
}
Using a pointer to the structure is a little more involved, but still straight forward.

note: compiled with $ gcc -Wall -W1 -o prog -c prog.c

James,

Last edited by skoona; 02-08-2005 at 07:38 PM.
 
Old 02-09-2005, 02:54 PM   #11
ngan_yine
Member
 
Registered: Aug 2003
Distribution: Slackware 10.1
Posts: 113

Original Poster
Rep: Reputation: 15
Thank you so much ,guy ,This really help me a lot .
 
  


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
c struct/table question kpachopoulos Programming 1 10-07-2005 01:51 PM
how to pass pointer of struct to function? jinxcat Programming 2 09-01-2005 09:29 AM
g++ and wrong struct member addresses / struct size misreporting sonajiso Linux - General 5 05-22-2004 10:16 PM
switch statement converting struct char to struct int oceaneyes2 Programming 2 12-10-2003 04:30 PM
C: struct as a function parameter zokik Programming 3 12-06-2003 02:52 PM

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

All times are GMT -5. The time now is 06:57 PM.

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