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.
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75
Rep:
C similar struct's
Hi guys
I have two similar structures that differ in only a couple of elements (called http_request and http_response). I have written code that processes HTTP headers which works the same way for both requests and responses.
Is there a way i can write a function that can take ether a http_request or a http_response argument and do exactly the same thing to it? (the relevant members that this function needs to do are identical in both struct's).
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
If the structure members you use are at the same offset, you may have your function accepting a void * and cast it to use either structure pointer.
This is however poor coding. A better and less risky approach would be splitting your structure in a common trunk and an union of two other structures containing the differing members.
An even better approach would be using a object oriented language as class inheritance is really what you are after.
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75
Original Poster
Rep:
So somethig like this would work?
Code:
//It's important for the common members to be at the top of the struct yes?
typedef struct{int common_member1;
int common_member2;
int not_common_member;} http_request;
typedef struct{int common_member1;
int common_member2;
unsigned long double not_common_member;
char* not_common_member_too;} http_response;
void dostuff(void* arg)
{
http_response* tmp;
tmp = (http_response*)arg
tmp->common_member1 = 8; //works
tmp->not_common_member = 9000000.3423473; //doesn't work, if http_request was passed to the function i'm assigning unsigned long double to int
return;
}
I can see why this is poor coding.
I might consider modifying the structs to store common information in a different struct
As for object oriented language...meh i don't really know why i'm using C for this since i can use C++ just fine. mabye I just like having a more intimate understanding of what exactly my program is doing.
Btw thanks guys
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75
Original Poster
Rep:
You can assin to stdout and stdin like that? Hehe you learn something new every day.
In other news i've decided to use the ugly "typecast void* to http_request*" hack i posted earlier, since it turns out my struct's are identical- I managed to make the only difference the name of the members, and since nobody except me will ever see this code, readability can go to hell lol.
have you ever gone back a year later and said "what the hell was I on about?"
You know, I had this conversation with my lead programmer from a decade ago. We wanted him to commented because we couldn't read his code. He never commented because he said that code should be readable on his own. I notice he couldn't read his own code 6 months later.
I remember testing an application program a few years back and was not able to break it (I cheated and had a squint at the source code looking for clues to weaknesses). Imagine my surprise when I realised that I had actually written it. I was more than impressed at some of the constructs that I had used. Software developer - elaborate and refine.
I don't suppose there are many of us without that particular Tshirt.
C++ has polluted the namespace, broken void pointers, mangled all the identifiers and induced a gross lack of compilation speed. C++ is by no means a superset of C.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.