c - random character array sort -
i'm having trouble compiling program , i'm not sure if logic and/or syntax correct. i'm trying make array of size (defined user) of strings, each of 20 characters.
//melissa p. //university of massachusetts dartmouth //cis362 //2-6-2016 #include <stdio.h> #define min 0 //for random number generator #define max 51 //so can choose randomly letter given array in getrandomstring function int getnum(void); char * getrandomstring(void); int main (void) { int input = 0; int = 0; int j = 0; char word[20]; //placeholder random string result printf("how many strings want in array? "); //questions user amount of 20 character strings scanf("%d", &input); const char *array[input]; //pointer array of size defined user (i = 0; < input; i++) { *word = getrandomstring(); //gets 20 character string function array[i] = *word; //and puts in each space of array } (i = 0; < input; i++) { printf("%s\n", array[i]); //prints array } } int getnum() //function random number between 0 , 51. { int num; num = rand()% ((max + 1) - min) + min; return num; } char * getrandomstring(void) { char word[20]; //declares array random word go in. int num = 0; int = 0; char letterarray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; //array of lower case , upper case letters (i = 0; < 20; i++) { num = getnum(); //gets random number seed word[i] = letterarray[num]; //fills word array random letter } word[i] = '\0'; return *word; //returns string } the error i'm getting this: "assignment makes integer pointer without cast" happens in main , in getrandomstring method. thank you!
your code has many bugs, have corrected bugs, try:
#include <stdio.h> #include <string.h> #include <stdlib.h> #define min 0 //for random number generator #define max 51 //so can choose randomly letter given array in getrandomstring function int getnum(void); char * getrandomstring(void); int main (void) { int input = 0; int = 0; int j = 0; char* word; //placeholder random string result printf("how many strings want in array? "); //questions user amount of 20 character strings scanf("%d", &input); char **array = malloc(input * sizeof(char*)); //pointer array of size defined user (i = 0; < input; i++) { array[i] = malloc(21); } (i = 0; < input; i++) { word = getrandomstring(); //gets 20 character string function strcpy(array[i],word); //and puts in each space of array free(word); } (i = 0; < input; i++) { printf("%s\n", array[i]); //prints array } //free memory (i = 0; < input; i++) { free(array[i]); } free(array); } int getnum() //function random number between 0 , 51. { int num; num = rand()% ((max + 1) - min) + min; return num; } char * getrandomstring(void) { char* word = malloc(21); //declares array random word go in. int num = 0; int = 0; char letterarray[52] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}; //array of lower case , upper case letters (i = 0; < 20; i++) { num = getnum(); //gets random number seed word[i] = letterarray[num]; //fills word array random letter } word[i] = '\0'; return word; //returns string } you have study better pointers, have use string.h strcpy function , stdlib.h malloc.
Comments
Post a Comment