Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I have a shape that will be defined by:

char s_type;
char color;
double height;
double width;

These variables are scanned in from a request string sent to my server and passed into my printing function, which then prints out the shape. Currently they are just local variables sitting in my main(); however, I was wondering if there would be any advantage in creating a struct containing these variables, and then passing the struct to my printing function? or how else might I improve my program's structure/style, would passing a struct by reference have any kind of performance benefit if there were many requests and therefore many printing function calls?

printer(char st, char cr, double ht, double wd);

int main()
{
    // Other main functionality.

    char s_type;
    char color;
    double height;
    double width;

    sscanf (serv_req, "GET /%c/%c/%lf/%lf", &s_type, &color, &height, &width);

    printer(s_type, color, height, width);

    // Other main functionality.

    return 0;
}

It seemed "neater" if I had a struct or something that didn't leave me with declarations in the middle of everything else going on in main. I'm interested in structure/style as well as performance.

EDIT: didn't mean to put printer declaration inside main.

share|improve this question
    
So what's stopping you from creating a struct if you think that'd help? –  Caleb Apr 9 '12 at 3:12
    
@Caleb: I wasn't sure if it would help or not, hence the question. It was just an idea, wanted to hear how others would group them etc. –  han42 Apr 9 '12 at 3:15
add comment

2 Answers

Style is pretty subjective; if using a struct makes the code look better to you, do it. Unless your printer() function is trivial the way you pass the data (struct or not, by reference or not) won't make a measurable difference.

Probably the best thing you could do for your code would be to break out the various parts of main() indicated by the "other main functionality" comments into separate functions.

share|improve this answer
add comment

You can use macros to achieve something like:

sscanf4(serv_req,"GET /%c/%c/%lf/%lf", typ, col, h, w, char, char, double, double);
printer(typ, col, h, w);

As for performance, using structs won't make much difference. You can double check that adding for-loop, looping though whole thing 10000 times, and calling timeGetTime() to measure time spent. Usually possibilities to optimize are not where you think they are.

share|improve this answer
    
Edited, I didn't mean to imply that the writing out the types in my printer() function was the problem. That was just a typo :S So, you don't think that passing a struct into printer() as one argument instead of four is of any consequence? –  han42 Apr 8 '12 at 14:53
    
most likely no. after all it's not like some time critical image-processing. btw another ways to achieve same optimization is adding __forceinline attribute to printer function, or changing arguments to "(const char&, const char&, const double&, const double&)". –  AareP Apr 8 '12 at 16:54
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.