简体   繁体   中英

c pass a string to function then return a string

After exploring the internet, i've wrote the following script, but still got errors, anyone knows what the errors are?( the line with //err)

typedef struct name_value_set {
    char name[250]; 
    char value[250]; 
} nv_set;

char * getInput2(char *param) {
    char *my_data=0;
    int data_len;
    char *tmp_ptr, *tmp;
    int i;
    nv_set *nv;

    data_len=atoi(getenv("CONTENT_LENGTH"));
    char field[data_len]; 

    my_data=(char*)malloc(sizeof(char)*(data_len+1));
    fread(my_data,1,data_len,stdin);

    i=0;
    while (my_data[0]!='\0') {
     tmp=split(my_data,'='); // err
     makespace(tmp); 
     tmp=convert(tmp); // err
     strcpy(nv[i].name,tmp); // 
     tmp=split(my_data,'&'); //err 
     makespace(tmp); // 
     tmp=convert(tmp); // err
     strcpy(nv[i].value,tmp); // 
     i++;
    }
    i--; // 

    int j=0;
    for (j=0; j<i; j++) {
    if(nv[j].name == param) {
        return nv[j].value;
    }


}


void makespace(char *s)
{
    int i,len;
    len=strlen(s);

    for (i=0;i<len;i++) {
      if (s[i]=='+')
        s[i]=' ';
    }
}

char *split(char *s, char stop)
{
    char *data;
    char *tmp;
    int i,len,j;

    len=strlen(s);
    tmp=s;
    data=(char*)malloc(sizeof(char)*(len+1));

    for (i=0;i<len;i++) {
       if (s[i]!=stop)
         data[i]=s[i]; // 
      else {
        i+=1; // 
          break;
       } 
    }
    data[i]='\0';
    for (j=i;j<len;j++)
      s[j-i]=tmp[j];
      s[len-i]='\0';
     return data;
}


char *convert(char *s)
{
    int x,y,len;
    char *data;

    len=strlen(s);
    data=(char*)malloc(sizeof(char)*(len+1));
    y=0;
    for (x=0;x<len;x++) {
    if (s[x]!='%') {
        data[y]=s[x]; // 
          y++;
      }
    else {

        data[y]=(char)(16*hexa(s[x+1])+hexa(s[x+2]));
        y++;
        x=x+2;
      }
    }
     data[y]='\0';
     return data;
}


int hexa(char c)
{
    switch(c) {
    case '0':return 0;
    case '1':return 1;
    case '2':return 2;
    case '3':return 3;
    case '4':return 4;
    case '5':return 5;
    case '6':return 6;
    case '7':return 7;
    case '8':return 8;
    case '9':return 9;
    case 'A':return 10;
    case 'B':return 11;
    case 'C':return 12;
    case 'D':return 13;
    case 'E':return 14;
    case 'F':return 15;
    }
    return 0;
}

Try adding prototypes for the functions "makespace", "split", "convert" and "hexa" at the beginning of your code; I expect what you're seeing is the result of implicitly declared functions conflicting with the function definitions. Also, just to be sure, you're including stdio.h and string.h in your source file, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM