Zum Inhalt springen
View in the app

A better way to browse. Learn more.

Fachinformatiker.de

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Zugriffsverletzung

Empfohlene Antworten

Veröffentlicht

Hallo zusammen!

Ich bekomme die Fehlermeldung "Eine Zugriffsverletzung (Segmentation Fault) trat in Ihrem Programm auf" bei folgendem Programm:


/* Das Programm sollte ein Polynom mithilfe des Horner-Schemas 

auswerten */


#include <stdio.h>


int horner(int a[], int x, int k){  /* x ist die Variable eines Polynoms */

    int h;                                /* k ist der Grad des Polynoms */ 

                                           /* a ist ein Feld, in welchem die  

                                               Polynomkoeffizienten gespeichert 

    if (k>0)                                 werden */

       h = horner(a, x, k-1);

    else

       return a[k];


    return h*x+a[k];

} 


main(){                                  /* Hier komme ich gar nicht rein. Der  

                                                Compiler bleibt an der horner-Funktion

                                                hängen */

       int n, i;

       int array[n];

       int x, result;


       printf("\nBitte geben Sie an, Polynom welchen Grades möchten Sie auswerten: ");

       scanf("%i", &n);


       for (i=0; i<=n; i++){

           printf("\na_%i: ", i);

           scanf("%i", &array[i]);

           } 


       printf("\n Bitte geben Sie x ein: ");

       scanf("%i", &x); 


       result = horner(array, x, n);

       printf("Das Ergebnis ist: %i", result);               

}

Hier komme ich gar nicht rein. Der Compiler bleibt an der horner-Funktion hängen
Kann nicht sein. Entweder liefert der Compiler einen Fehler (den du dann bitte nennst), oder du kannst das Programm erstellen und ausführen und es kommt zu einem Laufzeitfehler, dann hat der Compiler aber nichts mehr damit zu tun.

Ich glaube übrigens nicht, dass

int array[n];

etwas sinnvolles tun kann, wenn n an dieser Stelle noch gar nicht initialisiert ist. So legst du jedenfalls kein Array an, dass "magisch" die richtige Größe hat, sobald n einen Wert bekommt.

Danke euch beiden!

Das array[n] war das Problem. Ich habe das jetzt einfach nach der Eingabe von n deklariert.

Noch mal vielen Dank!

Archiv

Dieses Thema wurde archiviert und kann nicht mehr beantwortet werden.

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.