How to handle invalid passed arguments in Ansi C -
i new c , come ground of newer languages (like java , c++) , i'm not sure how handle runtime errors incorrect arguments sent function.
for example want write function manipulate string (assuming valid int acceptable return value):
int foo (char s[]) { if ( strlen(s) < 1) // error .... .... return someint; }
how handle scenario in ansi c if want function stop immediately? in c++ or java throw exception caught caller.
you can use return value :
if( /* bad parameters */ ) return -1;
and :
int value = foo( ); if( value == -1 ) // error else // no error
or pass parameters :
int foo( char s[], int* value ) { if( /* error */ ) return 1;// error code 1 // ... *value = something; return 0; }
then when call function can verify if executed correctly :
if( foo( "something", &result ) ) { //ok } else { // not ok }
both of these method implies caller verify manually if there error
Comments
Post a Comment