Read one character at a time (using getc(stdin)) and grow the string (realloc) as you go.
Here's a function I wrote some time ago. Note it's intended only for text input.
char *getln()
{
char *line = NULL, *tmp = NULL;
size_t size = 0, index = 0;
int ch = EOF;
while (ch) {
ch = getc(stdin);
/* Check if we need to stop. */
if (ch == EOF || ch == '\n')
ch = 0;
/* Check if we need to expand. */
if (size <= index) {
size += CHUNK;
tmp = realloc(line, size);
if (!tmp) {
free(line);
line = NULL;
break;
}
line = tmp;
}
/* Actually store the thing. */
line[index++] = ch;
}
return line;
}
Answer from cnicutar on Stack OverflowRead one character at a time (using getc(stdin)) and grow the string (realloc) as you go.
Here's a function I wrote some time ago. Note it's intended only for text input.
char *getln()
{
char *line = NULL, *tmp = NULL;
size_t size = 0, index = 0;
int ch = EOF;
while (ch) {
ch = getc(stdin);
/* Check if we need to stop. */
if (ch == EOF || ch == '\n')
ch = 0;
/* Check if we need to expand. */
if (size <= index) {
size += CHUNK;
tmp = realloc(line, size);
if (!tmp) {
free(line);
line = NULL;
break;
}
line = tmp;
}
/* Actually store the thing. */
line[index++] = ch;
}
return line;
}
You could have an array that starts out with 10 elements. Read input character by character. If it goes over, realloc another 5 more. Not the best, but then you can free the other space later.
How to dynamically allocate an array and add strings to it? (In C)
How to dynamically allocate string size according to user input in C? - Stack Overflow
c - Dynamically allocate an array of strings - Stack Overflow
Dynamically Allocate and Store Array of Strings in C - Stack Overflow
Videos
NOTE: My examples are not checking for NULL returns from malloc()... you really should do that though; you will crash if you try to use a NULL pointer.
First you have to create an array of char pointers, one for each string (char *):
char **array = malloc(totalstrings * sizeof(char *));
Next you need to allocate space for each string:
int i;
for (i = 0; i < totalstrings; ++i) {
array[i] = (char *)malloc(stringsize+1);
}
When you're done using the array, you must remember to free() each of the pointers you've allocated. That is, loop through the array calling free() on each of its elements, and finally free(array) as well.
The common idiom for allocating an N by M array of any type T is
T **a = malloc(N * sizeof *a);
if (a)
for (i = 0; i < N; i++)
a[i] = malloc(M * sizeof *a[i]);
As of the 1989 standard, you don't need to cast the result of malloc, and in fact doing so is considered bad practice (it can suppress a useful diagnostic if you forget to include stdlib.h or otherwise don't have a prototype for malloc in scope). Earlier versions of C had malloc return char *, so the cast was necessary, but the odds of you having to work with a pre-1989 compiler are pretty remote at this point. C++ does require the cast, but if you're writing C++ you should be using the new operator.
Secondly, note that I'm applying the sizeof operator to the object being allocated; the type of the expression *a is T *, and the type of *a[i] is T (where in your case, T == char). This way you don't have to worry about keeping the sizeof expression in sync with the type of the object being allocated. IOW, if you decide to use wchar instead of char, you only need to make that change in one place.
Okay so basically I have a function that takes in a string and counts the lowercase tokens in it. I need to make a function that then returns an array of the lowercase tokens. I would need to use malloc to allocate enough memory for such array but I donโt know how to go about doing so.
Once the array is allocated how would I put the token strings into the array?
Any help is appreciated thank you
char *s;
s = malloc(1024 * sizeof(char));
The above declares the variable s and assigns to it the address of a dynamically allocated chunk of memory large enough to hold 1024 chars. However, no error checking is performed, so s may be NULL at this point.
scanf("%s", s);
This reads data from the input stream until a whitespace is encountered (or input terminates) and stores it in the allocated chunk of memory whose address is contained in s , appending a '\0' after the data that is read. If there is more than 1023 bytes of data or if the previous malloc returned NULL, undefined behavior occurs. (This is bad.)
s = realloc(s, strlen(s) + 1);
This is an attempt to shrink the amount of memory being used so that s now points to a smaller chunk of memory which is just big enough to hold the string that was read. The +1 is in the 2nd argument to realloc because C stores strings as null terminated arrays, and it takes an extra character to store that terminator. This is possibly a memory leak, since realloc can return NULL, in which case the original memory is lost and the program hasn't stored that value anywhere so cannot free it.
Do not ever use "%s" in a call to scanf. If you have allocated N bytes for an array, use a width modifier of N - 1. Often this means dynamically generating the format string, but often it is as simple as:
char s[32]; scanf("%31s", s);
Always check the value returned by malloc, realloc, and scanf.
An alternative approach to shrinking the memory to the minimum needed for user input would be to allocate a second chunk of memory based on the size of the input, copy the string into it, then free the original.
We're just going to assume malloc and scanf worked as we meant them tto and not error check for the purposes of this answer.
char *s1 = malloc(1024);
scanf("%1023s", s1);
char *s2 = malloc(strlen(s1) + 1);
strcpy(s2, s1);
free(s1);
You might also use strdup.
Your allocation is not the best, and printf argument arr[i] expects a char* but you pass it an int (a char if you'd like).
Here is how you should do it, with comments:
Live demo
#include <stdio.h>
#include <stdlib.h> //for malloc
int main(){
int n;
int i;
printf("Give me a number:");
scanf("%d", &n);
//declare a variable of pointer to pointer to char
//allocate memory for the array of pointers to char,
//each one capable of pointing to a char array
char **arr = malloc(n * sizeof *arr);
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
//allocate memory for each individual char array
for(i = 0; i < n; i++){
arr[i] = malloc(40); //char size is always 1 byte
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
}
for (i = 0; i < n; i++){
printf("Give me a word: ");
//limit the size of read input to 39 charaters to avoid overflow,
//a nul character will be added by scanf
scanf("%39s", arr[i]);
}
for (i = 0; i < n; i++){
printf("%s\n", arr[i]);
}
for(int i = 0; i < n; i++){ //free the memory for each char array
free(arr[i]);
}
free(arr); //free array of pointers
return 0;
}
You can also do this with less code using a pointer to array of 40 chars, this will simplify the memory allocation and deallocation:
Sample with comments:
Live demo
#include <stdio.h>
#include <stdlib.h> //for malloc
int main(){
int n;
int i;
printf("Give me a number:");
scanf("%d", &n);
//declare a pointer to array of chars and
//allocate memory for all the char arrays
char (*arr)[40] = malloc(n * sizeof *arr);
if(arr == NULL){ //check for allocation errors
perror("malloc");
return EXIT_FAILURE;
}
for (i = 0; i < n; i++){
printf("Give me a word: ");
scanf("%39s", arr[i]);
}
for (i = 0; i < n; i++){
printf("%s\n", arr[i]);
}
free(arr); //free allocated memory
return 0;
}
This:
for(i=0;i<n;i++){
printf("Give me a word: ");
scanf("%s",&arr[i]);
}
is probably not what you want.
You probably want this instead:
for(i=0; i<n; i++){
printf("Give me a word: ");
scanf("%s", arr + i*40);
}
then later:
for(i=0; i<n; i++){
printf("%s", arr + i*40);
}
Remember that a string in C is just an array of characters.
Thus when defining char *arr, you are creating a single string. Had you done char **arr it would have been an array of strings, which is what you want.
However, I find that allocating/freeing arrays of arrays on the heap to be rather inconvenient, and prefer 'flattening' them into a single array.
This is exactly what you were doing with arr = malloc(n*40), but then later you treated this array of characters as an array of strings when you did this:
for(i=0; i<n; i++){
printf("Give me a word: ");
scanf("%s", &arr[i]);
}
Note that this is actually perfectly legal (scanf wanted a char* and you gave it one), but it's a logical error since you are giving it the n-th character when you wanted to give it the n-th array.
And oh yes, don't forget to free(arr) later.
I need a little help with a problem. Just a little guidance in the logic behind solving a problem.
I have to create a program that keeps asking for a string input, and storing it in an array, until a user inputs the string "quit". The only thing I can assume, is that the string will be no longer than 100 chars, but there is no limit on how many strings a user might input.
I think I would have do to this in a while loop to keep asking for an input, and then use strcmp() to see when a person enters "quit" in order to break the loop.
My problem is, I'm not too sure how to dynamically allocate an array.
I have this code to read lines from a file:
char ** readFromFile() {
FILE * fp;
char * line = NULL;
int lineCount = 0;
size_t len = 0;
ssize_t read;
fp = fopen("./wordsfile.txt", "r");
if(fp == NULL) {
return;
}
// getline(&buffer, &size, stdin);
while ((read = getline(&line, &len, fp)) != -1) {
lineCount++;
printf("Retrieved line of length %zu:\n", read);
printf("%s", line);
}
// close file stream
fclose(fp);
if(line) {
// if we have something in line - free it
free(line);
}
}
int main() {
char **wordBank = readFromFile();
}I want to store the lines read into an array of strings so I assume I'll use a char**. How do I go about allocating the memory for this when I don't know how many strings there are? I've thought about either searching through the document in advance looking for newline symbols and then allocating n number of strings in my char** where n is the number of symbols found. Or, could I possibly keep allocating new memory as more lines appear with realloc?