C中传递一个字符串函数,则返回一个字符串 [英] c pass a string to function then return a string

查看:129
本文介绍了C中传递一个字符串函数,则返回一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

探索互联网之后,我写了下面的脚本,但是仍然有错误,有谁知道什么错误?(与// ERR行)

  typedef结构name_value_set {
    字符名称[250];
    char值[250];
} nv_set;字符* getInput2(字符*参数){
    字符* my_data = 0;
    INT DATA_LEN;
    字符* tmp_ptr,* tmp目录;
    INT I;
    nv_set * NV;    DATA_LEN =的atoi(GETENV(CONTENT_LENGTH));
    炭场[DATA_LEN]    my_data =(字符*)malloc的(的sizeof(字符)*(DATA_LEN + 1));
    FREAD(my_data,1,DATA_LEN,标准输入);    I = 0;
    而(my_data [0]!='\\ 0'){
     TMP =拆分(my_data,'='); // 呃
     makespace(TMP);
     TMP =转换(TMP); // 呃
     的strcpy(NV [I] .name和TMP); //
     TMP =拆分(my_data,'和;'); //呃
     makespace(TMP); //
     TMP =转换(TMP); // 呃
     的strcpy(NV [I] .value的,TMP); //
     我++;
    }
    一世 - ; //    INT J = 0;
    为(J = 0; J< I; J ++){
    如果(NV [J]。名称==参数){
        返回NV [J]。价值;
    }
}
无效makespace(字符* S)
{
    INT I,LEN;
    LEN = strlen的(S);    对于(i = 0; I< LEN,我++){
      如果(S [I] =='+')
        S [i] ='';
    }
}字符*分(字符* S,字符停止)
{
    字符*的数据;
    字符* tmp目录;
    INT I,LEN,J;    LEN = strlen的(S);
    TMP = S;
    数据=(字符*)malloc的(的sizeof(字符)*(LEN + 1));    对于(i = 0; I< LEN,我++){
       如果(S [I]!=停止)
         数据[i] = S [I] //
      其他{
        我+ = 1; //
          打破;
       }
    }
    数据[I] ='\\ 0';
    为(J = I; J< LEN; J ++)
      S [J-I] = TMP [J]。
      S [LEN-I] ='\\ 0';
     返回的数据;
}
字符*转换(字符* S)
{
    INT X,Y,LEN;
    字符*的数据;    LEN = strlen的(S);
    数据=(字符*)malloc的(的sizeof(字符)*(LEN + 1));
    Y = 0;
    为(X = 0; X&下; len个; X ++){
    如果(S [X]!='%'){
        数据[Y] = S [X]; //
          ÿ++;
      }
    其他{        数据[Y] =(char)的(16 *六(S [X + 1])+六(S [X + 2));
        ÿ++;
        X = X + 2;
      }
    }
     数据[Y] ='\\ 0';
     返回的数据;
}
INT六(焦三)
{
    开关(三){
    情况下0:返回0;
    案1:返回1;
    案2:回归2;
    案3:回归3;
    案4:回归4;
    案5:回归5;
    案6:回归6;
    案7:回归7;
    情况下8:回归8;
    案例'9':9的回报;
    案例'A':回归10;
    案例'B':回归11;
    情况下C:返回12;
    案D:回归13;
    案例'E':回归14;
    案例'F':回归15;
    }
    返回0;
}


解决方案

尝试添加原型的功能makespace,分裂,转换和六在你的code的开始;我希望你看到的是隐式声明的函数与函数定义发生冲突的结果。此外,只是要确定,你包括你的源文件stdio.h和string.h中,对吧?

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?

这篇关于C中传递一个字符串函数,则返回一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆