I don't know what's happening here:
#include<stdio.h>
int main()
{
int i, j, *k, x,array[]={5,3,4,1,8,9,2,7,6,0};
int *ptr=array;
for(j=1;j<10;j++) {
printf("---------iteration %d--------------\n",j);
*k=*(ptr+j); // the segmentation error is occurring here at this line
printf("key=%d\n",*k);
i=j-1;
while( i>=0 && *k < *(ptr+i)) {
*(ptr+i+1)=*(ptr+i);
i--;
}
*(ptr+i+1) = *k;
printf("%d\n",*(ptr+i+1));
for( x=0;x<10;x++)
printf("%d,",*(ptr+x));
printf("\n");
}
for( i=0;i<10;i++)
printf("%d,",*ptr++);
printf("\n");
}
The error is occurring right after the printf statement in the for loop and when I remove the * from the both the sides it works but the answer is wrong.
This is an insertion sort using pointers in C.
(image by Swfung8
void main()in code posted here unless you like being shouted at for doing so. And please, please, please indent your code, 4 spaces per level (no tabs).__FILE__or__func__. Other names that start with an underscore are also mostly reserved to the implementation for a more restricted set of uses; again, stay clear. (C Standard, §7.1.3 Reserved Identifiers.) Types ending in_tare reserved to the implementation by POSIX. Again, tread with caution. I've not seen_inas a pervasive suffix; I'm not aware of any proscription on them.=%when you are on a curly bracket{or}. Note that when editing a question or answer, the{}button indents or outdents the selection by 4 spaces. A character in columns 1-4 forces an indens.