• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

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

0
adamport
Asked:
adamport
  • 11
  • 7
  • 3
  • +1
2 Solutions
 
evilrixSenior Software Engineer (Avast)Commented:
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]);

0
 
evilrixSenior Software Engineer (Avast)Commented:
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

0
 
evilrixSenior Software Engineer (Avast)Commented:
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)?
0
Live webcast with Pinal Dave

Pinal Dave will teach you tricks to help identify the real root cause of database problems rather than red herrings. Attendees will learn scripts that they can use in their environment to immediately figure out their performance Blame Shifters and fix them quickly.

 
Infinity08Commented:
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

0
 
Infinity08Commented:
btw, // is not a valid comment in C !!

And your main should return int :

        int main(void) {
            /* your code */
            return 0;
        }
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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() !!!
0
 
Infinity08Commented:
>> >> 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 :)
0
 
evilrixSenior Software Engineer (Avast)Commented:
>> 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.
0
 
evilrixSenior Software Engineer (Avast)Commented:
s/note/not
0
 
Infinity08Commented:
>> >> 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

0
 
evilrixSenior Software Engineer (Avast)Commented:
>> At least one of the compilers I'm currently using does not accept it (Mingw gcc).
Then is does not support C99 fully :)
0
 
adamportAuthor Commented:
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

0
 
ozoCommented:
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));
      }
    }
}
0
 
ozoCommented:
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);
      }
    }
0
 
evilrixSenior Software Engineer (Avast)Commented:
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

0
 
evilrixSenior Software Engineer (Avast)Commented:
Fixed some other issues -- commented. Probably missed something though :)
#include "stdafx.h"
 
#define _USE_MATH_DEFINES
#include <stdio.h>
#include <math.h>
 
#define MAX_SIZE 19 /* Use const for size */
 
void trig(int, double **);
double rad_deg(int, double);
 
int main() /* return iont */
{
	double solutions[MAX_SIZE];
	double *pSolutions = solutions; /* use pointer to make life easier */
	int i;
 
	trig(0, &pSolutions);
 
	for(i=0; i<MAX_SIZE; ++i)
	{
		printf("\n Angle: %f",solutions[i]); /* use %f */
	}
 
	return 0; /* return something */
}
 
/* return void */
void trig(int type, double **solutions)
{
	double angle;
	int i;
 
	if(type==0) //Sine
	{	angle = rad_deg(1,0.0);
		for(i=0; i<MAX_SIZE; 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

0
 
Infinity08Commented:
>> >> 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

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

why is it double** and not just double* for the definition of the function?
0
 
evilrixSenior Software Engineer (Avast)Commented:
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.
0
 
ozoCommented:
If solutions is a double*, and the function is passed &solutions so the function can modify solutions, then it's double**
if you are passed solutions and just want to modify solutions[i] then it can be double*
0
 
evilrixSenior Software Engineer (Avast)Commented:
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

0
 
Infinity08Commented:
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 ...
0
 
Infinity08Commented:
And btw : I hope you fixed the other issues I pointed out, like the main that needs to return int, the %f, etc.
0

Featured Post

The IT Degree for Career Advancement

Earn your B.S. in Network Operations and Security and become a network and IT security expert. This WGU degree program curriculum was designed with tech-savvy, self-motivated students in mind – allowing you to use your technical expertise, to address real-world business problems.

  • 11
  • 7
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now