// Function main - execution starts here int main(void) { size_t pS_size = INIT_NSTR; // count of pS elements char **pS = calloc(pS_size, sizeof(char*)); // Array of string pointers if(!pS) { printf("Failed to allocate memory for string pointers.\n"); exit(1); } char **pTemp = NULL; // Temporary pointer size_t str_count = 0; // Number of strings read char *pStr = NULL; // String pointer printf("Enter one string per line. Press Enter to end:\n"); while((pStr = str_in()) != NULL) { if(str_count == pS_size) { pS_size += NSTR_INCR; if(!(pTemp = realloc(pS, pS_size*sizeof(char*)))) { printf("Memory allocation for array of strings failed.\n"); return 2; } pS = pTemp; } pS[str_count++] = pStr; } str_sort(pS, str_count); // Sort strings str_out(pS, str_count); // Output strings free_memory(pS, str_count); // Free all heap memory return 0; }
int main(int argc,char** argv) { char *pS[NUMP]; int count=0; printf("\nEnter successive lines, pressing Enter at at the end of" " each line.\nJust press Enter to end.\n"); for(count = 0; count < NUMP; count ++) if(!str_in(&pS[count])) break; str_sort(pS,count); str_out(pS,count); return 0; }
/* Function main - execution starts here */ int main(void) { char *pS[NUM_P]; /* Array of string pointers */ int count = 0; /* Number of strings read */ printf("\nEnter successive lines, pressing Enter at the end of" " each line.\nJust press Enter to end.\n"); for(count = 0; count < NUM_P ; count++) /* Max of NUM_P strings */ if(!str_in(&pS[count])) /* Read a string */ break; /* Stop input on 0 return */ str_sort( pS, count); /* Sort strings */ str_out( pS, count); /* Output strings */ return 0; }
int latest_exchange(int od, int frequency, char type, const char* path, char* filename) { DIR *dp; struct dirent *ep; sprintf(filename, "%s/%04d/",path,od); char namestart[20]; sprintf(namestart, "L%03d-%04d-%c-",frequency,od,type); char *pS[NUM_P]; /* Array of string pointers */ dp = opendir (filename); int i=0; if (dp != NULL) { while (ep = readdir (dp)) if (strncmp(ep->d_name, namestart,12) == 0) { puts (ep->d_name); pS[i] = (char*)malloc(strlen( ep->d_name) + 1); strcpy(pS[i], ep->d_name); /* DEBUG printf("%d\n",i); */ i++; } (void) closedir (dp); } else perror ("Couldn't open the directory"); str_sort( pS, i); /* Sort strings */ /* DEBUG int j=0; for (j=0 ; j<i;j++) { printf("%d\n",j); puts(pS[j]); } */ strcat(filename, pS[i-1]); return 0; }
/* Function main */ int main(void) { char *pS[NUM_P]; /* Array of string pointers */ int count = 0; /* Number of strings read */ printf("\nEnter succesive lines, pressing Enter at the end of" " each line.\nJust press Enter to end.\n"); for(count = 0; count < NUM_P; count++) /* Max of NUM_P strings */ if(!str_in(&pS[count])) /* Read a string */ break; /* Stop input on 0 return */ /* * The argument to the function str_in is &pS[i]. This is the address of pS[i] - in other words, a pointer to pS[i]. * And pS[i] is a pointer to char. Hence the argument type for that function is to be declared as char**. * In this case we want to modify the contents of the elements of pS, so if we used one "*" in the parameter type * definition, instead and just used pS[i] as the argument, the function receives whatever is contained in pS[i] * which is a memory address. * * Because all the work is done in the str_in() function, all that's necessary here is to continue the loop until you * get false returned from the function, which will cause the break to be executed, or until you fill up the pointer * array pS, which is indicated by count reaching the value NUM_P, thus ending the loop. The loop also counts how many * strings are entered in count. */ str_sort(pS, count); /* * Having safely stored all the strings, main() then calls the function str_sort() to sort the strings. The first * argument is the array name, pS, so the address of the first location of the array is transferred to the function. * The second argument is the count of the number of strings so the function will know how many there are to be * sorted. */ str_out(pS, count); return 0; }
static int scan_maildir(void) { long pos; long i; msg_bytes = 0; if (!str_truncate(&msg_filenames, 0)) return 0; msg_count = 0; if (!scan_dir("cur", &msg_filenames, &cur_count, max_cur_count)) return 0; if (!scan_dir("new", &msg_filenames, &new_count, max_new_count)) return 0; if (msg_count == 0) return 1; if (!str_sort(&msg_filenames, 0, -1, fn_compare)) return 0; del_count = 0; del_bytes = 0; if (msgs != 0) free(msgs); if ((msgs = malloc(msg_count * sizeof msgs[0])) == 0) return 0; for (i = pos = 0; i < msg_count; pos += strlen(msg_filenames.s+pos)+1, ++i) make_msg(&msgs[i], msg_filenames.s + pos); return 1; }