简体   繁体   English

c将字符串传递给函数,然后返回字符串

[英]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) 浏览互联网后,我编写了以下脚本,但是仍然出现错误,有人知道错误是什么吗?(​​带有// 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; 尝试在代码的开头添加“ makespace”,“ split”,“ convert”和“ hexa”函数的原型; 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? 另外,请确保在源文件中包含stdio.h和string.h,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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