sniperalex117
sniperalex117

Reputation: 79

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

Upvotes: 1

Views: 567

Answers (2)

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

Upvotes: 1

James Kirsch
James Kirsch

Reputation: 72

As already mentioned use **gift() see here for some more information

Upvotes: 1

Related Questions