2016-03-25 15 views
-4

Bir listeyi saklıyorum ve sonra görüntülemeye çalışıyorum ancak görüntüleme işlevini çağırdığımda bu hatayı alıyorum. Herhangi bir uyarı veya hata vermediğinden sorunun ne olduğundan emin değilim. Hiç bu kadar ileride kullanmak üzere, hata da o büyük takdir ne anlama geldiğini açıklayabilir EğerListeyi görüntülemeye çalışırken erişim ihlali (0xCCCCCCD0) okuyun (0xCCCCCCD0)

void displayAll(struct list_el *pointer) { 

    struct list_el *temp; 
    temp = pointer; 

    if (pointer == NULL) { 
     printf("Nothing to Display"); 
    } 

    else { 
     while (temp != NULL) { 
      printf("\nDisplaying All Surveys\n"); 
      printf("Name: %s %s", temp->data.firstName, temp->data.lastName); 
      printf("\nPPS: %d", temp->data.ppsNo); 
      printf("\nAddress: %s", temp->data.address); 
      printf("\nAddress: %s", temp->data.email); 

      // Switch statement that determines the age group depending on the value 
      switch (temp->data.age) { 
      case 1: 
       printf("\n18 - 20"); 
       break; 

      case 2: 
       printf("\n20 - 30"); 
       break; 

      case 3: 
       printf("\n30 - 50"); 
       break; 

      case 4: 
       printf("\n50 - 65"); 
       break; 

      case 5: 
       printf("\n65+"); 
       break; 
      } 

      switch (temp->data.income) { 

      case 1: 
       printf("\nNo Income"); 
       break; 

      case 2: 
       printf("\nLess than 20,000"); 
       break; 

      case 3: 
       printf("\nLess than 40,000"); 
       break; 

      case 4: 
       printf("\nLess than 60,000"); 
       break; 

      case 5: 
       printf("\nLess than 80,000"); 
       break; 

      case 6: 
       printf("\nLess than 100,000"); 
       break; 

      case 7: 
       printf("\nGreater than 100,000"); 
       break; 
      } 

      switch (temp->data.age) { 

      case 1: 
       printf("\nMale"); 
       break; 

      case 2: 
       printf("\nFemale"); 
       break; 
      } 

      switch (temp->data.exercise) { 

      case 1: 
       printf("\t1: Never\n"); 
       break; 

      case 2: 
       printf("\nExercises less than three times per week"); 
       break; 

      case 3: 
       printf("\nExercises less than five times per week"); 
       break; 

      case 4: 
       printf("\n Exercises more than five times per week"); 
       break; 
      } 

      switch (temp->data.alcohol) { 

      case 1: 
       printf("\nNo Units\n"); 
       break; 

      case 2: 
       printf("\nLess than 2 Units per week"); 
       break; 

      case 3: 
       printf("\nLess than 3 Units per week"); 
       break; 

      case 4: 
       printf("\nMore than 4 Units per week"); 
       break; 
      } 

      switch (temp->data.cigarettes) { 

      case 1: 
       printf("\nNo Cigarettes"); 
       break; 

      case 2: 
       printf("\nLess than 20 cigarettes per week"); 
       break; 

      case 3: 
       printf("\nLess than 40 cigarettes per week"); 
       break; 

      case 4: 
       printf("\nMore than 40 cigarettes per week"); 
       break; 
      } 

      temp = temp->next; 
     } 
    } 

} 

gösterilecek değerler

void addSurvey(struct list_el** pointer) { 

    struct list_el* data; 
    data = (struct list_el*)malloc(sizeof(struct list_el)); 

    printf("\n===== Adding Someone to the Survey =====\n"); 

    // Depending on the number added, they output will be different 
    // Taking in details and passing through the list 
    printf("Enter PPS Number: "); 
    scanf("%d", &data->data.ppsNo); 
    printf("Enter First Name: "); 
    scanf("%s", data->data.firstName); 
    printf("Enter Last Name: "); 
    scanf("%s", data->data.lastName); 
    printf("Enter Address: "); 
    scanf("%s", data->data.address); 
    printf("Enter Email Address: "); 
    scanf("%s", data->data.email); 

    // Number added will determine the age group 
    printf("\nEnter age group\n"); 
    printf("\t1: 18 - 20\n"); 
    printf("\t2: 20 - 30\n"); 
    printf("\t3: 30 - 50\n"); 
    printf("\t4: 50 - 65\n"); 
    printf("\t5: 65+\n"); 
    scanf("%d", &data->data.age); 

    if (data->data.age > 5 && data->data.age < 1) { 
     printf("Error - Re Enter Age group\n"); 
     scanf("%d", &data->data.age); 
    } 

    // Number added will determine income group 
    printf("\nIncome Bracket - What category do you fall under\n"); 
    printf("\t1: No Income\n"); 
    printf("\t2: Less than 20,000\n"); 
    printf("\t3: Less than 40,000\n"); 
    printf("\t4: Less than 60,000\n"); 
    printf("\t5: Less than 80,000\n"); 
    printf("\t6: Less than 100,000\n"); 
    printf("\t7: Greater than 100,000\n"); 
    scanf("%d", &data->data.income); 

    if (data->data.income > 7 && data->data.income < 1) { 
     printf("Error - Re Enter Income\n"); 
     scanf("%d", &data->data.income); 
    } 

    // Number added will determine gender 
    printf("\nGender\n"); 
    printf("\t1: Male\n"); 
    printf("\t2: Female\n"); 
    scanf("%d", &data->data.gender); 

    if (data->data.gender > 2 && data->data.gender < 1) { 
     printf("Error - Re Enter Gender\n"); 
     scanf("%d", &data->data.gender); 
    } 

    // Number will be added to determine how often they exercise 
    printf("\nExercise - How often do you exercise\n"); 
    printf("\t1: Never\n"); 
    printf("\t2: Less than three times per week\n"); 
    printf("\t3: Less than five times per week\n"); 
    printf("\t4: More than five times per week\n"); 
    scanf("%d", &data->data.exercise); 

    if (data->data.exercise > 4 && data->data.exercise < 1) { 
     printf("Error - Re Enter Exercise\n"); 
     scanf("%d", &data->data.exercise); 
    } 

    // Number will be added to determine how many units they drink 
    printf("\nAlcohol - How many units of Alcohol do you drink per week\n"); 
    printf("\t1: None\n"); 
    printf("\t2: Less than 2 Units per week\n"); 
    printf("\t3: Less than 3 Units per week\n"); 
    printf("\t4: More than 4 Units per week\n"); 
    scanf("%d", &data->data.alcohol); 

    if (data->data.alcohol > 4 && data->data.alcohol < 1) { 
     printf("Error - Re Enter Alcohol\n"); 
     scanf("%d", &data->data.alcohol); 
    } 


    // Number will be added to determine how many cigarattes per week they smoke 
    printf("\nCigarettes - How many Cigarettes do you smoke per week\n"); 
    printf("\t1: None\n"); 
    printf("\t2: Less than 20 cigarettes per week\n"); 
    printf("\t3: Less than 40 cigarettes per week\n"); 
    printf("\t4: More than 40 cigarettes per week\n"); 
    scanf("%d", &data->data.alcohol); 

    if (data->data.alcohol > 4 && data->data.alcohol < 1) { 
     printf("Error - Re Enter Alcohol\n"); 
     scanf("%d", &data->data.alcohol); 
    } 

} 

işlevi almak

typedef struct { 

    // Variables - Details on person taking survey 
    int ppsNo; 
    char firstName[15]; 
    char lastName[15]; 
    char address[30]; 
    char email[30]; 

    // Groups - Statistics are broken down based on these groups 
    int age; 
    int income; 
    int gender; 

    // Survey - What the survey questions will consist of 
    int exercise; 
    int alcohol; 
    int cigarettes; 

}survey_t; // Structure defined 

struct list_el{ 

    survey_t data; 
    struct list_el* next; 
}; // List for passing values into the struct 

Gelecekte nasıl başa çıkacağını biliyorum.

+0

Oku erişim ihlali (o yok muhtemelen çünkü) bu adresi okuyamaz demektir . Adres, hata ayıklamada yardımcı olmak için "açıkçası yanlış" olmak üzere belleğe yazılmış bir şey gibi bir hata ayıklama sentinel değerine benziyor. Tahmin edeceğim (kodunuzu okumadan), muhtemelen boşalttıktan sonra hafızayı kullandınız. Hata ayıklama modunda, bellek yöneticisi genellikle bu tür sentinel değerlerle serbest belleğin üzerine yazacaktır, ancak bu derleyici ve platforma bağlıdır ve platformunuzdan bahsetmediniz. – Ben

+0

Hata ayıklayıcı .................................. –

cevap

1

Başlatılmamış işaretçi. Hata ayıklama modunda, yığın daha hızlı hata tespiti için genellikle 0xCC değerleri ile doldurulur.

0

Hata, yasa dışı bir adreste belleğe (okuma/yazma) erişmeye çalıştığınız anlamına gelir. Tipik olarak bu, yasa dışı bir işaretçi değerini geçersiz kılmanız anlamına gelir.

Listeniz eksik görünüyor. Bu kodda

: öyle görünüyor

void addSurvey(struct list_el** pointer) { 

    struct list_el* data; 
    data = (struct list_el*)malloc(sizeof(struct list_el)); 
    ... 

Eğer pointer hiç kullanmıyorum. Başka bir deyişle, yeni öğeyi listeye eklemiyorsunuz. Eğer böyle bir şey eksik: yani yeni elemanın next başlatmak gerekir Ayrıca

*pointer = data; 

,

data->next = NULL; 
İlgili konular