Ok.. I know how to fill an array, but how does one go about filling an array from command line? Like c:\program1.exe 2.0 1.3 2.2
using floats as my example. Thank you for any help in advance.
Ok.. I know how to fill an array, but how does one go about filling an array from command line? Like c:\program1.exe 2.0 1.3 2.2
using floats as my example. Thank you for any help in advance.
declare your main function like this:
int main(int argc, char **argv)
int argc contains the number of arguments you passed to your command line including the program name.
char argv is an array of char arrays. Each one will contain the float you passed to it.
You can simply declare a fixed size array and traverse through argv to read in your floats. Remember that it's a string and you will need to convert it to a float by using atof(). You can also make it more advanced by reading in argc and dynamically creating your array during runtime.
EDIT - must...fix.....grammar
Last edited by Cshot; 09-26-2002 at 01:53 PM.
Try not.
Do or do not.
There is no try.
- Master Yoda
CShot beat me...but here's some code anywayCode:# include <stdio.h> # include <stdlib.h> int main (int argc,char** argv) { int i; float *f; if(argc<=1){ printf("No Params!"); return 1; } f = malloc(sizeof(float)*argc-1); if(!f)return 1; for(i=0;i<argc-1;i++) f[i] = (float)atof(argv[1+i]); for(i=0;i<argc-1;i++) printf("Float found! - %2.2f\n",f[i]); free(f); return 0; }
Thank you both so very much!!