Link to home
Start Free TrialLog in
Avatar of adamport
adamport

asked on

C Compiler Error program

Hi, I have a C program using functions and pointers that wont compile, it gives the error:

line(21) - warning C4028: formal parameter 2 different from declaration

My code is posted here below, any ideas on whats wrong?

Thanks
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
 
int trig(int, double*);
double rad_deg(int, double);
 
main()
{
	double solutions[19];
	int i;
 
	trig(0, &solutions[19]);
 
	for(i=0; i<18; i++)
	{
		printf("\n Angle: %lf",solutions[i]);
	}
}
 
int trig(int type, double *solutions[19])
{
  //Function code here
}
 
double rad_deg(int type, double angle)
{
  //Function code here	
}

Open in new window

Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Try changing trig function to this...

int trig(int type, double *solutions)

As you are just passing in the address of one element in the call

trig(0, &solutions[19]);

Fixed
// testr.cpp : Defines the entry point for the console application.
//
 
#include "stdafx.h"
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
 
int trig(int, double*);
double rad_deg(int, double);
 
int main()
{
	double solutions[19];
	int i;
 
	trig(0, &solutions[19]);
 
	for(i=0; i<18; i++)
	{
		printf("\n Angle: %lf",solutions[i]);
	}
}
 
int trig(int type, double *solutions)
{
	//Function code here
	return 0;
}
 
double rad_deg(int type, double angle)
{
	//Function code here	
	return 0;
}

Open in new window

When you do &solutions[19] you are pulling out a single double element from solutions (element 19) and then taking its address, the type of which is double * (does that make sense)?
I think this is a better way to fix the code :


#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
 
int trig(int type, double solutions[19]);   /* <--- fixed the prototypes */
double rad_deg(int type, double angle);
 
main()
{
        double solutions[19];
        int i;
 
        trig(0, solutions);     /* just pass the array */
 
        for(i=0; i<19; i++)     /* upper limit < 19 instead of < 18 */
        {
                printf("\n Angle: %f",solutions[i]);  /* %f instead of %lf */
        }
}
 
int trig(int type, double solutions[19])
{
  //Function code here
}
 
double rad_deg(int type, double angle)
{
  //Function code here  
}

Open in new window

btw, // is not a valid comment in C !!

And your main should return int :

        int main(void) {
            /* your code */
            return 0;
        }
>> btw, // is not a valid comment in C !!
I think this has now been adopted by the C99 standard has it not?

>> And your main should return int :
C++ compilers, unfortunately, ignore this (so I didn't spot it) as it is valid C++ code to not return anything from int main() !!!
>> >> btw, // is not a valid comment in C !!
>> I think this has now been adopted by the C99 standard has it not?

But has the C99 standard been adopted by all compilers ? ;)

Ok, I'm probably living in the past heh :)
>> But has the C99 standard been adopted by all compilers ? ;)
Probably note, after all they have only had 8 years to do this! :)

Well done for picking up those other issues. I'm a bit pushed for time at work so I didn't really have time to go through the code properly so I just concentrated on identifying the reason for the compilation failure.
s/note/not
>> >> But has the C99 standard been adopted by all compilers ? ;)
>> Probably note, after all they have only had 8 years to do this! :)

At least one of the compilers I'm currently using does not accept it (Mingw gcc). It complains with these warnings :

1 wrong.c [Warning] return type defaults to `int'
   wrong.c In function `main':
1 wrong.c [Warning] control reaches end of non-void function
   At top level:
5 wrong.c parse error before '/' token

for this code :

main() {
 
}
 
// a comment

Open in new window

>> At least one of the compilers I'm currently using does not accept it (Mingw gcc).
Then is does not support C99 fully :)
Avatar of adamport
adamport

ASKER

Both those versions of code work but what I'm trying to do is create an array inside a function and return it with that function to be used in main()

The complie error given now is:

line(31) - error C2100: illegal indirection

Here is the full code below:
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
 
int trig(int, double *);
double rad_deg(int, double);
 
main()
{
	double solutions[19];
	int i;
 
	trig(0, &solutions[19]);
 
	for(i=0; i<18; i++)
	{
		printf("\n Angle: %lf",solutions[i]);
	}
}
 
int trig(int type, double *solutions)
{
	double angle;
	int i;
 
	if(type==0) //Sine
	{	angle = rad_deg(1,0.0);
		for(i=0; i<19; i++)
		{	
			/* Add to the solutions array and return it to be output in main() */
			*solutions[i] = sin(angle);
			angle = rad_deg(1,(angle+10.0));
		}
	}
}
 
double rad_deg(int type, double angle)
{
	//Type = 0 Convert from degrees to radians
	//Type = 1 Convert from radians to degrees
	
	if(type==0) return angle*(M_PI/180);
	else return angle*(180/M_PI);
}

Open in new window

Is tis what you mean?

main()
{
  double solutions[19];
  int i;

  trig(0, solutions);

  for(i=0; i<18; i++)
    {
      printf("\n Angle: %lf",solutions[i]);
    }
}

int trig(int type, double *solutions)
{
  double angle;
  int i;

  if(type==0) //Sine
    {       angle = rad_deg(1,0.0);
    for(i=0; i<19; i++)
      {
        /* Add to the solutions array and return it to be output in main() */
        solutions[i] = sin(angle);
        angle = rad_deg(1,(angle+10.0));
      }
    }
}
And perhaps you also meant
for(i=0; i<19; i++)
      {
        /* Add to the solutions array and return it to be output in main() */
        solutions[i] = sin(rad_deg(0,angle));
        angle = (angle+10.0);
      }
    }
I think you are probably after something like this...
#include "stdafx.h"
 
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
 
int trig(int, double **);
double rad_deg(int, double);
 
int main()
{
	double solutions[19];
	double *pSolutions = solutions; /* use pointer to make life easier */
	int i;
 
	trig(0, &pSolutions);
 
	for(i=0; i<18; i++)
	{
		printf("\n Angle: %lf",solutions[i]);
	}
 
	return 0;
}
 
int trig(int type, double **solutions)
{
	double angle;
	int i;
 
	if(type==0) //Sine
	{	angle = rad_deg(1,0.0);
		for(i=0; i<19; i++)
		{	
			/* Add to the solutions array and return it to be output in main() */
			(*solutions)[i] = sin(angle);
			angle = rad_deg(1,(angle+10.0));
		}
	}
 
	return 0;
}
 
double rad_deg(int type, double angle)
{
	//Type = 0 Convert from degrees to radians
	//Type = 1 Convert from radians to degrees
	
	if(type==0) return angle*(M_PI/180);
	else return angle*(180/M_PI);
 
	return 0;
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland 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
>> >> At least one of the compilers I'm currently using does not accept it (Mingw gcc).
>> Then is does not support C99 fully :)

It doesn't claim to either ;)


>> Both those versions of code work but what I'm trying to do is create an array inside a function and return it with that function to be used in main()

Something like this ?


#define _USE_MATH_DEFINES
#include <stdio.h>
#include <stdlib.h>       /* <--- add this include */
#include <math.h>
 
int trig(int, double **);       /* <--- modified the function prototype */
double rad_deg(int, double);
 
int main()          /* <--- main has to return int */
{
        double *solutions = 0;  /* <--- declare as pointer only - no size */
        int i;
 
        int size = trig(0, &solutions);    /* <--- pass the address of the pointer and return the size */
 
        for(i = 0; i < size; ++i)     /* <--- use the returned size as loop limit */
        {
                printf("\n Angle: %f",solutions[i]);   /* <--- use %f instead of %lf */
        }
 
        free(solutions);     /* <--- clean up */
 
        return 0;     /* <--- return an int !! */
}
 
int trig(int type, double **solutions)     /* <--- double** instead of double* */
{
        double angle;
        int i;
 
        if(type==0) //Sine
        {       angle = rad_deg(1,0.0);
                *solutions = (double*) calloc(19, sizeof(double));  /* <--- allocate the array */
                for(i=0; i<19; i++)
                {       
                        /* Add to the solutions array and return it to be output in main() */
                        (*solutions)[i] = sin(angle);   /* <--- use () */
                        angle = rad_deg(1,(angle+10.0));
                }
        }
        return 19;        /* <--- return the size of the array */
}
 
double rad_deg(int type, double angle)
{
        //Type = 0 Convert from degrees to radians
        //Type = 1 Convert from radians to degrees
        
        if(type==0) return angle*(M_PI/180);
        else return angle*(180/M_PI);
}

Open in new window

Yeah, those are all good but i have a question:

why is it double** and not just double* for the definition of the function?
Because you must pass a pointer to a pointer so that when the pointer inside the function call is updated the changes to the pointer are seen on the outside. Just like when you pass an int you must pass the address if you want to change the contents inside the function and see the changes on the outside. A pointer is nothing special, it is just another data type.
SOLUTION
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
This little contrived example may help clarify.

As you can see, just as we need to pass the address of n to foo to see the changes to its contents outside foo so to we have to pass the address of pn to bar to see the changes to its contents outside of bar. The fact that pn just happens to be a pointer type of int is of no consequence, it is still subject to the same semantics and syntax as all other first class data types.

int gn = 1;
 
void bar(int ** ppn_)
{
	(*ppn_) = &gn;
}
 
void foo(int * pn_)
{
	int *pn;
	bar(&pn);
 
	(*pn_) = *pn;
}
 
int main()
{
	int n;
	foo(&n);
	return n;
}

Open in new window

So, you did NOT want to create the array inside the function after all ? I'm confused now.

The last code I posted does exactly that ...
And btw : I hope you fixed the other issues I pointed out, like the main that needs to return int, the %f, etc.