tags:

views:

93

answers:

4
#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 fucntion be done in the library itself . It doesnt 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;
}

The array has data

    123
    11
    2312
    121
    231

and it should print the data exactly the same way . Even though its printing at the end its giving segmentation fault.
+2  A: 

What type is array? It sounds like you're using it incorrectly.

If you have an array of ints:

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

Note %i and %d are synonymous for output.

If you have an array of strings:

%s is for strings of the type char*. That means to use %s you have to be sure each element of your array holds its own null terminated string each of type char*. Make sure the strings are arrays of chars with a 0 termination.

Brian R. Bondy
@natheres: %i and %d are synonymous for output.
Brian R. Bondy
The question keeps changing as we answer it. I pointed out that he was accessing the array incorrectly, then he corrected it in his question. Then it became an array of strings, not (what looked like) an array of ints. Please hold the downvotes, this question has a life of its own :)
Tim Post
@Tim: I think I was getting downvotes (3) because people thought you can't use %i for printing an int. But as the man 3 printf documentation states they are the same as far as output is concerned.
Brian R. Bondy
@Tim I gave the whole code now . I have terminated each string with a null value and also made sure that type is char *
mekasperasky
@mekaspearasky - yes, the exact same code you posted in this question: http://stackoverflow.com/questions/2920549/where-is-the-error-in-this-c-code-and-how-to-get-rid-of-the-warnings
Tim Post
+5  A: 

What is the type of array? If it is an array of int's, you should print it with a format of %d, not %s.

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

If array is an array of int:

If you use %s, the printf function will treat array[k] as a string (char*), therefore will dereference the value print out the characters there. But 123 (0x7b) is an invalid address, so the system will kill the executable with a SEGFAULT.

Please enable all warnings while you compile. The compiler is able to see the type error and warn you.


Edit: But array is an array of char.... It can only hold 1 string logically, not 5. To print it you use

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

You'd better revise the structure of your code.

KennyTM
it is an arary of strings ..
mekasperasky
From the question you asked 15 minutes ago it looks like it's an array of ints.
Andreas Brinck
@meka: Please post all the code. The error is elsewhere if it is an array of strings.
KennyTM
Most compilers will gladly accept whatever you pass into `scanf/printf` without warning.
Andreas Brinck
@Andreas: gcc will warn when seeing a wrong type (with `-Wformat` flag). MSVC will warn when seeing a `printf` (*insecure!!111 use `printf_s`*) :)
KennyTM
i posted the whole code
mekasperasky
@meka: That's not an array of strings. That's an array of `char`!
KennyTM
A: 

Are you certain that it is the printf loop where the segmentation fault occurs? If all the numbers did print out as you say then it is possible something after it is the cause of the segmentation fault.

Add a printf("Made it to here.\n"); line after the loop make certain.

Amardeep
A: 
printf("%s \n",array[k]);

array[k] is a character, not a character pointer, so would need to be printed with %c, not %s. Otherwise it will attempt to dereference an address in the first 256 bytes of memory int he futile search for a string to print.

soru