Alexander (HH): C: Verlinken statischer Bibliothek führt zu falschem Ergebnis

Beitrag lesen

Moin Moin!

Ich bin bei C etwas eingerostet, aber ...

double wert = 0.0;

0.0 ist ein single, kein double.

for (int i = 0; i < arrused; i++)  {
wert += arr[i];
}

if (arrused != 0)

Auch wenn arrused<0? Nicht gut.

  return wert /= arrused;  

return wert;

x/0=0? Mein Mathelehrer rotiert im Grabe!

}
[/code]

Main.c

[code lang=c]
#include "durchschnitt.h"

int main(void)   {

int c;
int j;
int arrayused;
double array[MAX];
char hilfsarray[10];
j = arrayused = 0;

while (arrayused < MAX && ((c = getchar()) != EOF))   {
if ( (c >= '0' && c <= '9') || c == '.')  {
  hilfsarray[j++] = c;
}
else  {
  hilfsarray[j] = '\0';
  array[arrayused++] = atof(hilfsarray);

atof liefert keine Fehlermeldungen, nutze strtod.

   	  j = 0;  
   }  

Und was passiert, wenn c weder NUL noch Ziffer noch Punkt ist?

$ gcc -c durchschnitt.c -o durchschnitt.o -std=c99
$ ar rcs libdurchschnitt.a durchschnitt.o
$ gcc -static durchschnitt.h Main.c -L. -ldurchschnitt -o Main_statisch -std=c99

Laß gcc mal mit -Wall -pedantic laufen und beseitige alle Warnungen.

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so".