error compiling Jeck Klein's getsafe function 
Author Message
 error compiling Jeck Klein's getsafe function

Dear All,

I used Jack Klein's getsafe() function in a short program I wrote, in order
to read in a file name. When compiling this program on an SGI (Mips C compiler)
I received the following error message:

A value of type "int" cannot be assigned to an entity of type "char *".

    if (np=strchr(buffer,'\n')) {

The program compiled and ran without problem on a Linux system. Why fo I get this
error and how do I fix it?

regards,

RB

*****
My whole program is below:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>

/* Jack Klein's getsafe function for reading character strings             */
/* http://www.*-*-*.com/ ~jackklein/ctips01.html                             */

char *getsafe(char *buffer, int count)
{
  char *result = buffer, *np;

  if ((buffer == NULL) || (count <1)) {
    result=NULL;;
  } else if (count == 1) {
    *result = '\0';
  } else if ((result=fgets(buffer,count,stdin)) != NULL) {
    if (np=strchr(buffer,'\n')) {
      *np='\0';
    }
  }

  return result;

Quote:
}

/* Error function from "Numerical Recipes in C", 2nd Ed.                   */

void nrerror(char error_text[])  
{
  void exit();

  fprintf(stderr,"Numerical Recipes run-time error...\n");
  fprintf(stderr,"%s\n",error_text);
  fprintf(stderr,"...now exiting to system...\n");
  exit(1);

Quote:
}

/* Chebyshev evaluation routine from "Numerical Recipes in C", 2nd Ed,     */
/* modified for the function described in Ren, Z. & Moffat, K. (1995)      */
/* J. Appl. Cryst, 28:461-481.                                             */

double chebev(float a, float b, float c[], int m, float x)
{
  void nrerror(char error_text[]);
  float d=0.0,dd=0.0,sv,y,y2;
  int j;

  if ((x-a)*(x-b) > 0.0) nrerror("x not in range in routine chebev");
    y2=2.0*(y=(2.0*x-a-b)/(b-a));
    for (j=m-1;j>=0;j--) {      /* c[0] of array is c_1 (i.e. for T_1 (x)) */
      sv=d;
      d=y2*d-dd+c[j];
      dd=sv;
    }
  return y*d-dd;                /* c_0 = 0 for LaueView Chebyshev function */

Quote:
}

int main(int argc, char *argv[])
{
  FILE     *f_in, *f_out;
  char    c,file_in[20],file_out[20];
  int      i, nl, np;
  float    zero,lmin,lmax,lref,lint,*ccf;
  double  fx,fr,x;

  nl=np=lmin=lmax=lint=zero=fx=fr=x=0;

  printf("\n reconstruct the energy spectrum for a Laue dataset using\n");
  printf(" the Chebyshev polynomial coefficients from LaueView.\n\n\n");

  if (argc == 1) {           /* no filenames specified on the command line */
    printf("input filename: ");
    f_in=fopen(getsafe(file_in,20),"r");
    printf("wavelength range (min max): ");
    scanf("%f%f",&lmin,&lmax);
    printf("reference wavelength: ");
    scanf("%f",&lref);
    printf("wavelength interval: ");
    scanf("%f",&lint);
    c=getchar();
    printf("output filename: ");
    f_out=fopen(getsafe(file_out,20),"w");
    printf("output to %s\n",file_out);
  } else if (argc == 2) {  /* input filename specified on the command line */
    printf("opening %s ...\n",argv[1]);
    f_in=fopen(argv[1],"r");
    printf("wavelength range (min max): ");
    scanf("%f%f",&lmin,&lmax);
    printf("reference wavelength: ");
    scanf("%f",&lref);
    printf("wavelength interval: ");
    scanf("%f",&lint);
    c=getchar();
    printf("output filename: ");
    f_out=fopen(getsafe(file_out,20),"w");
    printf("output to %s\n",file_out);
   } else if (argc == 3) {   /* input/output filenames on the command line */
    printf("opening %s ...\n",argv[1]);
    f_in=fopen(argv[1],"r");
    printf("output to %s\n",argv[2]);
    f_out=fopen(argv[2],"w");
    printf("wavelength range (min max): ");
    scanf("%f%f",&lmin,&lmax);
    printf("reference wavelength: ");
    scanf("%f",&lref);
    printf("wavelength interval: ");
    scanf("%f",&lint);    
    c=getchar();
  } else {                               /* some error on the command line */
    nrerror("too many arguments!\n");
  }

  printf("Energy spectrum from %-4.2f to %-4.2f Angstrom ",lmin,lmax);
  printf("in %-4.2f Angstrom intervals.\n",lint);

  do {            /* parse input file for number of Chebyshev coefficients */
    fscanf(f_in," %i %*f",&nl);
  } while ((c=fgetc(f_in)) != EOF);

  printf("number of Chebyshev coefficients: %i\n", nl);

  ccf=(float *)malloc((nl)*sizeof(float));     /* create coefficient array */
  for (i=0;i<nl;i++) {
    ccf[i]=0;
  }
  rewind(f_in);
  for (i=0;i<nl;i++) {                      /* read coefficients from file */
    fscanf(f_in,"%*i %f\n",&ccf[i]);
    printf("%f\n", ccf[i]);
  }

  fclose(f_in);              /* Close the input file after reading from it */

  np=(lmax-lmin)/lint;     /* Calculate the value of the function for lref */
  fr=chebev(lmin,lmax,ccf,nl,lref);     /* and the number of datapoints np */

  if ((lmin+np*lint) == lmax ) {  /* lmax-lmin = lmin+np*lmax, np-1 points */
    fprintf(f_out,"%8.4f\t%f\n",lmin,zero);
    for (i=1;i<(np-1);i++) {
      fx=chebev(lmin,lmax,ccf,nl,(lmin+i*lint));
      x=fx-fr;
      fprintf(f_out,"%8.4f\t%f\n",(lmin+i*lint),exp(-x));
    }
    fprintf(f_out,"%8.4f\t%f\n",lmax,zero);
  } else {                          /* lmax-lmin > lmin+np*lmax, np points */
    fprintf(f_out,"%8.4f\t%f\n",lmin,zero);
    for (i=1;i<np;i++) {
      fx=chebev(lmin,lmax,ccf,nl,(lmin+i*lint));
      x=fx-fr;
      fprintf(f_out,"%8.4f\t%f\n",(lmin+i*lint),exp(-x));
    }
    fprintf(f_out,"%8.4f\t%f\n",lmax,zero);
  }

  fclose(f_out);              /* Close the output file after writing to it */

  return 0;

Quote:
}



Sun, 20 Mar 2005 05:10:43 GMT  
 error compiling Jeck Klein's getsafe function

Quote:

> Dear All,

> I used Jack Klein's getsafe() function in a short program I wrote, in order
> to read in a file name. When compiling this program on an SGI (Mips C compiler)
> I received the following error message:

> A value of type "int" cannot be assigned to an entity of type "char *".

>     if (np=strchr(buffer,'\n')) {

> The program compiled and ran without problem on a Linux system. Why fo I get this
> error and how do I fix it?

> regards,

> RB

> *****
> My whole program is below:

> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>

#include <string.h> /* for strchr */
That oughta do it!

HTH,
--ag

- Show quoted text -

Quote:

> /* Jack Klein's getsafe function for reading character strings             */
> /* http://home.att.net/~jackklein/ctips01.html                             */

> char *getsafe(char *buffer, int count)
> {
>   char *result = buffer, *np;

>   if ((buffer == NULL) || (count <1)) {
>     result=NULL;;
>   } else if (count == 1) {
>     *result = '\0';
>   } else if ((result=fgets(buffer,count,stdin)) != NULL) {
>     if (np=strchr(buffer,'\n')) {
>       *np='\0';
>     }
>   }

>   return result;
> }

> /* Error function from "Numerical Recipes in C", 2nd Ed.                   */

> void nrerror(char error_text[])  
> {
>   void exit();

>   fprintf(stderr,"Numerical Recipes run-time error...\n");
>   fprintf(stderr,"%s\n",error_text);
>   fprintf(stderr,"...now exiting to system...\n");
>   exit(1);
> }

> /* Chebyshev evaluation routine from "Numerical Recipes in C", 2nd Ed,     */
> /* modified for the function described in Ren, Z. & Moffat, K. (1995)      */
> /* J. Appl. Cryst, 28:461-481.                                             */

> double chebev(float a, float b, float c[], int m, float x)
> {
>   void nrerror(char error_text[]);
>   float d=0.0,dd=0.0,sv,y,y2;
>   int j;

>   if ((x-a)*(x-b) > 0.0) nrerror("x not in range in routine chebev");
>     y2=2.0*(y=(2.0*x-a-b)/(b-a));
>     for (j=m-1;j>=0;j--) {      /* c[0] of array is c_1 (i.e. for T_1 (x)) */
>       sv=d;
>       d=y2*d-dd+c[j];
>       dd=sv;
>     }
>   return y*d-dd;                /* c_0 = 0 for LaueView Chebyshev function */
> }

> int main(int argc, char *argv[])
> {
>   FILE     *f_in, *f_out;
>   char    c,file_in[20],file_out[20];
>   int      i, nl, np;
>   float    zero,lmin,lmax,lref,lint,*ccf;
>   double  fx,fr,x;

>   nl=np=lmin=lmax=lint=zero=fx=fr=x=0;

>   printf("\n reconstruct the energy spectrum for a Laue dataset using\n");
>   printf(" the Chebyshev polynomial coefficients from LaueView.\n\n\n");

>   if (argc == 1) {           /* no filenames specified on the command line */
>     printf("input filename: ");
>     f_in=fopen(getsafe(file_in,20),"r");
>     printf("wavelength range (min max): ");
>     scanf("%f%f",&lmin,&lmax);
>     printf("reference wavelength: ");
>     scanf("%f",&lref);
>     printf("wavelength interval: ");
>     scanf("%f",&lint);
>     c=getchar();
>     printf("output filename: ");
>     f_out=fopen(getsafe(file_out,20),"w");
>     printf("output to %s\n",file_out);
>   } else if (argc == 2) {  /* input filename specified on the command line */
>     printf("opening %s ...\n",argv[1]);
>     f_in=fopen(argv[1],"r");
>     printf("wavelength range (min max): ");
>     scanf("%f%f",&lmin,&lmax);
>     printf("reference wavelength: ");
>     scanf("%f",&lref);
>     printf("wavelength interval: ");
>     scanf("%f",&lint);
>     c=getchar();
>     printf("output filename: ");
>     f_out=fopen(getsafe(file_out,20),"w");
>     printf("output to %s\n",file_out);
>    } else if (argc == 3) {   /* input/output filenames on the command line */
>     printf("opening %s ...\n",argv[1]);
>     f_in=fopen(argv[1],"r");
>     printf("output to %s\n",argv[2]);
>     f_out=fopen(argv[2],"w");
>     printf("wavelength range (min max): ");
>     scanf("%f%f",&lmin,&lmax);
>     printf("reference wavelength: ");
>     scanf("%f",&lref);
>     printf("wavelength interval: ");
>     scanf("%f",&lint);    
>     c=getchar();
>   } else {                               /* some error on the command line */
>     nrerror("too many arguments!\n");
>   }

>   printf("Energy spectrum from %-4.2f to %-4.2f Angstrom ",lmin,lmax);
>   printf("in %-4.2f Angstrom intervals.\n",lint);

>   do {            /* parse input file for number of Chebyshev coefficients */
>     fscanf(f_in," %i %*f",&nl);
>   } while ((c=fgetc(f_in)) != EOF);

>   printf("number of Chebyshev coefficients: %i\n", nl);

>   ccf=(float *)malloc((nl)*sizeof(float));     /* create coefficient array */
>   for (i=0;i<nl;i++) {
>     ccf[i]=0;
>   }
>   rewind(f_in);
>   for (i=0;i<nl;i++) {                      /* read coefficients from file */
>     fscanf(f_in,"%*i %f\n",&ccf[i]);
>     printf("%f\n", ccf[i]);
>   }

>   fclose(f_in);              /* Close the input file after reading from it */

>   np=(lmax-lmin)/lint;     /* Calculate the value of the function for lref */
>   fr=chebev(lmin,lmax,ccf,nl,lref);     /* and the number of datapoints np */

>   if ((lmin+np*lint) == lmax ) {  /* lmax-lmin = lmin+np*lmax, np-1 points */
>     fprintf(f_out,"%8.4f\t%f\n",lmin,zero);
>     for (i=1;i<(np-1);i++) {
>       fx=chebev(lmin,lmax,ccf,nl,(lmin+i*lint));
>       x=fx-fr;
>       fprintf(f_out,"%8.4f\t%f\n",(lmin+i*lint),exp(-x));
>     }
>     fprintf(f_out,"%8.4f\t%f\n",lmax,zero);
>   } else {                          /* lmax-lmin > lmin+np*lmax, np points */
>     fprintf(f_out,"%8.4f\t%f\n",lmin,zero);
>     for (i=1;i<np;i++) {
>       fx=chebev(lmin,lmax,ccf,nl,(lmin+i*lint));
>       x=fx-fr;
>       fprintf(f_out,"%8.4f\t%f\n",(lmin+i*lint),exp(-x));
>     }
>     fprintf(f_out,"%8.4f\t%f\n",lmax,zero);
>   }

>   fclose(f_out);              /* Close the output file after writing to it */

>   return 0;
> }

--
Artie Gold -- Austin, Texas


Sun, 20 Mar 2005 05:17:47 GMT  
 error compiling Jeck Klein's getsafe function
"Richard Baxter" posted:

Quote:

> I used Jack Klein's getsafe() function in a short program I wrote, in order
> to read in a file name. When compiling this program on an SGI (Mips C
compiler)
> I received the following error message:

> A value of type "int" cannot be assigned to an entity of type "char *".

>     if (np=strchr(buffer,'\n')) {

> The program compiled and ran without problem on a Linux system. Why fo I get
this
> error and how do I fix it?

You do not have a proper prototype for strchr in scope. You need to include
string.h as well.

Quote:
> #include <stdio.h>
> #include <stdlib.h>
> #include <math.h>

[snip]


Sun, 20 Mar 2005 05:24:32 GMT  
 error compiling Jeck Klein's getsafe function

wrote in comp.lang.c:

Quote:
> Dear All,

> I used Jack Klein's getsafe() function in a short program I wrote, in order
> to read in a file name. When compiling this program on an SGI (Mips C compiler)
> I received the following error message:

> A value of type "int" cannot be assigned to an entity of type "char *".

>     if (np=strchr(buffer,'\n')) {

You had me worried for a moment there, I had to rush and check the web
page myself.  Here is how it appears:

#include <stdio.h>
#include <string.h>
char *getsafe(char *buffer, int count)
{
   char *result = buffer, *np;
   if ((buffer == NULL) || (count < 1))
      result = NULL;
   else if (count == 1)
      *result = '\0';
   else if ((result = fgets(buffer, count, stdin)) != NULL)
      if (np = strchr(buffer, '\n'))
         *np = '\0';
   return result;

Quote:
}

So as others have pointed out, you need to include <string.h>.  I did,
but you removed it for some reason.  Easily fixed.  Still, it's nice
to see people getting some use from the code.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq



Sun, 20 Mar 2005 10:00:07 GMT  
 error compiling Jeck Klein's getsafe function

Quote:
>I used Jack Klein's getsafe() function in a short program I wrote, in order
>to read in a file name. When compiling this program on an SGI (Mips C compiler)
>I received the following error message:

>A value of type "int" cannot be assigned to an entity of type "char *".

>    if (np=strchr(buffer,'\n')) {

>The program compiled and ran without problem on a Linux system.

I don't think so.  This code requires a diagnostic (in the context of
your actual program) so you've got a warning on the Linux system, too.
You foolishly decided to ignore it, though.  Why?!?

Quote:
>Why fo I get this error and how do I fix it?

The compiler diagnostic was very explicit.  Have you provided a
declaration for strchr() anywhere in your program?  If not, what is the
implicit declaration the compiler will use for it?

This is very basic stuff.  You really need to learn C before trying to
pick other people's code and use it.  And, of course, you've shot
yourself in the foot by removing a line from Jack Klein's code.  Don't
do such foolish things unless you *really* know what you're doing.

Dan
--
Dan Pop
DESY Zeuthen, RZ group



Sun, 20 Mar 2005 19:24:21 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. Newbie - Compile error (Cannot open include file: 'xmldom.idl': No such file or directory)

2. compile error ( 'struct' redefinition )

3. Compile Error : 'class' type redefinition

4. Can't compile - Error C1010 - Precompiled header?

5. HELP I'm having compile errors.

6. don't understand compile-time error

7. Help! NMAKE won't display compile errors

8. Compilation error of 'const' functions

9. Compilation Error - when modifying 'const' functions

10. Thrdcore's compile error

11. error C2668: 'sqrt': ambiguous call to overloaded function

12. Can't compile any project that uses API (link errors)

 

 
Powered by phpBB® Forum Software