-1

Can anyone tell me whats the problem with this code and how Can I fix it? (Compile error) thank you! :)

#include <stdio.h>

int[] initArray(int arr[]);
void printArray(int arr[]);

 int main()
 {
      int myGrades[2];
      myGrades = initArray(myGrades);
      printArray(myGrades);
      return (0);
 }

 int[] initArray(int arr[])
 {
      arr[0] = 78;
      arr[1] = 86;
      return arr; 
 }

 void printArray(int arr[])
 {
     printf("My grade in English is: %d!\n",arr[0]);
     printf("My grade in History is %d! What an achievement!\n",arr[1]);
 }
Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97
NoNameP
  • 3
  • 5
  • Are you sure you need this to write `int[] initArray(int arr[]`? – Michi Jan 09 '16 at 12:24
  • No,but still have the problem. – NoNameP Jan 09 '16 at 12:26
  • @NoNameP [Try this](http://ideone.com/fZqfSN) – Michi Jan 09 '16 at 12:31
  • it's long time i didn't had any thing to do with C++. but i remember once i wanted to use it in VS, and some specific project type, i had to use the "using std" or something similar import, and it had it's own order ...here found it: `using namespace std` i'm not sure what kind of error you receive that i be able to help you, but take a look at this one – Hassan Faghihi Jan 09 '16 at 12:33
  • @deadManN What has [tag:c++] to do with this? – Iharob Al Asimi Jan 09 '16 at 12:34
  • @deadManN The question was tagged `C` and not `C++` – Michi Jan 09 '16 at 12:34
  • 3
    @deadManN: that's terrible advice even if the question was about C++. If that's all you remember, better forget that too. – Mat Jan 09 '16 at 12:36
  • Is there another way without using Pointers? never knew Pointers and I do not understand it – NoNameP Jan 09 '16 at 12:40
  • If you don't understand pointers or you never learned them, Why do you still learn the Language ? – Michi Jan 09 '16 at 12:45
  • [Try This](http://ideone.com/NWgUkB) and also [This](http://ideone.com/1zKM6U) and read iharob Answer – Michi Jan 09 '16 at 12:47
  • @Mat :D sorry then, cause i didn't see any thing wrong with his program, (the way i remember it), again sorry – Hassan Faghihi Jan 09 '16 at 14:06

1 Answers1

1
  1. You cannot assign to arrays in , so this

    int myGrades[2];
    myGrades = initArray(myGrades);
    

    is invalid, you simply cannot assign to an array but passing the array as a parameter should modify it inside the function as a pointer to the first element of the array is really passed, see the next point to understand why.

    To fix this part, just remove the assignment

    initArray(myGrades);
    

    is enough.

  2. You cannot define a function like this

    int[] initArray(int arr[])
    {
         arr[0] = 78;
         arr[1] = 86;
         return arr; 
    }
    

Quoting this document, the C11 Standard Draft

6.7.6.3 Function declarators (including prototypes)

  1. A function declarator shall not specify a return type that is a function type or an array type.

The [] syntax cannot be used for a function return type, also as a function parameter it will become a int poitner anyway so using int arr[] can be misleading, for example you can try to apply the sizeof operator to it, and it will give you the size of a pointer.

Change it to

    void initArray(int *arr)
    {
         arr[0] = 78;
         arr[1] = 86;
    }

and it should compile.

Although, the returned pointer is only valid in the scope where you defined arr, in your program it's valid in main() so pretty much everywhere if you pass arr as a parameter to any function it will be fine. The point is that you don't need to return it at all since the original array (the one you passed to the function) is altered in place.

Iharob Al Asimi
  • 52,653
  • 6
  • 59
  • 97