static declaration in functions follows non-static declaration

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Ko
    New Member
    • Dec 2011
    • 1

    static declaration in functions follows non-static declaration

    #include <stdio.h>
    int main()
    {
    int n=20;
    int A[n], B[n], C[n];
    int addarrays (int add1, int add2, int counter);
    int i;
    for(i=0; i<n; i++)
    {
    printf("%d", addarrays(A[i], B[i], i));
    }

    int addarrays (int add1, int add2, int counter) //defining function
    {
    C[counter]= add1 + add2;
    printf("%d", C[counter]);
    return C[counter];
    }
    }

    I am trying to create a function that adds elements of arrays in a specific way (for uni) but i get the error that was in the title.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are defining the addarrays fnction inside main().

    Plus you are using main()varaibles directly insde the function.

    !) write your function ouside main()
    2) pass main() variables to the function.
    3) inside the function use the argumwnt variables only.

    Comment

    • lspxy
      New Member
      • Nov 2011
      • 12

      #3
      Code:
      #include <stdio.h>
      
      #define N 20
      
      int C[N];
      int addarrays (int add1, int add2, int counter); // declare function
      
      int main()
      {
          
      	int A[N], B[N];	// A[], B[] should be initialed before used.
      	int i;
      
      	for(i=0; i<n; i++)
      	{
      		printf("%d\t", addarrays(A[i], B[i], i));
      		printf("\n");
      	}
      }
      
      int addarrays (int add1, int add2, int counter) //defining function
      {
      	C[counter]= add1 + add2;
      
      	printf("%d", C[counter]);
      
      	return C[counter];
      }

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        This:

        Code:
        for(i=0; i<n; i++)
        etc...
        should be:
        Code:
        for(i=0; i<N; i++)
        etc...

        Comment

        • lspxy
          New Member
          • Nov 2011
          • 12

          #5
          yea, so careless. thanks.

          Comment

          Working...