The following piece of code works alright but it looks ugly.

The following piece of code works alright but it looks ugly. Can a better method be suggested to implement it. The problem is passing a structure as a parameter to a function and from it to another function. Don't burn me for the choice of function names.:)

#include
#include

struct addbook
{
char name[20];
char add[30];
};

// struct addbook again; this line also works if main is shown as void

void check(struct addbook again);
void show(struct addbook again);

main(struct addbook again)
{

clrscr();
check(again);
}

void check(struct addbook again)
{
printf("Enter the name.... ");
gets(again.name);
printf("Enter the address.... ");
gets(again.add);
show(again);
getch();
}

void show(struct addbook again)
{
printf("

The name is .... %s", again.name);
printf("

The address is ... %s, again.add);
}




Comments

  • Why are you passing the struct to the main() function? That is not ISO C nor C++ compliant.

    Also, the name "check()" is very vague and incoherent to what it does.

    Finally, you should pass those structures either by reference (I forget if C supports this though) or by pointers. The reason is that of the stack and performance. It is never a good idea to pass any structs by value (Which is what you are currently doing).

    I dont really have any other complaints though... Looks good :)

    If you wanted, you can move main() below the other function definitions, and get rid of the function prototype (forward declaration) lines.

    Probably a better method is getting rid of check() altogether, and providing a method to getInput:
    [code]#include
    #include

    struct addbook
    {
    char name[20];
    char add[30];
    };

    void show(struct addbook* again)
    {
    if (!again) return;

    printf("

    The name is .... %s", again->name);
    printf("

    The address is ... %s", again->add);
    }

    void getInput (struct addbook* again) {

    if (!again) return;

    printf("Enter the name.... ");
    gets(again->name);

    printf("Enter the address.... ");
    gets(again->add);
    }

    int main(void)
    {
    struct addbook again;

    clrscr ();
    getInput (&again);
    show(&again);
    getch();
    }[/code]

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • : Why are you passing the struct to the main() function? That is not
    : ISO C nor C++ compliant.
    :
    : Also, the name "check()" is very vague and incoherent to what it
    : does.
    :
    : Finally, you should pass those structures either by reference (I
    : forget if C supports this though) or by pointers. The reason is that
    : of the stack and performance. It is never a good idea to pass any
    : structs by value (Which is what you are currently doing).
    :
    : I dont really have any other complaints though... Looks good :)
    :
    : If you wanted, you can move main() below the other function
    : definitions, and get rid of the function prototype (forward
    : declaration) lines.
    :
    : Probably a better method is getting rid of check() altogether, and
    : providing a method to getInput:
    : [code]: #include
    : #include
    :
    : struct addbook
    : {
    : char name[20];
    : char add[30];
    : };
    :
    : void show(struct addbook* again)
    : {
    : if (!again) return;
    :
    : printf("

    The name is .... %s", again->name);
    : printf("

    The address is ... %s", again->add);
    : }
    :
    : void getInput (struct addbook* again) {
    :
    : if (!again) return;
    :
    : printf("Enter the name.... ");
    : gets(again->name);
    :
    : printf("Enter the address.... ");
    : gets(again->add);
    : }
    :
    : int main(void)
    : {
    : struct addbook again;
    :
    : clrscr ();
    : getInput (&again);
    : show(&again);
    : getch();
    : }[/code]:
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :
    MT2002,

    Many thanks for your response to my query. I was passing the struct to main merely to see if my compiler would groan in anguish and send me angry messages in protest. It didn't.:)

    The purpose of this little piece of code was to see how to pass a struct to a function and from within that to another. The pass by value was deliberate and, as I mentioned, ugly.

    As to the function names, the less said the better. They are truly dreadful.

    Your code is indeed elegant compared to my baby.:)

    Thanks again.

  • : Many thanks for your response to my query. I was passing the struct
    : to main merely to see if my compiler would groan in anguish and send
    : me angry messages in protest. It didn't.:)

    Of course not. However, just because it works does not make it correct.

    Technically, you can disregard main() altogether and redefine your own entry point routine. C++ gives alot of options that you can do with your code - even if it does not follow the standards. (There are reasons for this, as there are areas where its needed)

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
  • :
    : : Many thanks for your response to my query. I was passing the struct
    : : to main merely to see if my compiler would groan in anguish and send
    : : me angry messages in protest. It didn't.:)
    :
    : Of course not. However, just because it works does not make it
    : correct.
    :
    : Technically, you can disregard main() altogether and redefine your
    : own entry point routine. C++ gives alot of options that you can do
    : with your code - even if it does not follow the standards. (There
    : are reasons for this, as there are areas where its needed)
    :
    : [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS
    : Operating
    : System[rightbr][leftbr][link=http://www.brokenthorn.com]Website ::
    : OS Development Series[rightbr][/link][/size]
    :
    MT2002

    Now you have set me of thinking. Always thought that you need a main for C programming. I know for a Win32 Application you have WinMain but for a console application I had no idea. How can such a redefinition be done? Would love to know that
  • In an "hosted environment", ie on an OS, you can't use anything else but

    int main()

    That is true for both C and C++. WinMain is not ISO C/C++, it will not compile on standard compilers.

    As a programmer in a hosted environment, there is nothing you can do to change main(), it is up to the compiler manufacturer if they want to make a standard compiler or a non-standard one.

    On an embedded system, you don't need main(). Instead you can choose to rely on implementation-defined behavior.
  • : MT2002
    :
    : Now you have set me of thinking. Always thought that you need a main
    : for C programming. I know for a Win32 Application you have WinMain
    : but for a console application I had no idea. How can such a
    : redefinition be done? Would love to know that
    :

    I should probably correct myself here. I should not have said "C++ gives alot of options that you can do with your code" but rather those options depend entirely on your build envirement.

    Some envirements (embedded systems; no OSs; device drivers, etc) may require non ISO compliant setups that are specific to their envirement in order for them to work. Setting this up depends on your build tools and current system envirement.

    ^If you are interested in this, I would recommend getting a deeper knowledge of C++ before doing so. This is my personal opinion though.

    I *can* show you how with Visual Studio, but you should keep in mind that it would be coinsidered poor practice to use it in normal applications (from my experience) and that it is not at all portable.

    [hr][size=1][leftbr].:EvolutionEngine[rightbr][leftbr].:MicroOS Operating System[rightbr][leftbr][link=http://www.brokenthorn.com]Website :: OS Development Series[rightbr][/link][/size]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories