Oracle FAQ | Your Portal to the Oracle Knowledge Grid |
![]() |
![]() |
Home -> Community -> Usenet -> c.d.o.server -> Re: Compilation problems??
Groovy hepcat Programming was jivin' on Mon, 11 Aug 1997 10:12:13
-0500 in comp.lang.c.
Compilation problems??'s a cool scene! Dig it!
>#include "stdio.h"
First, stdio.h should be enclosed in angle brackets, not quotes. It is a standard header file, not a header you've written. Also, I would include stdlib.h instead. In fact, it couldn't hurt to include them both: better safe than sorry, eh.
>int main()
>{
> char *ls_returnvalue;
>
> ls_returnvalue = "";
The above line is not necissary. The call to checkvalue below sets the value of ls_returnvalue, so you don't have to initialise it.
> ls_returnvalue = checkvalue (); /* line 86 */
> printf("ls_returnvalue is: %s\n", ls_returnvalue);
> exit(0);
A return statement would have been better here, instead of exit().
return 0;
>} /********** end main ************/
>char *checkvalue() /*line 96 */
>{
> char *ls_getvalue;
> int li_valid;
>
> ls_getvalue = (char*) malloc(80);
Check the return from malloc() to make sure nothing went wrong:
if(NULL == ls_getvalue) { printf("Allocation error!\n"); return NULL; }
> scanf("%s\n", ls_getvalue);
>
> li_valid = 0;
>
> if (li_valid == 0)
> {
> printf("ls_getvalue is: %s\n", ls_getvalue);
> free (ls_getvalue);
> ls_getvalue = 0;
Keep it portable. Use NULL instead of 0 here.
ls_getvalue = NULL;
> } /* end if */
I don't see a return statement here. I'm surprised you didn't get a warning about this. I assume this function is supposed to return ls_getvalue.
return ls_getvalue;
>} /************ end checkvalue ****************/
>"char1.c", line 86.21: 1506-068 (S) Operation between types "char*" and "int" is not
>allowed.
>"char1.c", line 96.7: 1506-343 (S) Redeclaration of checkvalue differs from previous
>declaration on line 86 of "char1.c".
>"char1.c", line 96.7: 1506-050 (I) Return type "char*" in redeclaration is not
>compatible with the previous return type "int".
You have not prototyped the function checkvalue() before it's used in main(), so the compiler assumes it returns an int. Here's what you should have after fixing the points I've mentioned:
#include <stdlib.h>
#include <stdio.h>
char *checkvalue(void); /* prototype the function */
int main(void)
{
char *ls_returnvalue;
ls_returnvalue = checkvalue ();
printf("ls_returnvalue is: %s\n", ls_returnvalue);
return 0;
}
char *checkvalue(void)
{
char *ls_getvalue;
int li_valid;
ls_getvalue = (char*) malloc(80);
if(NULL == ls_getvalue)
{
printf("Allocation error!\n");
return NULL;
}
scanf("%s\n", ls_getvalue);
li_valid = 0;
if (li_valid == 0)
{
printf("ls_getvalue is: %s\n", ls_getvalue);
free (ls_getvalue);
ls_getvalue = NULL;
}
return ls_getvalue;
}
![]() |
![]() |