string array in c -


how use string array parameter in c? if write function signature:

guess didnt explain myself well... i'll post code i'm trying work.

int format_parameters(char* str) {      char local_str[201] = "";     int = 0;     int j = 0;     int flip = 0;      while(str[i]) {          if((str[i] == '"') && (flip == 0)) flip = 1;//sentence allowed         else if((str[i] == '"') && (flip == 1)) flip = 0;//sentence not allowed          if(flip == 1) append_char(local_str, str[i]);         //check if space         else if(flip == 0) {              int c = str[i];             if(!isspace(c)) append_char(local_str, str[i]);              else {                 if((strlen(local_str) > 0) && (j < 4)) {                     //local-str copied param[j] here                     //printf("j = %d %s\n",j,local_str);                      local_str[0] = '\0';                     j++;                 }             }         }         i++;     }      //add \0 param      return flip; }//end format_parameters   void append_char(char* str, char c) {     int len = strlen(str);     str[len] = c;     str[len+1] = '\0'; }//end append_char  int main() {         char str[200];         //str filled stuff...         int x = format_parameters(str); } 

there should second (and third?) parameter in format_parameterssignature, char* param[5] should readable main.

does work?

#include <string.h> #include <assert.h> #include <stdio.h>  int format_parameters(char *str, char *param[], size_t nparam) {     char **next = param;     char **end  = param + nparam;     char  *data = str;      assert(str != 0 && param != 0 && nparam != 0);      while (next < end && *data != '\0')     {         *next++ = data;         data = strchr(data, ' ');   // choose own splitting criterion         if (data == 0)             break;         *data++ = '\0';     }     return(next - param);  }   int main(void)  {      char  str[] = "a b c d";      char *param[5];      int   nvals = format_parameters(str, param, 5);      int   i;       (i = 0; < nvals; i++)          printf("param %d: <<%s>>\n", i+1, param[i]);       return 0;   } 

the return value number of parameters found. if pass empty string, 0. beware leading, trailing , repeated blanks; code works - maybe not want to.


Comments

Popular posts from this blog

ASP.NET/SQL find the element ID and update database -

jquery - appear modal windows bottom -

c++ - Compiling static TagLib 1.6.3 libraries for Windows -