Question:
C: Passing array pointer to function, syntax question?
Thejon
2012-10-15 12:06:15 UTC
I'm new to C and need some pointer syntax intuition.

I get the following error, (on myntablet programming app):

Compile error
prog.c: In function main:
prog.c:27: error: static declaration of maxValue follows non-static declaration
prog.c:8: error: previous declaration of maxValue was here

Tried to add some comments in the code, some parts are not in use
My code for now is:

#include
#define SIZE 5

int main(void) {

// Header

int maxValue( int *,const int );
int minValue( int *,const int ); // not in use yet
double average( int[], const int ); // not in use yet


// Main
const int size= 5;

int array[SIZE] = { 6, 2, 13, 9, 5 };
int *arrayPtr = array;

int testMaxValue; // testMinValue, testAverage; Not in use yet

testMaxValue = maxValue( arrayPtr, size); // function call

printf( "The max value is %d", testMaxValue );


// Functions

int maxValue( int *arrayFunction, const int SIZEFUNK){

int i; // counter for-loop
int result = 0; // Stores the temp max value

for( i = 0; i < SIZEFUNK; i++ ){

if( arrayFunction[i] > result ){
result = arrayFunction[i];

} // end if


} // end for

return result;
} // end function maxValue

/* int minValue( int*,const int ){

} */

/* double average( int[], const int ){

} */

return 0;
}
Three answers:
Jonathan
2012-10-15 15:21:37 UTC
I thought you did remarkably well. As another mentioned, you nested a function inside of main(). But otherwise, it's not too bad. Allow me to re-arrange a few lines and submit to you something that may work better. I chose to extract your function declarations [a declaration is NOT the function itself, but just the line that 'talks' about what to expect when calling the function -- so here I just mean the three lines of code that do just that] from inside of main() and put them at module level. But that was a choice. You could place them inside, as you did beforehand. But that would limit their scope. So I put them outside of main(). With that comment in mind, here's the code:



#include

#define SIZE 5



int maxValue( int *,const int );

int minValue( int *,const int ); // not in use yet

double average( int[], const int ); // not in use yet



int main( void ) {

    const int size= 5;

    int array[SIZE] = { 6, 2, 13, 9, 5 };

    int *arrayPtr = array;

    int testMaxValue; // testMinValue, testAverage; Not in use yet



        testMaxValue = maxValue( arrayPtr, size); // function call

        printf( "The max value is %d", testMaxValue );



    return 0;

}



int maxValue( int *arrayFunction, const int SIZEFUNK ){

    int i; // counter for-loop

    int result = 0; // Stores the temp max value



        for( i = 0; i < SIZEFUNK; i++ ){

            if( arrayFunction[i] > result ){

                result = arrayFunction[i];

            } // end if

        } // end for



    return result;

} // end function maxValue



/* int minValue( int*,const int ){



} */



/* double average( int[], const int ){



} */
Laurence I
2012-10-15 12:12:27 UTC
your use of braces { } is a mess

they are supposed to enclose a code block.



definitions of functions belong at the outer levels, not

in the middle of statements(code blocks)



the compiler wont know what to make of this mess.

thus compiler errors will be so affected as to make it

impossible to work out whats wrong.
David
2012-10-15 12:29:50 UTC
You can't define functions in main.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...