PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > comp.lang.c > Array of pointer and pointer of array
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
Array of pointer and pointer of array

Réponse
 
LinkBack Outils de la discussion
Vieux 28/01/2008, 12h44   #1
erfan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Array of pointer and pointer of array

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[i],functable[i](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
  Réponse avec citation
Vieux 28/01/2008, 12h53   #2
Joachim Schmitz
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Array of pointer and pointer of array

erfan wrote:
<irrelevant code snipped>
> 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


  Réponse avec citation
Vieux 28/01/2008, 12h54   #3
fnegroni
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Array of pointer and pointer of array

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.
  Réponse avec citation
Vieux 28/01/2008, 12h54   #4
Mark Bluemel
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Array of pointer and pointer of array

erfan wrote:
> 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
  Réponse avec citation
Vieux 28/01/2008, 12h57   #5
erfan
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Array of pointer and pointer of array

On 1ÔÂ28ÈÕ, ÏÂÎç8ʱ54·Ö, Mark Bluemel <mark_blue...@pobox.com> wrote:
> erfan wrote:
> > 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
  Réponse avec citation
Vieux 28/01/2008, 13h11   #6
Army1987
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Array of pointer and pointer of array

erfan wrote:

> 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.


--
Army1987 (Replace "NOSPAM" with "email")
  Réponse avec citation
Vieux 28/01/2008, 20h55   #7
Martin Ambuhl
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: Array of pointer and pointer of array

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.
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 06h00.


Édité par : vBulletin® version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières ©2000-2008
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,19000 seconds with 15 queries