繁体   English   中英

在C中返回2d char数组

[英]Return 2d char array in C

我想将2D字符数组作为函数的输入(实际上我的2D数组是全局的,并且该函数没有输入),更改其中的值,然后返回另一个2D字符数组。

char stoixeia[50][7];
int main(int argc, char *argv[])

if (strcmp(answer, "Gift")==0) 
        {
            gift();
        } 


char gift ()
    {
     int i,j,m;
     int wrong=0;
     int k=0;
     char usern[50];
     while(wrong=0)
    {

     printf("Enter the username that you want to Gift:\n");
     scanf("%s", &usern);
     for (i=0; i<50; i++)
    {
     if (*usern==stoixeia[i][0])
     {
     wrong=1;
     k=i;
     }
     }

     }  
      m=strlen(usern);
      for(i=0; i<m; i++)
      {
        stoixeia[k][6]= stoixeia[k][6] + 10;
      }



      return stoixeia[50][7];

     }  

我的想法是,如果我将数组声明为全局数组,那么函数中的所有内容都会发生变化,并且数组将被“更新”。 编译器没有显示任何错误,但是当我运行程序并且我的answerGift ,.exe停止工作。 你能建议我什么吗? 谢谢

您的功能必须是这样的:

您无需返回值,因为您直接更改了全局变量。

void gift ()
{
    int i,j,m;
    int wrong=0;
    int k=0;
    char usern[50];
    while(wrong==0) /* replace = by ==*/
    {

        printf("Enter the username that you want to Gift:\n");
        scanf("%s", usern);
        for (i=0; i<50; i++)
        {
            if (usern[i]==stoixeia[i][0])
            {
                wrong=1;
                k=i;
            }
        }
    }  
    m=strlen(usern);
    for(i=0; i<m; i++)
    {
        stoixeia[k][6]= stoixeia[k][6] + 10;
    }
}

前面已经提到使用**gift()看到这里的一些更多的信息

暂无
暂无

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

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