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.
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;
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);
}
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*/}
#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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.