the following is giving an error, please tell what is wrong with it [closed]
#include<stdio.h>
main( )
{ int num[ ] = {24, 34, 12, 44, 56, 17};
dislpay(&num[0],6);
}
display ( int *j, int n )
{
int i ;
for ( i = 0 ; i <= n - 1 ; i++ )
{
printf ( "\nelement = %d", *j ) ;
j++ ; /* increment pointer to point to ne开发者_运维知识库xt element */
}
}
The language is c, windows vista using visual c++ 2005 express.
The correct code should be something like :
#include<stdio.h>
void display(int*, int); //declaration of your function
int main( ) //return type of main should be int
{
int num[ ] = {24, 34, 12, 44, 56, 17};
display(&num[0],6); //Correct the spelling mistake
}
void display ( int *j, int n ) //specify a return type
{
int i ;
for ( i = 0 ; i <= n - 1 ; i++ )
{
printf ( "\nelement = %d", j[i] ) ;
}
}
Typo line 4, dislpay -> display?
The reason you're getting an error, besides your typo, is that in C, you cannot refer to a variable or function before its declaration. Thus you can fix your code by either moving the display function to before main, or as Prasoon did, add a declaration above main.
Technically you can leave out return types as C assumes int (at least ANSI C89 does), but it's not a good idea. It's good practice to always specify return type to improve readability and avoid tricky type-mismatch bugs (especially since C does a lot of implicit casting).
精彩评论