2015-07-10 61 views
1

這是我的第一篇文章。我搜索了一下,但找不到任何特定於我的問題,有助於解決它。當我試圖解引用指針或在函數中應用指針數學時,事情並沒有做到他們應該做的。爲什麼我的程序(sort_them函數)沒有排序?

例如:*(ptr + a)應該在技術上推進一個指向我設定的值的指針。相反,我只是得到一個錯誤,表示「請求成員'名稱'的東西不是結構或聯盟」。這是沒有道理的,因爲我已經聲明並將我的結構指針傳遞給函數。如果我使用:「start_ptr-> name.last + a」風格,它會編譯但不排序任何內容。我知道這第二個版本也是不正確的,當涉及到指針數學。

所以基本上,看起來錯在編譯,看起來正確的不是,實際上也沒有排序任何東西。

下面是代碼

#include <stdio.h> 
#include <string.h> 
#include <ctype.h> 

/* Structure definition */ 
/* -------------------- */ 

struct split 
{ 
    char last[10]; 
    char first[10]; 
}; 

struct info 
{ 
    struct split name; 
    char address[20]; 
    char city[15]; 
    char state[3]; 
    long zip; 
    int  age; 
    char gender; 
}; /* end struct info */ 


void sort_them(struct info *, char); // sort prototype 

int main(void) 
{ 
/* Declare variables */ 
/* ----------------- */ 

struct info  people[] = 
{ 

    {"Asimov", "Isaac", "15 Main St", "Worcestor", "MA", 01555, 23, 'M'}, 
    {"Smith", "Jane", "17 Make Peace", "Wallham", "ND", 10102, 28, 'F'}, 
    {"De Rippa", "Jack", "18 Able Way ", "Boston", "MA", 50503, 74, 'M'}, 
    {"Cobb", "Jim", "55 Elm St", "Ware", "MO", 61555, 65, 'M'}, 
    {"Kapone", "Al", "15 Morin Ave", "Idunno", "MN", 31333, 34, 'M'}, 
    {"Seigel", "Myron", "44 Wing Blvd West", "Sandwich", "WA", 02537,  21, 'M'}, 
    {"Thymes", "Mary", "88 Same Place", "Washington", "DC", 90555, 44, 'F'} 

}; 

int    bad_sort; 
int    num_people = sizeof(people)/sizeof(people[0]); //this will be the count of how many people there are! 
char   sort_order; 
char   big_name[22]; 





struct info *start_ptr = &people[0]; 
struct info *nums_ptr, *nums_end_ptr = &people[6]; 
char *big_ptr = &big_name[0]; 







printf ("\nWelcome to the People Structure Data Report Program\n"); 



do 
{ 
     bad_sort = 1; 

     printf ("\nEnter the sort order for the report: "); 
     printf ("\n(N=Name, A=Age, S=State, Z=Zip code ' '=no sort)\n "); 

/*    blank or return is allowed for no sorting of the data */ 

     sort_order = getchar(); 

     if(sort_order == '\n' || sort_order == ' ') break; 

     sort_order = toupper(sort_order); 

     if ((sort_order == 'N') || (sort_order == 'A') || 
        (sort_order == 'S') || (sort_order == 'Z')) 
      bad_sort = 0; 
     else 
      printf("\nIncorrect Sort order selected, please re-enter: "); 

} while (bad_sort == 1); 



switch (sort_order) 
{ 
    case 'N': 
     printf("Sort by Name %i People.\n", num_people); 
     break; 
    case 'A': 
     printf("Sort by Age %i People.\n", num_people); 
     break; 
    case 'S': 
     printf("Sort by State %i People.\n", num_people); 
     break; 
    case 'Z': 
     printf("Sort by Zip %i People.\n", num_people); 
     break; 
    default: 
     printf("No Sort selected %i People.\n", num_people); 
     break; 
} /* end cases */ 

if(sort_order != ' ') 
    sort_them (start_ptr, sort_order); 


/* Print Report */ 
/* ------------ */ 

printf("\n\n     The People Report\n\n"); 

printf ("\n\n%-20s%-20s %-15s %-5s %-6s %-3s %-6s", 
     "Name", "Address","City","State","Zip","Age","Gender"); 

printf ("%-20s%-20s %-15s %-5s %-6s %-3s %-6s\n", 
     "----", "------","----","-----","---","---","------"); 


for (nums_ptr = start_ptr; nums_ptr <= nums_end_ptr; nums_ptr++, start_ptr++) 
{ 

    strcpy(big_ptr, start_ptr->name.last); 
    strcat(big_ptr, ", "); 
    strcat(big_ptr,start_ptr->name.first); 



    printf ("%-20s%-20s %-15s %-4s%.5ld %-3i %c\n", 
                      big_ptr,start_ptr->address,start_ptr->city,start_ptr->state,start_ptr->zip, 
      start_ptr->age,start_ptr->gender);  

} /* end for loop */ 



printf("\n\n"); 

return 0; 

} /* end main */ 

void sort_them(struct info *start_ptr, char sort_by) 
{ 
int a,b; 
struct info temp; 


for(a = 0; a < 6; a++) 

    for(b = a + 1; b < 7; b++) 
    { 
     if( (sort_by == 'N' && strcmp(start_ptr->name.last + a, start_ptr->name.last + b) > 0) 
      || (sort_by == 'A' && start_ptr->age + a > start_ptr->age + b) 
      || (sort_by == 'S' && strcmp(start_ptr->state + a, start_ptr->state + b) > 0) 
      || (sort_by == 'Z' && (start_ptr->zip + a) > (start_ptr->zip + b)) 
     ) 


     { 
      temp = *(start_ptr + a); 
      *(start_ptr + a) = *(start_ptr + b); 
      *(start_ptr + b) = temp; 

     } /* end if */ 


    } /* end inner for loop */ 





}; 









/* end of sort them 

回答

0

我認爲以下行

strcmp(start_ptr->name.last + a, start_ptr->name.last + b) > 0 

應與其他的比較來改變像下面

strcmp((start_ptr + a)->name.last, 
     (start_ptr + b)->name.last 
    ) > 0 

等。

0

首先沒有理由不使用陣列中符號C:

for(a = 0; a < 6; a++) 
for(b = a + 1; b < 7; b++) 
{ 
    if( (sort_by == 'N' && strcmp(start_ptr[a].name.last, start_ptr[b].name.last) > 0) 
     || (sort_by == 'A' && start_ptr[a].age > start_ptr[b].age) 
     || (sort_by == 'S' && strcmp(start_ptr[a].state, start_ptr[b].state) > 0) 
     || (sort_by == 'Z' && (start_ptr[a].zip) > (start_ptr[b].zip)) 
    ) 


    { 
     temp = start_ptr[a]; 
     start_ptr[a] = start_ptr[b]; 
     start_ptr[b] = temp; 

    } /* end if */ 

表達start_ptr->name.last + a添加到start_ptr[0].name.last和沒有指針運算的「風格」。

指針運算可以通過(ptr+a)來完成,其中ptr被提前a*sizeof(*ptr),如果* ptr指向有效的對象。否則,* ptr產生一個段錯誤(例如ptr = NULL)。然後,您可以使用* ptr:sizeof(type)的類型,在我們的示例中爲:sizeof(struct info)

基本上*(ptr+a)ptr[a](ptr+a)&ptr[a]相同。

如果你想使用->運營商,你需要寫

(start_ptr+a)->name 

例如。

相關問題