c - How can I parse numbers from a command line argument into two arrays -
i'm trying take 2 arguments command line, char , int string of 1's , 0's of x length. want check if it's 5 1's , 0's, if i'll put them array. if it's longer 5, ten such 10011 10110; in case want parse input 2 different arrays. first digit goes a, second 1 goes b , on until have 2 arrays such = 10101, b = 01110.
how can accomplish this? i've tried put(c) until eof while loop can't seem out of it. below entire program put arrays lll.
#include <stdio.h> #include <stdlib.h> struct nodea { int dork; struct nodea * next; }; typedef struct nodea d1; struct nodeb { int dork; struct nodeb * next; }; typedef struct nodeb d2; struct mathop { char operation; struct mathop * next; }; typedef struct mathop dorkop; int main(int argc, char * argv[]) { char a; int b; d1 * curr = null, * head = null; dorkop * curr3 = null, * head3 = null; int = 0; int aa[8] = {0}; int ab[8] = {0}; int tempa, tempb; if (argc != 3) { printf("program takes <char> <int>\n"); exit(-1); } = atoi (argv[1]); b = atoi (argv[2]); printf("test call = %c, b = %d \n", a, b); tempa = b; tempb = b; (i = 0; < 1; i++) { curr3 = (dorkop *)malloc(sizeof(dorkop)); curr3->operation = a; curr3->next = head3; head = curr; } while (curr3) { printf("%c\n", curr3->operation); curr3 = curr3->next; } (i = 0; < 1; i++) { curr = (d1 *)malloc(sizeof(d1)); curr->dork = tempa; curr->next = head; head = curr; } while (curr) { printf("%d\n", curr->dork); curr = curr->next; } return 0; }
i did not see question answer, here ops code, comments, prefixed '<--
' , following line indentation.
- good indentation helps wonderfully readability, have indented code (and modified struct , variable definitions align coding practice)
here compile results:
50:16: warning: variable 'tempb' set not used 48:9: warning: unused variable 'ab' 47:9: warning: unused variable 'aa'
i have not fixed of these items in following code. aside, why post code not compile?
btw: code, supplied op, none of actions listed in op's remarks.
#include <stdio.h> #include <stdlib.h> struct nodea { int dork; struct nodea * next; }; typedef struct nodea d1; struct nodeb { int dork; struct nodeb * next; }; typedef struct nodeb d2; struct mathop { char operation; struct mathop * next; }; typedef struct mathop dorkop; int main(int argc, char * argv[]) { char a; int b; d1 * curr = null; d1 * head = null; //d2 * curr2 = null; //d2 * head2 = null; dorkop * curr3 = null; dorkop * head3 = null; int = 0; //int array[10] = {0}; int aa[8] = {0}; int ab[8] = {0}; int tempa; int tempb; if(argc != 3) { printf("program takes <char> <int>\n"); exit(-1); } = atoi (argv[1]); // <-- argv[1] expected char, // , 'a' defined char // result of atoi() 0 // , 'a = 0' not yield useful b = atoi (argv[2]); // <-- argv[2] null terminated string of 1s , 0s, // easy parse, why conversion? printf("test call = %c, b = %d \n", a, b); // <-- 'a' unprintable char, output doubtful tempa = b; // <-- shouldn't be: tempa = a; tempb = b; for(i = 0; < 1; i++) // <-- waste of 'for' because room 1 dorkop // , 'for' execute once // suggest removing 'for' not enclosed code block { curr3 = (dorkop *)malloc(sizeof(dorkop)); // <-- in c, casting returned value malloc // bad programming practice, several reasons // <-- returned value malloc() needs checked // assure successful operation // if not successful, // then, following lines dereferencing offset 0 // result in seg fault event curr3->operation = a; // <-- 'a' contains 0x00, (see above comment setting 'a') // curr3->operation contains 0x00 curr3->next = head3; // <-- head3 contains null // line places null in curr3->next head = curr; // <-- curr , head both ptrs containing null // line has no effect } while(curr3) { printf("%c\n", curr3->operation); // <-- curr3->operation (see above) set 0x00 // non-printable char // displayed output in doubt curr3 = curr3->next; // <-- curr3, on first pass through loop set null } for(i = 0; < 1; i++) // <-- waste of 'for' because room 1 d1 // , 'for' execute once // suggest removing 'for' not enclosed code block { curr = (d1 *)malloc(sizeof(d1)); // <-- in c, casting returned value malloc // bad programming practice, several reasons // <-- code needs check returned value malloc // assure successful operation // if not successful // then, following code dereferencing address 0 // result in seg fault event curr->dork = tempa; curr->next = head; // <-- head contains null curr-> contains null head = curr; } // <-- following loop execute once // why make loop? while(curr) { printf("%d\n", curr->dork); curr = curr->next; } return 0; } // end function: main
Comments
Post a Comment