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 12-15-2007, 03:40 AM   #1
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Rep: Reputation: 15
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).
 
Old 12-15-2007, 06:07 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 12-15-2007, 11:47 AM   #3
xlq
Member
 
Registered: Feb 2007
Distribution: Slackware 12.0
Posts: 58

Rep: Reputation: 15
Code:
struct http_headers {
    /* fields common to both */
};

struct http_request {
    struct http_headers h;
    /* fields specific to http_request */
};

struct http_response {
    struct http_headers h;
    /* fields specific to http_response */
};

void dostuff(struct http_headers *h)
{
    /* do stuff to http request/response */
}
Instead of passing the http_request or http_response structs to dostuff(), pass a pointer to the h field of either.
 
Old 12-16-2007, 01:57 AM   #4
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Original Poster
Rep: Reputation: 15
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
 
Old 12-16-2007, 03:33 AM   #5
xlq
Member
 
Registered: Feb 2007
Distribution: Slackware 12.0
Posts: 58

Rep: Reputation: 15
Why aren't you using C++? Because C++ has operator overloading, and other such features which obscures the meaning of the code.
 
Old 12-16-2007, 01:40 PM   #6
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Original Poster
Rep: Reputation: 15
Lol hmm....is there any reason for C to exist anymore, given C++ is everything C is and better? What exactly does C have over C++?

Last edited by greeklegend; 12-16-2007 at 01:50 PM. Reason: Added a question mark :P
 
Old 12-16-2007, 02:08 PM   #7
paulsiu
Member
 
Registered: Apr 2006
Posts: 143

Rep: Reputation: 15
Quote:
Originally Posted by greeklegend View Post
Lol hmm....is there any reason for C to exist anymore, given C++ is everything C is and better? What exactly does C have over C++?
Well, it's possible that for embedded systems, C++ may add an overhead over C. C exists because a lot of stuff is written in C.

Paul
 
Old 12-17-2007, 06:18 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
Lol hmm....is there any reason for C to exist anymore, given C++ is everything C is and better? What exactly does C have over C++?

it's readable

er, this for one:
Code:
    stdout = (FILE *) fp;
 
Old 12-17-2007, 11:43 PM   #9
greeklegend
Member
 
Registered: Feb 2006
Location: At a computer
Distribution: Ubuntu 7.04, LFS 6.3 rc1 (living dangerously ;), Windows XP
Posts: 75

Original Poster
Rep: Reputation: 15
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.
 
Old 12-18-2007, 03:23 AM   #10
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
Quote:
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?"

 
Old 12-18-2007, 07:32 AM   #11
paulsiu
Member
 
Registered: Apr 2006
Posts: 143

Rep: Reputation: 15
Quote:
Originally Posted by bigearsbilly View Post
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.
 
Old 12-18-2007, 10:47 AM   #12
PAix
Member
 
Registered: Jul 2007
Location: United Kingdom, W Mids
Distribution: SUSE 11.0 as of Nov 2008
Posts: 195

Rep: Reputation: 40
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.
 
Old 12-18-2007, 11:45 AM   #13
xlq
Member
 
Registered: Feb 2007
Distribution: Slackware 12.0
Posts: 58

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


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
similar flavours thok Linux - Newbie 6 10-24-2007 10:54 AM
Looking for somewhat similar to potatoque Linux - Software 4 10-14-2006 11:50 PM
Need C help with struct's and pointers djgerbavore Programming 9 09-03-2005 04:21 PM
dc++ or similar porio Linux - Software 3 04-27-2005 09:00 AM
Something similar to webmin? yongbeng Linux - Software 4 05-21-2003 03:44 AM

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

All times are GMT -5. The time now is 05:43 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