Array of pointer and pointer of array

E

erfan

hi, here is my code :
//learn a pointer points to functions
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
double Add(double x,double y) {return (x+y);}
double Sub(double x,double y) {return x-y;}
double Mul(double x,double y) {return x*y;}
double Div(double x,double y) {return x/y;}
//double Pow(double x,double y) {return pow(x,y);}
double (*functable[5])(double,double)={Add,Sub,Mul,Div,pow};
char *msgtable[5]={"Sum","Sub","Mul","Div","Pow"};
int main()
{
int i;
double x=0,y=0;
printf("input two numbers:\n");
if(scanf("%lf,%lf",&x,&y)!=2)
{
printf("wrong input!\n");
exit(1);
}
for(i=0;i<5;i++)
printf("%s:%f\n",msgtable,functable(x,y));
return 0;
}
-------------------
when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o:
(.data+0x10): undefined reference to `pow'
collect2: ld $BJV2s(B 1
what`s wrong, i want to use function of pow,and his header
<<math.h>>was include,however,
where is the key of the error?
have a nice day :)
 
J

Joachim Schmitz

erfan wrote:
when i run this programe,gcc gave me an error,that is
:/tmp/cc2ZP8T4.o: (.data+0x10): undefined reference to `pow'
collect2: ld $BJV2s(B 1
what`s wrong, i want to use function of pow,and his header
<<math.h>>was include,however,
where is the key of the error?
have a nice day :)
You'd need to link the math lib, done via the "-lm" Compiler/Linker option

Bye, Jojo
 
F

fnegroni

I have successfully compiled your program.
What platform are you on?

Some platforms require the -lm option to link the math library. On
some (OS X the one I am using now) I guess it is not needed.
 
M

Mark Bluemel

erfan said:
hi, here is my code :
[snip]

And what has this to do with the subject (which was "Array of
pointer and pointer of array")?
when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o:
(.data+0x10): undefined reference to `pow'
collect2: ld $BJV2s(B 1
what`s wrong, i want to use function of pow,and his header
<<math.h>>was include,however,
where is the key of the error?

This looks like FAQ 13.25 - see http://www.c-faq.com
 
E

erfan

erfan said:
hi, here is my code :

[snip]

And what has this to do with the subject (which was "Array of
pointer and pointer of array")?
when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o:
(.data+0x10): undefined reference to `pow'
collect2: ld ·µ»Ø 1
what`s wrong, i want to use function of pow,and his header
<<math.h>>was include,however,
where is the key of the error?

This looks like FAQ 13.25 - seehttp://www.c-faq.com

a-wo, i missed to link the library :-} , thank you all for your
tips
 
A

Army1987

erfan said:
hi, here is my code : [...]
#include<math.h>
[...]
//double Pow(double x,double y) {return pow(x,y);}
double (*functable[5])(double,double)={Add,Sub,Mul,Div,pow};
char *msgtable[5]={"Sum","Sub","Mul","Div","Pow"};
int main()
{ [...]
exit(1);
That's nonportable, use exit(EXIT_FAILURE);
[...]
when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o:
(.data+0x10): undefined reference to `pow' collect2: ld 返回 1
what`s wrong, i want to use function of pow,and his header
<<math.h>>was include,however,
Go to http://www.c-faq.com/ and read question 14.3.
 
M

Martin Ambuhl

erfan wrote:
[..]
when i run this programe,gcc gave me an error,that is :/tmp/cc2ZP8T4.o:
(.data+0x10): undefined reference to `pow'
collect2: ld 返回 1
what`s wrong, i want to use function of pow,and his header
<<math.h>>was include,however,
where is the key of the error?

Learn to check the FAQ (as well as your textbook and implementation
documentation) before posting. In this case, your question is 13.25 in
the FAQ. The FAQ can be found at <http://c-faq.com/>, and it would
direct you to <http://c-faq.com/lib/extlibs.html>, the text of which
follows. Notice the cross-references at the end.

comp.lang.c FAQ list · Question 13.25

Q: I keep getting errors due to library functions being undefined, but
I'm #including all the right header files.

A: In the general case of calling code in an external library, using
#include to pull in the right header file(s) is only half of the story;
you also have to tell the linker to search the external library itself.
The declarations in the header file only tell the compiler how to call
the external functions; the header file doesn't supply the definitions
of the external functions, or tell the compiler/linker where to find
those definitions.

In some cases (especially if the functions are nonstandard) obtaining
those definitions may require explicitly asking for the correct
libraries to be searched when you link the program. (Some systems may be
able to arrange that whenever you #include a header, its associated
library, if nonstandard, is automatically requested at link time, but
such a facility is not widespread.) See also questions 10.11, 11.30,
13.26, 14.3, and 19.40.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top