Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<string.h>
- #include<stdlib.h>
- int compare(const void* a, const void* b)
- {
- const char *ia = (const char *)a;
- const char *ib = (const char *)b;
- return strcmp(ia, ib);
- }
- main()
- {
- /*str is a pointer that points to pointers that points to strings*/
- char **str;
- /*i will denote how much memory we should take for now.
- Start from 1*/
- int i=1;
- str=(char**)malloc(sizeof(char*)*i);
- while(1)
- {
- str[i-1]=(char*)malloc(sizeof(char)*21);
- gets(str[i-1]);
- if(strlen(str[i-1])==0 && str[i-1][0]==0)
- break;
- //Increment i to account for more memory.
- i++;
- /*make the memory size larger again*/
- str=(char**)realloc(str, sizeof(char*)*i);
- }
- printf("%d\n", sizeof(str));
- qsort(str, i-1, sizeof(str[0]),compare);
- for(int itr=0; itr<i-1; itr++)
- {
- printf("%s\n", str[itr]);
- }
- free(str);
- }
Advertisement
Add Comment
Please, Sign In to add comment