Segmentation fault in C while using pointers -


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);   // segmentation error occurring here @ 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 occurring right after printf statement in for loop , when remove * both sides works answer wrong.

this insertion sort using pointers in c.

the problem is, said, right after printf():

*k=*(ptr+j) 

i didn't far looking @ right side. left side has problem: pointer not initialized, writing address cause trouble.

the right side has memory access too, but, after inspection looks might okay.


Comments

Popular posts from this blog

How do you change vbulletin's home page to the forums instead of the default (CMS or Activity Stream)? -

c++ - Troubles when compiling phash program -