1

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

5
  • 1
    1 stoixeia[50][7]; is out of bound. 2. to return an array, you need to have a char **, at least. Commented Dec 30, 2015 at 13:53
  • 2
    BTW while(wrong=0) --> while(wrong==0) Commented Dec 30, 2015 at 13:55
  • @SouravGhosh what do you mean "is out of bound"? it is under the libraries Commented Dec 30, 2015 at 14:01
  • @BLUEPIXY oh yeah, now it doesnt crashes but somehow when i enter a username in usern it doesnt find anything and it always asks me to give another username Commented Dec 30, 2015 at 14:05
  • 1
    "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"." -- Yes. True. So, you do not need to return the array. Commented Dec 30, 2015 at 14:39

2 Answers 2

1

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;
    }
}
Sign up to request clarification or add additional context in comments.

16 Comments

compilers shows me: [Warning] return makes pointer from integer without a cast
type of stoixeia isn't char**.
look, i changed ` if (usern[i]==stoixeia[i][0])` as you said and the function works perfectly. I didnt put char **
Please use proper indentation and drop the & from scanf.
"/* you should initialize stoixeia to 0 via memcpy for example */" -- Why? stoizeia is a global array and it is automatically initialized to 0.
|
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.