Funktionen mit Zeigern



  • Hallo alle zusammen,

    ich schreibe gerade für das Studium ein Programm wo ich nicht weiter komme 😕 .
    Das Programm soll mittels Array vier Werte erfassen.
    Dieser Teil funktioniert auch.
    Bei der Übergabe der Werte in eine andere Funktion kommen allerdings Fehler, weil ich wohl die falschen Übergabeparamenter eingeben habe 😕 .
    Kann mir einer helfen??

    #include <stdio.h>
    
    float projektion(float x[],float y[],float xy[], float size);
    
    float *zgr_xy;
    
    float main(void)
    
    {
       int i;
       int size = 0;
       float a_array[2];
       float b_array[2];
       float xy_array[2];
       zgr_xy=&xy_array;
    
       for(i=0;i<2;i++)
       {
    	   printf("Bitte geben Sie Vektor a ein");
    	   scanf("%f", &a_array[i]);
           printf("Eingabe a[%d]: %f\n",i, a_array[i]);
       }
    
       for(i=0;i<2;i++)
       {
    	   printf("Bitte geben Sie Vektor b ein");
    	   scanf("%f", &b_array[i]);
           printf("Eingabe b[%d]: %f\n",i, b_array[i]);
           size=i;
       }
    
       printf("\nErgebnissvektor:\n\n");
       for(i=0;i<size;i++)
       {
          printf("\nba[%f] = " ,projektion(a_array,b_array,*zgr_xy));   /* Übergabe die nicht geht */
       }
    
       return 0;
    
    }
    
       float projektion(float x[],float y[],float xy[], int size);
    
       {
    
    	   int count;
    
    	   for(count=0;count<size;count++)
    
    	   {
    		   xy[count]=a[count]+b[count];   /*Ich muss zwei Werte zurückgeben*/
           }
    
    	   return 0;
       }
    


  • der prototyp hat bei size einen anderen datentyp als bei der definition.

    ps: und das semikolon hinter dem funktionskopf bei der definition schenk dir bitte auch



  • Vielen dank für den Tipp. 🙂
    Hast Du auch die Lösung für das Problem?? So das ich zwei Werte zurückgeben kann?

    Danke



  • Weiß nicht genau, wie die Berechnung aussehen soll, aber klappt es so?

    #include <stdio.h>
    
    void projektion(float x[],float y[],float xy[], int size);
    
    float main(void)
    
    {
       int i;
       int size = 0;
       float a_array[2];
       float b_array[2];
       float xy_array[2];
    
       for(i=0;i<2;i++)
       {
           printf("Bitte geben Sie Vektor a ein");
           scanf("%f", &a_array[i]);
           printf("Eingabe a[%d]: %f\n",i, a_array[i]);
       }
    
       for(i=0;i<2;i++)
       {
           printf("Bitte geben Sie Vektor b ein");
           scanf("%f", &b_array[i]);
           printf("Eingabe b[%d]: %f\n",i, b_array[i]);
           size=i+1;
       }
    
       printf("\nErgebnissvektor:\n\n");
       projektion(a_array,b_array,xy_array,size);
       for(i=0;i<size;i++)
       {
          printf("\nba[%f] = ", xy_array[i]);   /* Übergabe die nicht geht */
       }
    
       return 0;
    }
    
       void projektion(float x[],float y[],float xy[], int size)
       {
           int count;
    
           for(count=0;count<size;count++)
           {
               xy[count]=x[count]+y[count];   /*Ich muss zwei Werte zurückgeben*/
           }
       }
    


  • Vielen Dank! Das war flot! 🙂
    Wenn ich mal C kann helfen ich auch im Forum 😉
    Die Rechnung ist nicht so wichtig, ich muss nur Verstehen wie der Austausch von Parameter bzw. Zeigern funktioniert.

    Das Programm funktioniert auch, nur weiß ich nicht genau warum.

    Warum:

    float projektion(float x[],float y[],float xy[], float size);

    statt

    void projektion(float x[],float y[],float xy[], int size);

    Die Funktion gibt doch der aufrufenden Funtktion aus main einen Wert zurück?

    Und:
    [cpp]
    printf("\nErgebnissvektor:\n\n");
    projektion(a_array,b_array,xy_array,size);
    for(i=0;i<size;i++)
    {
    printf("\nba[%f] = ", xy_array[i]); /* Übergabe die nicht geht */
    }

    return 0;
    }

    void projektion(float x[],float y[],float xy[], int size)
    {
    int count;

    for(count=0;count<size;count++)
    {
    xy[count]=x[count]+y[count]; /*Ich muss zwei Werte zurückgeben*/
    [/cpp]

    Zum Verständnis:
    projektion(a_array,b_array,xy_array,size);
    Übergibt beide Werte von a_array und beide Werte von b_array??? Ich brauche kein count.

    wie kann xy_array[i]); alle Werte zurückgeben, obwohl kein count zählt??

    Vielen Dank


Anmelden zum Antworten