Tuesday 15 January 2013

lisp - Processing Lists of char * in C? -


I need to do different functions on the long list of stars in C.

  char * list 1 [] = {"one", "two", "three", "four", "five", "six"};   

Initially, I am trying to implement Simple Car / CDR functions, but it is a while and I I forgot the best way to manage a list of four * [] in the CD. In the rest of the list of char * s in a fresh malloc'ed array, in my case it is "two", "three", "four", "five", "six". Unknown list size unknown It is complicated. Maybe, should I use a straight line with the length of the list?

  char ** cdr (char * aList []) {/ * malloc for space n-1 char * * / return aList; // Return to check everything}   

For example, I will have other functions, list all the words with two consecutive vowels, or return in more than 10 characters. Never want to change the wire (i.e. they are irreversible). I am malling memory in functions like CDR, so I will need to keep an eye on it because I take my results and send it to other tasks. Anyway, if someone can start me, then it would be very appreciated thanks.

  four * list1 [] = {"one", "two", "three", "four", "five", "six"}; // Short list room ** CDR (four * ALIISIS []) {/ * N-1 character * * / return alive for MLL; // to check everything}} four * car (four * alist []) {ILIF [0]; } Int main () {const char * first; Four * rest []; Int len ​​= sizeof (list1) / sizeof (four *); Printf ("list1 len =% d \ n", lane); First = car (list 1); Rest = CDR (list1); Printf ("% s \ n", first); LAN = shape (left) / size (four *); Printf ("rest lane =% d \ n", lane); Return 0; }   

unknown unknown list size not so much "it makes complex" As "it makes it impossible" If you do not want to go around the clear length, then you need to use a null value as the Terminator.

  int length (const char * string []) {int rv = 0; While (string [RV]) ++ RV; Return RV .; } Int main (int argc, char * argv []) {printf ("Note that this technique is also used in argv:% p \ n", argv [argc]); Const * list [] = {"+", "42", "30", NULL}; Printf ("length:% d \ n", length (list)); Return 0; }   

Using such length () in your other actions, you should be able to know the appropriate amount of memory to allocate. (Remember that you need to allocate space for extra pointer for zero!)

No comments:

Post a Comment