开发者

runtime error in code

i have following code

#include <stdlib.h>
#include <stdio.h>
int sorter( const void *first_arg,const void* second_arg){
    int first=*(int *) first;
    int second =*(int*) second;
     if (first<second){
          return -1;
             }

     else if ( first==second){

          return 0;
             }
     else{

          return 1;
     }


}
int main(){
    int arr[10];
     int i;
     /*
     fi开发者_StackOverflow社区ll the array
     */
     int t=sizeof(arr)/sizeof(arr[0]);
      for (i=0;i<t;i++){
          arr[i]=t-i;
}
       qsort(arr,t,sizeof(int),sorter);
        for (int i=0;i<t;i++){
             printf("%d\n",arr[i]);


        }


}

but here is runtime errors according to debuging i got following

first   -858993460  int


first_arg   0x0015f738  const void *



second  -858993460  int


    second_arg  0x0015f74c  const void *

also

>   sorter_include.exe!sorter(const void * first_arg, const void * second_arg)  Line 4 + 0x20 bytes C++
msvcr100d.dll!qsort(void * base, unsigned int num, unsigned int width, int (const void *, const void *)* comp)  Line 151 + 0xb bytes    C
sorter_include.exe!main()  Line 31 + 0x17 bytes C++
sorter_include.exe!__tmainCRTStartup()  Line 555 + 0x19 bytes   C
sorter_include.exe!mainCRTStartup()  Line 371   C
kernel32.dll!77911174()     
[Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll]  
ntdll.dll!779fb3f5()    
ntdll.dll!779fb3c8()    

please help


It's one of those overlooking variable name problems.

You need to change:

int first=*(int *) first;
int second =*(int*) second;

to

int first=*(int *) first_arg;
int second =*(int*) second_arg;

At the beginning of the sorter function.


int sorter( const void *first_arg,const void* second_arg){
    int first=*(int *) first_arg;
    int second =*(int*) second_arg;
     if (first<second){
          return -1;
             }

*first or *second is a nonsensical typo - you need to use the args.


int first=*(int *) first;
int second =*(int*) second;

I think you meant

int first=*(int *) first_arg;
int second =*(int*) second_arg;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜