简体   繁体   中英

Return 2d char array in C

I want to take a 2D char array as an input in a function (actually my 2D array is global and the function doesn't have inputs) , change the values in it and then return another 2D char array.

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];

     }  

My thought was that if i declare my array as a global one everything would change in my functions and the array will get "updated". The compiler doesn't show any errors but when I run the programm and my answer is Gift the .exe stops working. Can you suggest me anything? Thank you

your function must be like this:

You don't need to return a value because you change directly the global variable.

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()看到这里的一些更多的信息

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