2010-05-27 52 views
0
#include<stdlib.h> 
#include<stdio.h> 
#include<string.h> 

//This program is a sorting application that reads a sequence of numbers from a file and prints them on the screen . The reading from the file here , is a call back function . 

typedef int (*CompFunc)(const char* , const char*); 
typedef int (*ReadCheck)(char nullcheck); 
char array[100]; 

//Let this function be done in the library itself. It doesn't care as to where the compare function and how is it implemented. Meaning suppose the function wants to do sort in ascending order or in descending order then the changes have to be done by the client code in the "COMPARE" function who will be implementing the lib code. 

void ReadFile(FILE *fp,ReadCheck rc) 
{ 
    char a; 
    char d[100]; 
    int count = 0,count1=0; 
    a=fgetc(fp); 
    //printf("%c",a); 
    //count1=(*rc)(a); 
    //printf("%d",count1); 
    while (1 !=(*rc)(a)) 
    { if(a==' ') 
     { 

     d[count1]='\0'; 
     strcpy(&array[count],d); 
     count=count+1; 
     printf("%s \n",d); 
     memset(d,'\0',100); 
     count1=0; 
     } 
     else 
     { 

     d[count1]=a; 
     count1=count1+1; 
     //a=fgetc(fp); 

     } 
     //printf("%c",a); 
     a=fgetc(fp); 
    } 

} 
void Bubblesort(char* array , int size , int elem_size , CompFunc cf) 
{ int i,j,k; 
    int *temp; 
    for(i=0;i < size ;i++) 
    { 
     for (j=0;j < size -1 ; j++) 
     { 
      // make the callback to the comparision function 
      if(1 == (*cf)(array+j*elem_size,array+ (j+1)*elem_size)) 
       { 
        //interchanging of elements 
        temp = malloc(sizeof(int *) * elem_size); 
        memcpy(temp , array+j*elem_size,elem_size); 
        memcpy(array+j*elem_size,array+(j+1)*elem_size,elem_size); 
        memcpy(array + (j+1)*elem_size , temp , elem_size); 
        free(temp); 
       } 
     } 
    } 

for (k=0;k<5;k++) 
    printf("%s \n",array[k]); 
} 



//Let these functions be done at the client side 

int Compare(const char* el1 , const char* el2) 
    { 
     int element1 = *(int*)el1; 
     int element2 = *(int*)el2; 

     if(element1 < element2) 
      return -1; 
     if(element1 > element2) 
      return 1 ; 
     return 0; 
    } 

int ReadChecked(char nullcheck) 
    { 
     if (nullcheck=='\n') 
      return 1; 
     else 
      return 0; 
    } 
int main() 
{ 
    FILE *fp1; 
    int k; 
    fp1=fopen("readdata.txt","r"); 
    ReadFile(fp1,&ReadChecked); 
    for (k=0;k<5;k++) 
    printf("%s \n",array[k]); 
    Bubblesort((char*)array,5,sizeof(array[0]),&Compare); 
    printf("after sorting \n"); 
    for (k=0;k<5;k++) 
    printf("%s \n",array[k]); 

return 0; 
} 

陣列具有數據段故障而在打印陣列用C

123 
    11 
    2312 
    121 
    231 

它應該打印數據完全相同的方式。儘管它的打印在結束時給出了分段錯誤。

+0

什麼類型是數組? – 2010-05-27 12:46:12

+0

需要更多的代碼。 – Stephen 2010-05-27 12:49:37

+0

我給了現在的整個代碼.. – Hick 2010-05-27 13:01:06

回答

2

什麼類型是數組?這聽起來像你正在使用它不正確。

如果您有int數組:

printf("%i \n",array[k]); 

注意:%i和%d是同義的輸出。

如果你有一個字符串數組:

%s是該類型char*的字符串。這意味着要使用%s,您必須確保數組中的每個元素都擁有它自己的以NULL結尾的字符串,每個類型都是char*。確保字符串是帶有0終止符的字符數組。

+0

@natheres:%i和%d是輸出的同義詞。 – 2010-05-27 12:49:45

+0

隨着我們的回答,問題不斷變化。我指出他錯誤地訪問了陣列,然後他在他的問題中糾正了它。然後它變成了一串字符串,而不是(看起來像)一個int數組。請記住,這個問題有自己的生命:) – 2010-05-27 12:53:23

+0

@Tim:我認爲我得到了downvotes(3),因爲人們認爲你不能用%i來打印一個int。但是,正如printf文檔中指出的那樣,就輸出而言它們是相同的。 – 2010-05-27 12:54:35

5

array是什麼類型的?如果是int的數組,則應該使用格式%d而不是%s來打印它。

printf("%d\n", array[k]); 

如果arrayint的數組:

如果使用%s,所述printf功能將把array[k]作爲字符串(char*),因此將解引用值打印那裏的人物。但是123(0x7b)是一個無效地址,所以系統會用SEGFAULT來終止可執行文件。

請在編譯時啓用所有警告。編譯器能夠看到類型錯誤並警告您。


編輯:但是arraychar數組....它只能容納1串邏輯地而不是5.要打印您使用

printf("%s \n", array); // cannot index. 

你最好修改結構的代碼。

+0

這是一個字符串.. – Hick 2010-05-27 12:48:06

+0

從你問15分鐘前的問題,它看起來像它是一個整數。 – 2010-05-27 12:49:21

+0

@meka:請發佈所有代碼。如果它是一個字符串數組,則該錯誤在別處。 – kennytm 2010-05-27 12:51:38

0

你確定它是發生分段錯誤的printf循環嗎?如果所有數字都按照您的說法打印出來,那麼可能會導致分段錯誤。

添加一個printf(「Made it to here。\ n」);循環後確定。

0
printf("%s \n",array[k]); 

array [k]是一個字符,而不是字符指針,所以需要用%c打印,而不是%s。否則,它將試圖取消引用前256個字節的內存中的地址,並且無效地搜索要打印的字符串。