Link to home
Start Free TrialLog in
Avatar of cdromes
cdromes

asked on

C Compiler/Linker Warnings Involving Math function & Pointer Arithmetic

Howdy

I'm receiving a couple warnings and an error that I can't figure out.  First here's the code:

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include<sys/types.h>
#include<sys/stat.h>
#include <mpc.h>

double *A;
double *Q;
double *R;
double *Norm;
int i,j,rows,cols,a,b,c,d,e,f,temp, mn;
FILE *file;

#define FNAME "test"

int main()
{      
      file = fopen(FNAME, "r");
      fscanf(file,"%d",&rows);
      fscanf(file,"%d",&cols);

      temp = 0.0;

      A = (double *)malloc(rows*cols*sizeof(double));
      Q = (double *)malloc(rows*cols*sizeof(double));
      R = (double *)malloc(rows*cols*sizeof(double));
      Norm = (double *)malloc(cols*sizeof(double));

      if (A==NULL || Q==NULL || R==NULL)
            fprintf(stderr, "malloc failed\n");
      
      mn = rows*cols;
      for (i=0;i<mn;i++)
            fscanf(file,"%lf",(A+i));
      
      fclose(file);
      for (a=0;a<cols;a++)
      {
            for (b=0;b<rows;b++)
                  Norm[a] += pow(*(A+a*rows+b),2.0);
            Norm[a] = sqrt(Norm[a]);
            
            for (c=0;c<rows;c++)
                  *(Q+a*rows+c) = (1/Norm[a]) * (*(A+a*rows+c));
            
            *(R+a*rows+a) = Norm[a];
            for (d=a;d<cols-1;d++)
            {
                  temp = 0.0;
                  for (e=0;e<rows;e++)
                        temp += *(A+(d+1)*rows+e) * *(Q+a*rows+e);
                  *(R+(d+1)*rows+a);

                  temp = 0.0;
                  for (f=0;f<rows;f++)
                        *(A+(d+1)*rows+f) = *(A+(d+1)*rows+f) - (*(Q+a*rows+f) * *(R+(d+1)*rows+a));
            }
      }

      exit(0);
}

It's a sequential QR factorization of a given matrix.  Here are the warnings/errors:

cc-1171 cc: WARNING File = QR.c, Line = 64
 The indicated expression has no effect.

                        *(R+(d+1)*rows+a);
                        ^

cc-1001 cc: WARNING File = QR.c, Line = 97
  The source file does not end with a new-line character.

  }
   ^

        cc -64 -mp  -o QR QR.o
ld64: ERROR   33 : Unresolved text symbol "pow" -- 1st referenced by QR.o.
        Use linker option -v to see when and which objects, archives and dsos are loaded.  

So, I can't figure out why the pointer arithmetic involved with the R would have no effect, or for that matter, what that means.  Similar confusion with the new-line character at the end of the source file.  Finally, the linker doesn't appear to be able to find the power function in math.h called 'pow', although it has no problem with 'sqrt'.  

Does anyone have any experience with the above in any form?

Jason
ASKER CERTIFIED SOLUTION
Avatar of ozo
ozo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of cdromes
cdromes

ASKER

ozo

Naturally, I caught this after I posted the question.  It's supposed to be

*(R+(d+1)*rows+a) = temp;

I've got the code working the way it needs to, however, I was required to take out the 'pow' and explicitly multiply the number by itself in order to square it.  I still don't understand why 'pow' doesn't work.

Also, I'm still getting the warning regarding the new line character in the source file.  Any ideas on either issue?
try adding a newline at the end of the program and compiling with -lm
Avatar of cdromes

ASKER

Added a new line and inserted the flag in the compile statement, still no luck.  No big deal, though.  Like I said...the program works like a champ.  I'll suck up the warning.

Thanks for your help.
The -lm is a gcc compiler flag to link to the math library.

Depending on the compiler you use, you might need to use a different way to link to the math library.