Solved

C Compiler Error program

Posted on 2007-11-29
23
189 Views
Last Modified: 2010-05-18
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
Comment
Question by:adamport
  • 11
  • 7
  • 3
  • +1
23 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 20372191
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372200
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372207
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372227
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372229
btw, // is not a valid comment in C !!

And your main should return int :

        int main(void) {
            /* your code */
            return 0;
        }
0
 
LVL 40

Expert Comment

by:evilrix
ID: 20372264
>> 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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372278
>> >> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372287
>> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372288
s/note/not
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372315
>> >> 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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372372
>> At least one of the compilers I'm currently using does not accept it (Mingw gcc).
Then is does not support C99 fully :)
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

Author Comment

by:adamport
ID: 20372615
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
 
LVL 84

Expert Comment

by:ozo
ID: 20372643
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
 
LVL 84

Expert Comment

by:ozo
ID: 20372669
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372673
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
 
LVL 40

Accepted Solution

by:
evilrix earned 75 total points
ID: 20372699
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372794
>> >> 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
 

Author Comment

by:adamport
ID: 20372821
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372865
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
 
LVL 84

Assisted Solution

by:ozo
ozo earned 25 total points
ID: 20372903
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
 
LVL 40

Expert Comment

by:evilrix
ID: 20372944
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372973
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
 
LVL 53

Expert Comment

by:Infinity08
ID: 20372978
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now