Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Article Writing
  4. Programming Problem

Programming Problem

Scheduled Pinned Locked Moved Article Writing
helpperformancequestion
9 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    mvworld
    wrote on last edited by
    #1

    Hi I have to write a C program to take any number of arguments of any type from the user at runtime. I dont want to declare arrays of all the data types as that would be wastage of memory. Is there an efficient way to input any number of arguments from the user of any type? Please help as this is a part of my college project!! Thanks:confused:

    T M 2 Replies Last reply
    0
    • M mvworld

      Hi I have to write a C program to take any number of arguments of any type from the user at runtime. I dont want to declare arrays of all the data types as that would be wastage of memory. Is there an efficient way to input any number of arguments from the user of any type? Please help as this is a part of my college project!! Thanks:confused:

      T Offline
      T Offline
      Thomas Freudenberg
      wrote on last edited by
      #2

      Try va_list a.k.a. variable-argument list. It's obvious, isn't it ? ;) HTH, Thomas


      I am a signature virus! Help me spread and copy me to your sig!

      M 1 Reply Last reply
      0
      • T Thomas Freudenberg

        Try va_list a.k.a. variable-argument list. It's obvious, isn't it ? ;) HTH, Thomas


        I am a signature virus! Help me spread and copy me to your sig!

        M Offline
        M Offline
        mvworld
        wrote on last edited by
        #3

        Can you show me an example? Thanks:)

        T 1 Reply Last reply
        0
        • M mvworld

          Can you show me an example? Thanks:)

          T Offline
          T Offline
          Thomas Freudenberg
          wrote on last edited by
          #4

          Here is en example, found at MSDN:

          void main( void )
          {
          /* Call with 3 integers (-1 is used as terminator). */
          printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

          /* Call with 4 integers. */
          printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

          /* Call with just -1 terminator. */
          printf( "Average is: %d\n", average( -1 ) );
          }

          int average( int first, ... )
          {
          int count = 0, sum = 0, i = first;
          va_list marker;

          va_start( marker, first ); /* Initialize variable arguments. */
          while( i != -1 )
          {
          sum += i;
          count++;
          i = va_arg( marker, int);
          }
          va_end( marker ); /* Reset variable arguments. */

          return( sum ? (sum / count) : 0 );
          }

          HTH, Thomas


          I am a signature virus! Help me spread and copy me to your sig!

          M 1 Reply Last reply
          0
          • T Thomas Freudenberg

            Here is en example, found at MSDN:

            void main( void )
            {
            /* Call with 3 integers (-1 is used as terminator). */
            printf( "Average is: %d\n", average( 2, 3, 4, -1 ) );

            /* Call with 4 integers. */
            printf( "Average is: %d\n", average( 5, 7, 9, 11, -1 ) );

            /* Call with just -1 terminator. */
            printf( "Average is: %d\n", average( -1 ) );
            }

            int average( int first, ... )
            {
            int count = 0, sum = 0, i = first;
            va_list marker;

            va_start( marker, first ); /* Initialize variable arguments. */
            while( i != -1 )
            {
            sum += i;
            count++;
            i = va_arg( marker, int);
            }
            va_end( marker ); /* Reset variable arguments. */

            return( sum ? (sum / count) : 0 );
            }

            HTH, Thomas


            I am a signature virus! Help me spread and copy me to your sig!

            M Offline
            M Offline
            mvworld
            wrote on last edited by
            #5

            Hi thanks again :) But in your code you use all int arguments. Only the number of arguments is changing here. But I want to be able to multiple type of arguments!!! like float, char, long etc....possibly all of them!! is that possible with va_args?

            T D 2 Replies Last reply
            0
            • M mvworld

              Hi thanks again :) But in your code you use all int arguments. Only the number of arguments is changing here. But I want to be able to multiple type of arguments!!! like float, char, long etc....possibly all of them!! is that possible with va_args?

              T Offline
              T Offline
              Thomas Freudenberg
              wrote on last edited by
              #6

              IMHO, unfortunately, you have to know the type at compile-time. Regards, Thomas


              I am a signature virus! Help me spread and copy me to your sig!

              L 1 Reply Last reply
              0
              • T Thomas Freudenberg

                IMHO, unfortunately, you have to know the type at compile-time. Regards, Thomas


                I am a signature virus! Help me spread and copy me to your sig!

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                This is trivial. Either that or I don't understand the problem. Your requirements: Store variable arguments of different types at runtime. Type is irrelevant. It will all be ascii at first. Then you detect your types and convert them. See, since YOU write the program, YOU decide the types. The arguments will be in char **argv and the number of them will be int argc. Write code to print out the value of argc. When that works, modify it to also print the argument strings, one by one. You are 90 percent finished. Now for the other 90 percent... :) Modify your code so that, instead of printing the arguments, you are PARSING them. First, decide which types you will support. Start with int and char strings. For each argument string, check each character. If you get to the end and find only digits, you have an int. Call atoi() on it and store it in a freshly newed int. Be careful of overflow - might wanna use longs and atol(). If it wasn't an int, it's a character string. New up a buffer and strcpy it over. To add another type, you must invent a way to detect the type and then you must find a way to convert the ascii string to your desired type.

                1 Reply Last reply
                0
                • M mvworld

                  Hi thanks again :) But in your code you use all int arguments. Only the number of arguments is changing here. But I want to be able to multiple type of arguments!!! like float, char, long etc....possibly all of them!! is that possible with va_args?

                  D Offline
                  D Offline
                  David Fleming
                  wrote on last edited by
                  #8

                  This may not be the way you want to go about it, but... You can write an overload function for each of the different variable types. So, if the function for integers is... (as was written in the previous answer) int average( int first, ... ) { int count = 0, sum = 0, i = first; va_list marker; va_start( marker, first ); /* Initialize variable arguments. */ while( i != -1 ) { sum += i; count++; i = va_arg( marker, int); } va_end( marker ); /* Reset variable arguments. */ return( sum ? (sum / count) : 0 ); } Then you just overload the function for other variable types such as: long average(long first, ...) { ... } double average(double first, ...) { ... } Etc. Then, when the call is made, the proper function for the variable type is called for you. Voila'.

                  1 Reply Last reply
                  0
                  • M mvworld

                    Hi I have to write a C program to take any number of arguments of any type from the user at runtime. I dont want to declare arrays of all the data types as that would be wastage of memory. Is there an efficient way to input any number of arguments from the user of any type? Please help as this is a part of my college project!! Thanks:confused:

                    M Offline
                    M Offline
                    Malcolm McMahon
                    wrote on last edited by
                    #9

                    The question is somewhat confusing. By taking arguments from the user do you mean. 1) Reading program parameters from the command line. 2) Interpretting variable arguments passed to a subroutine from the caller. 3) Prompting for values to be put in. Various respondants seem to have interpretted it various ways. I guess 1) You'll probably loop through the parameters one by one. For each one you can try various conversions. The simplest way is probably to use sscanf. Try a format string like "%i%c" (tries to read the extra character to ensure the whole string was an integer). If it's not an integer try for a float. If it's not a float then treat is as a string. What you do with the arguments then really depends on the object of the excercise. You could, if you wanted, store them in a structure like -- struct myArg { short int type; union { float f_val; int i_val; char *string_val; } };

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups