fork download
  1. include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. void exit_if_null(void *ptr, const char *msg);
  7. int string_cmp(const void *x1, const void *x2);
  8.  
  9. int
  10. main(int argc, char *argv[]) {
  11. FILE *filename;
  12. char *buffer = NULL;
  13. char *file = NULL;
  14. int str_size = 1, word_len = 0;
  15. int ch, file_len = 0, file_size = 1;
  16. char **names = NULL;
  17. int name_count = 0, name_size = 1, i;
  18.  
  19. file = malloc(file_size * sizeof(*file));
  20. exit_if_null(file, "File Allocation");
  21.  
  22. if (argc < 2) {
  23. printf("Enter filename: ");
  24. while ((ch = getchar()) != '\n') {
  25. if (file_size == file_len) {
  26. file_size *= 2;
  27. file = realloc(file, file_size * sizeof(*file));
  28. exit_if_null(file, "Reallocation");
  29. }
  30. file[file_len++] = ch;
  31. }
  32. file[file_len] = '\0';
  33. filename = fopen(file, "r");
  34. } else {
  35. filename = fopen(argv[1], "r");
  36. }
  37.  
  38. if (filename) {
  39. names = malloc(name_size * sizeof(*names));
  40. exit_if_null(names, "String Array Allocation");
  41.  
  42. buffer = malloc((str_size+1) * sizeof(*buffer));
  43. exit_if_null(buffer, "Name Initial Allocation");
  44.  
  45. while ((ch = getc(filename)) != EOF) {
  46. if (isalpha(ch)) {
  47. if (str_size == word_len) {
  48. str_size *= 2;
  49. buffer = realloc(buffer, str_size * sizeof(*buffer));
  50. exit_if_null(buffer, "Reallocation");
  51.  
  52. }
  53. buffer[word_len++] = ch;
  54. }
  55.  
  56. if (ch == '\n') {
  57. buffer[word_len] = '\0';
  58. if (name_size == name_count) {
  59. name_size *= 2;
  60. names = realloc(names, name_size * sizeof(*names));
  61. exit_if_null(names, "Reallocation");
  62. }
  63. names[name_count] = malloc(strlen(buffer)+1);
  64. exit_if_null(names[name_count], "Allocation");
  65.  
  66. strcpy(names[name_count], buffer);
  67.  
  68. name_count++;
  69. str_size = 1;
  70. word_len = 0;
  71. }
  72. }
  73. free(buffer);
  74.  
  75. } else {
  76. fprintf(stderr, "Error reading from file!\n");
  77. exit(EXIT_FAILURE);
  78. }
  79.  
  80. qsort(names, name_count, sizeof(*names), string_cmp);
  81.  
  82. for (i = 0; i < name_count; i++) {
  83. printf("%s\n", names[i]);
  84. }
  85.  
  86. for (i = 0; i < name_count; i++) {
  87. free(names[i]);
  88. }
  89. free(names);
  90. free(file);
  91. return 0;
  92. }
  93.  
  94. int
  95. string_cmp(const void *x1, const void *x2) {
  96. return strcmp(*(char**)x1, *(char**)x2);
  97. }
  98.  
  99. void
  100. exit_if_null(void *ptr, const char *msg) {
  101. if (!ptr) {
  102. printf("Unexpected null pointer: %s\n", msg);
  103. exit(EXIT_FAILURE);
  104. }
  105. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
prog.c:1:9: error: expected '=', ',', ';', 'asm' or '__attribute__' before '<' token
 include <stdio.h>
         ^
In file included from prog.c:2:0:
/usr/include/stdlib.h:139:8: error: unknown type name 'size_t'
 extern size_t __ctype_get_mb_cur_max (void) __THROW __wur;
        ^
/usr/include/stdlib.h:466:22: error: unknown type name 'size_t'
 extern void *malloc (size_t __size) __THROW __attribute_malloc__ __wur;
                      ^
/usr/include/stdlib.h:468:22: error: unknown type name 'size_t'
 extern void *calloc (size_t __nmemb, size_t __size)
                      ^
/usr/include/stdlib.h:468:38: error: unknown type name 'size_t'
 extern void *calloc (size_t __nmemb, size_t __size)
                                      ^
/usr/include/stdlib.h:480:36: error: unknown type name 'size_t'
 extern void *realloc (void *__ptr, size_t __size)
                                    ^
/usr/include/stdlib.h:509:29: error: unknown type name 'size_t'
 extern void *aligned_alloc (size_t __alignment, size_t __size)
                             ^
/usr/include/stdlib.h:509:49: error: unknown type name 'size_t'
 extern void *aligned_alloc (size_t __alignment, size_t __size)
                                                 ^
/usr/include/stdlib.h:756:9: error: unknown type name 'size_t'
         size_t __nmemb, size_t __size, __compar_fn_t __compar)
         ^
/usr/include/stdlib.h:756:25: error: unknown type name 'size_t'
         size_t __nmemb, size_t __size, __compar_fn_t __compar)
                         ^
In file included from /usr/include/stdlib.h:760:0,
                 from prog.c:2:
/usr/include/i386-linux-gnu/bits/stdlib-bsearch.h:20:49: error: unknown type name 'size_t'
 bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
                                                 ^
/usr/include/i386-linux-gnu/bits/stdlib-bsearch.h:20:65: error: unknown type name 'size_t'
 bsearch (const void *__key, const void *__base, size_t __nmemb, size_t __size,
                                                                 ^
In file included from prog.c:2:0:
/usr/include/stdlib.h:765:34: error: unknown type name 'size_t'
 extern void qsort (void *__base, size_t __nmemb, size_t __size,
                                  ^
/usr/include/stdlib.h:765:50: error: unknown type name 'size_t'
 extern void qsort (void *__base, size_t __nmemb, size_t __size,
                                                  ^
/usr/include/stdlib.h:863:36: error: unknown type name 'size_t'
 extern int mblen (const char *__s, size_t __n) __THROW;
                                    ^
/usr/include/stdlib.h:867:34: error: unknown type name 'size_t'
      const char *__restrict __s, size_t __n) __THROW;
                                  ^
/usr/include/stdlib.h:874:8: error: unknown type name 'size_t'
 extern size_t mbstowcs (wchar_t *__restrict  __pwcs,
        ^
/usr/include/stdlib.h:875:32: error: unknown type name 'size_t'
    const char *__restrict __s, size_t __n) __THROW;
                                ^
/usr/include/stdlib.h:877:8: error: unknown type name 'size_t'
 extern size_t wcstombs (char *__restrict __s,
        ^
/usr/include/stdlib.h:878:38: error: unknown type name 'size_t'
    const wchar_t *__restrict __pwcs, size_t __n)
                                      ^
In file included from prog.c:3:0:
/usr/include/string.h:47:8: error: unknown type name 'size_t'
        size_t __n) __THROW __nonnull ((1, 2));
        ^
/usr/include/string.h:50:56: error: unknown type name 'size_t'
 extern void *memmove (void *__dest, const void *__src, size_t __n)
                                                        ^
/usr/include/string.h:66:42: error: unknown type name 'size_t'
 extern void *memset (void *__s, int __c, size_t __n) __THROW __nonnull ((1));
                                          ^
/usr/include/string.h:69:56: error: unknown type name 'size_t'
 extern int memcmp (const void *__s1, const void *__s2, size_t __n)
                                                        ^
/usr/include/string.h:96:48: error: unknown type name 'size_t'
 extern void *memchr (const void *__s, int __c, size_t __n)
                                                ^
/usr/include/string.h:133:39: error: unknown type name 'size_t'
         const char *__restrict __src, size_t __n)
                                       ^
/usr/include/string.h:141:9: error: unknown type name 'size_t'
         size_t __n) __THROW __nonnull ((1, 2));
         ^
/usr/include/string.h:147:57: error: unknown type name 'size_t'
 extern int strncmp (const char *__s1, const char *__s2, size_t __n)
                                                         ^
/usr/include/string.h:154:8: error: unknown type name 'size_t'
 extern size_t strxfrm (char *__restrict __dest,
        ^
/usr/include/string.h:155:40: error: unknown type name 'size_t'
          const char *__restrict __src, size_t __n)
                                        ^
/usr/include/string.h:285:8: error: unknown type name 'size_t'
 extern size_t strcspn (const char *__s, const char *__reject)
        ^
/usr/include/string.h:289:8: error: unknown type name 'size_t'
 extern size_t strspn (const char *__s, const char *__accept)
        ^
/usr/include/string.h:399:8: error: unknown type name 'size_t'
 extern size_t strlen (const char *__s)
        ^
/usr/include/string.h:451:33: error: unknown type name 'size_t'
 extern void __bzero (void *__s, size_t __n) __THROW __nonnull ((1));
                                 ^
In file included from /usr/include/string.h:635:0,
                 from prog.c:3:
/usr/include/i386-linux-gnu/bits/string2.h:945:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t __strcspn_c1 (const char *__s, int __reject);
                 ^
/usr/include/i386-linux-gnu/bits/string2.h:946:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t
                 ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strcspn_c1':
/usr/include/i386-linux-gnu/bits/string2.h:949:3: error: unknown type name 'size_t'
   size_t __result = 0;
   ^
/usr/include/i386-linux-gnu/bits/string2.h: At top level:
/usr/include/i386-linux-gnu/bits/string2.h:955:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t __strcspn_c2 (const char *__s, int __reject1,
                 ^
/usr/include/i386-linux-gnu/bits/string2.h:957:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t
                 ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strcspn_c2':
/usr/include/i386-linux-gnu/bits/string2.h:960:3: error: unknown type name 'size_t'
   size_t __result = 0;
   ^
/usr/include/i386-linux-gnu/bits/string2.h: At top level:
/usr/include/i386-linux-gnu/bits/string2.h:967:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t __strcspn_c3 (const char *__s, int __reject1,
                 ^
/usr/include/i386-linux-gnu/bits/string2.h:969:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t
                 ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strcspn_c3':
/usr/include/i386-linux-gnu/bits/string2.h:973:3: error: unknown type name 'size_t'
   size_t __result = 0;
   ^
/usr/include/i386-linux-gnu/bits/string2.h: At top level:
/usr/include/i386-linux-gnu/bits/string2.h:1021:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t __strspn_c1 (const char *__s, int __accept);
                 ^
/usr/include/i386-linux-gnu/bits/string2.h:1022:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t
                 ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strspn_c1':
/usr/include/i386-linux-gnu/bits/string2.h:1025:3: error: unknown type name 'size_t'
   size_t __result = 0;
   ^
/usr/include/i386-linux-gnu/bits/string2.h: At top level:
/usr/include/i386-linux-gnu/bits/string2.h:1032:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t __strspn_c2 (const char *__s, int __accept1,
                 ^
/usr/include/i386-linux-gnu/bits/string2.h:1034:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t
                 ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strspn_c2':
/usr/include/i386-linux-gnu/bits/string2.h:1037:3: error: unknown type name 'size_t'
   size_t __result = 0;
   ^
/usr/include/i386-linux-gnu/bits/string2.h: At top level:
/usr/include/i386-linux-gnu/bits/string2.h:1044:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t __strspn_c3 (const char *__s, int __accept1,
                 ^
/usr/include/i386-linux-gnu/bits/string2.h:1046:17: error: unknown type name 'size_t'
 __STRING_INLINE size_t
                 ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strspn_c3':
/usr/include/i386-linux-gnu/bits/string2.h:1049:3: error: unknown type name 'size_t'
   size_t __result = 0;
   ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strpbrk_c2':
/usr/include/i386-linux-gnu/bits/string2.h:1105:42: error: 'size_t' undeclared (first use in this function)
   return *__s == '\0' ? NULL : (char *) (size_t) __s;
                                          ^
/usr/include/i386-linux-gnu/bits/string2.h:1105:42: note: each undeclared identifier is reported only once for each function it appears in
/usr/include/i386-linux-gnu/bits/string2.h:1105:50: error: expected ';' before '__s'
   return *__s == '\0' ? NULL : (char *) (size_t) __s;
                                                  ^
/usr/include/i386-linux-gnu/bits/string2.h: In function '__strpbrk_c3':
/usr/include/i386-linux-gnu/bits/string2.h:1117:42: error: 'size_t' undeclared (first use in this function)
   return *__s == '\0' ? NULL : (char *) (size_t) __s;
                                          ^
/usr/include/i386-linux-gnu/bits/string2.h:1117:50: error: expected ';' before '__s'
   return *__s == '\0' ? NULL : (char *) (size_t) __s;
                                                  ^
prog.c: In function 'main':
prog.c:11:2: error: unknown type name 'FILE'
  FILE *filename;
  ^
prog.c:19:9: warning: implicit declaration of function 'malloc' [-Wimplicit-function-declaration]
  file = malloc(file_size * sizeof(*file));
         ^
prog.c:19:9: warning: incompatible implicit declaration of built-in function 'malloc'
prog.c:19:9: note: include '<stdlib.h>' or provide a declaration of 'malloc'
prog.c:23:3: warning: implicit declaration of function 'printf' [-Wimplicit-function-declaration]
   printf("Enter filename: ");
   ^
prog.c:23:3: warning: incompatible implicit declaration of built-in function 'printf'
prog.c:23:3: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c:24:16: warning: implicit declaration of function 'getchar' [-Wimplicit-function-declaration]
   while ((ch = getchar()) != '\n') {
                ^
prog.c:27:12: warning: implicit declaration of function 'realloc' [-Wimplicit-function-declaration]
     file = realloc(file, file_size * sizeof(*file));
            ^
prog.c:27:12: warning: incompatible implicit declaration of built-in function 'realloc'
prog.c:27:12: note: include '<stdlib.h>' or provide a declaration of 'realloc'
prog.c:33:14: warning: implicit declaration of function 'fopen' [-Wimplicit-function-declaration]
   filename = fopen(file, "r");
              ^
prog.c:33:12: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   filename = fopen(file, "r");
            ^
prog.c:35:12: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
   filename = fopen(argv[1], "r");
            ^
prog.c:45:16: warning: implicit declaration of function 'getc' [-Wimplicit-function-declaration]
   while ((ch = getc(filename)) != EOF) {
                ^
prog.c:45:35: error: 'EOF' undeclared (first use in this function)
   while ((ch = getc(filename)) != EOF) {
                                   ^
prog.c:49:15: warning: incompatible implicit declaration of built-in function 'realloc'
      buffer = realloc(buffer, str_size * sizeof(*buffer));
               ^
prog.c:49:15: note: include '<stdlib.h>' or provide a declaration of 'realloc'
prog.c:60:14: warning: incompatible implicit declaration of built-in function 'realloc'
      names = realloc(names, name_size * sizeof(*names));
              ^
prog.c:60:14: note: include '<stdlib.h>' or provide a declaration of 'realloc'
prog.c:76:3: warning: implicit declaration of function 'fprintf' [-Wimplicit-function-declaration]
   fprintf(stderr, "Error reading from file!\n");
   ^
prog.c:76:3: warning: incompatible implicit declaration of built-in function 'fprintf'
prog.c:76:3: note: include '<stdio.h>' or provide a declaration of 'fprintf'
prog.c:76:11: error: 'stderr' undeclared (first use in this function)
   fprintf(stderr, "Error reading from file!\n");
           ^
prog.c:80:2: warning: implicit declaration of function 'qsort' [-Wimplicit-function-declaration]
  qsort(names, name_count, sizeof(*names), string_cmp);
  ^
prog.c:83:3: warning: incompatible implicit declaration of built-in function 'printf'
   printf("%s\n", names[i]);
   ^
prog.c:83:3: note: include '<stdio.h>' or provide a declaration of 'printf'
In file included from /usr/include/string.h:635:0,
                 from prog.c:3:
prog.c: In function 'string_cmp':
prog.c:96:9: error: unknown type name 'size_t'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: 'size_t' undeclared (first use in this function)
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c:96:9: error: expected expression before 'const'
  return strcmp(*(char**)x1, *(char**)x2);
         ^
prog.c: In function 'exit_if_null':
prog.c:102:9: warning: incompatible implicit declaration of built-in function 'printf'
         printf("Unexpected null pointer: %s\n", msg);
         ^
prog.c:102:9: note: include '<stdio.h>' or provide a declaration of 'printf'
prog.c: In function 'string_cmp':
prog.c:97:1: warning: control reaches end of non-void function [-Wreturn-type]
 }
 ^
stdout
Standard output is empty