Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need help in understand a function declaration and definition 3

Status
Not open for further replies.

Rmcta

Technical User
Nov 1, 2002
478
US
I am getting confused on how to correctly create a function declaration (or prototype) and a function definition when using arrays.

example: If I have a function called myfunction and I wish to pass array_A[5] and array_B[3][3] would this be correct?

FUNCTION DECLARATION:

void myfunction(int array[], int array2[][3]);

FUNCTION DEFINITION:

void myfunction(int array[], int array2[][3]);
{
...
....
}


My function declaration and definition look exactly the same. Is that correct?

Something is wrong here but I can't quite figure out what.

Please guide me!




 
Don't bother.

Just declare it to accept an [tt]int*[/tt] and and [tt]int**[/tt] and two [tt]size_t[/tt] to indicate their lengths.

The way you're trying to do it limits you to using just the one size array, thus lessening its applicability to any situation.

 
You mean like the following:

FUNCTION DECLARATION:

void myfunction(int array[], int array2[][X]);

FUNCTION DEFINITION:

void myfunction(int array[], int array2[][X]);
{
...
....
}
 
No, like this:

Code:
void myfunction( int*  array1, size_t size1,
                 int** array2, size_t size2 );

void myfunction( int*  array1, size_t size1,
                 int** array2, size_t size2 )
{
    for ( size_t i = 0; i < size1; ++i )
        ...

    for ( size_t i = 0; i < size2; ++i )
        ...
}
 
And if your two arrays are always gonna be the same length (or your function will assume they are), just use the one size_t argument, of course.

If you're really curious about the syntax for the other way, I think the rule is something like "You need all the dimensions except the last one," and you're trying to put all the dimensions except the first one.

i.e.
Code:
void myfunction(int array[], int array2[3][]);
might work better... but put your correct dimension, of course.

I don't bother to remember how it goes since it has such limited use and really isn't worth it.
 
Hi,

I don't think the definition should have a ";"(See HERE).



FUNCTION DECLARATION:

void myfunction(int array[], int array2[][3]);

FUNCTION DEFINITION:

void myfunction(int array[], int array2[][3]); /* HERE */
{
...
....
}

Hope this helps.

Pappy
You learn something new everyday.
 
If you have
[tt]int array[Y][X];[/tt]

Then the correct prototype is any of the following
[tt]
void func ( int array[Y][X] ); /* spot the copy/paste idea here */
void func ( int array[][X] ); /* first dimension is optional */
void func ( int (*array)[X] ); /* alternative syntax, still the same to the compiler */
[/tt]

Which you would call as follows
[tt]func( array );[/tt]

You cannot pass a true 2D array such as above to a function expecting an int** parameter.

--
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top