开发者

the following is giving an error, please tell what is wrong with it [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 12 years ago.
#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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜