LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Closed Thread
  Search this Thread
Old 10-27-2003, 04:01 AM   #1
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Rep: Reputation: 15
Question a simple program but not worknig


this si s simple program . but it shows segmentation error when i run issort1.c pls tell m e why?

please save the following code as sort.h

/*sort.h*/

#ifndef SORT_H
#define SORT_H

int issort(void *data, int size, int esize, int (*compare)(const void *key1, const void *key2));

#endif

save the following code as issort.c

/*issort.c*/

#include <stdlib.h>
#include <string.h>

#include "sort.h"

int issort(void *data, int size, int esize, int (*compare)(const void *key1, const void *key2)) {

char *a = data;

void *key;

int i,
j;

if ((key = (char *)malloc(esize)) == NULL)
return -1;

for (j=0; j < size; j++) {

memcpy(key, &a[j * esize], esize);
i = j - 1;

while (i >= 0 && compare(&a[i * esize], key) > 0) {

memcpy(&a[(i + 1) * esize], &a[i * esize], esize);
i--;

}

memcpy(&a[(i + 1) * esize], key, esize);

}

free(key);

return 0;

}


save the following code as issort1.c or use it in the above program i.e. issort.c

/*insertion sort*/

#include "issort.c"

int data_compare(const void *key1, const void *key2)
{printf("HI");

if (key1 > key2)
return 1;

if (key1 == key2)
return 0;

if (key1 < key2)
return -1;

}


int main()
{

int data[100], size, esize, i,a;

printf("\nEnter the number of elements : ");
scanf("%d", &size);

printf("\nEnter the list of elements :\n");
for (i = 0; i < size; i++)
scanf("%d", &data[i]);

esize = malloc(sizeof(int));

issort(data, size, esize, data_compare);

printf("\nAfter insertion sort the list of elements :\n");
for (i = 0; i < size; i++)
printf("%d\n", data[i]);

}



please verify the errors i had made
 
Old 10-27-2003, 04:18 AM   #2
robartes
Member
 
Registered: Oct 2003
Location: Mechelen, BE
Distribution: Mandrake as base, most software hand rolled
Posts: 80

Rep: Reputation: 15
Re: a simple program but not worknig

Quote:
Originally posted by bprasanth_20
this si s simple program . but it shows segmentation error when i run issort1.c pls tell m e why?

esize = malloc(sizeof(int));
I suggest you take a debugger to this and figure out where the program segv's. The fact that you use the pointer you generate in the line above as an int in the issort function, for example, is very dubious indeed.
 
Old 10-27-2003, 04:44 AM   #3
bprasanth_20
Member
 
Registered: Oct 2003
Posts: 42

Original Poster
Rep: Reputation: 15
according tp the algorithm, we have to use esize as the size of each element in data. how shall we pass the esize data to issort from the main function. what should be the value or function of esize so that it is nothing but the size of each element entered in the array specified data whose size(i.e. number of elements) is size.
 
Old 10-27-2003, 06:38 AM   #4
robartes
Member
 
Registered: Oct 2003
Location: Mechelen, BE
Distribution: Mandrake as base, most software hand rolled
Posts: 80

Rep: Reputation: 15
Quote:
Originally posted by bprasanth_20
according tp the algorithm, we have to use esize as the size of each element in data. how shall we pass the esize data to issort from the main function. what should be the value or function of esize so that it is nothing but the size of each element entered in the array specified data whose size(i.e. number of elements) is size.
This should be a hint:

Code:
scanf("%d", &data[i]);
esize = sizeof(double);
 
Old 10-27-2003, 11:58 AM   #5
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
Use code tags, please...

Please, when posting code, please do this in between the [ code] and [ /code] tags.
Especially code with more than 3 lines. Personally, I tend to skip code-posts like this one, just because of the hard-to-read code listing.
 
Old 10-27-2003, 03:06 PM   #6
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
please follow link to later thread:

http://www.linuxquestions.org/questi...hreadid=109002
 
  


Closed Thread


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple program help! rohitkara Linux - General 1 05-24-2005 08:57 PM
simple c program liguorir Linux - Software 4 05-29-2004 06:22 AM
simple C program problem mined Programming 2 05-08-2004 05:42 AM
simple program bprasanth_20 Programming 5 10-29-2003 12:55 AM
Help with the following simple C program ..... purpleburple Programming 5 09-13-2002 10:52 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:54 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration