c - sorting strings with bubble sort algorithm -
this code stores number of words in integer variable , words in multidimensionnel string sorts words alphabetically using function. problem have in function call.
#include<stdio.h> #include<string.h> void sort(char*[50],int); int main () { int i,n=0; char s[50][10]; scanf("%d",&n);//scaning number of words for(i=0;i<=n;i++)//scaning words gets(s[i]); sort(s,n); for(i=0;i<=n;i++)//printing words sorted printf("%s\n",s[i]); } void sort(char*s[50],int n) { int i,j,cmp; char tmp[1][10]; //bubble sorting of words for(i=0; i<n; i++) for(j=0; j<n-1; j++) { cmp=strcmp(s[j],s[j+1]); if(cmp>0) { strcpy(tmp[0],s[j+1]); strcpy(s[j+1],s[j]); strcpy(s[j],tmp[0]); } } }
turn on warnings. char s[50][50]
not convertible char*
.
try this:
void sort(char(*)[50],int);
this tells compiler pass in pointer @ least 1 buffer of 50 characters. multidimensional arrays decay when passed functions.
and further rid of silly notion "char[]
same char*
", reason still taught everywhere, read this: http://c-faq.com/aryptr/aryptr2.html
Comments
Post a Comment