Use of malloc, malloc example
/*malloc example
* random string generator
* malloc, free, random
*/
#include <iostream>
using namespace std;
int main() {
int i, n;
char *buffer; //pointer
printf("How long do you want the string.");
scanf("%d", &i);
buffer = (char*) malloc(i + 1);
if (buffer == NULL) {
exit(1);
}
for (int n = 0; n < i; n++) {
buffer[n] = rand() % 26 + 'a';
buffer[i] = '\0';
}
printf("Random string: %s\n", buffer);
free(buffer);
return 0;
}