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 Overflow
๐ŸŒ
Quora
quora.com โ€บ How-do-you-properly-dynamically-allocate-a-string-C-string-pointers-memory-management-malloc-development
How to properly dynamically allocate a string (C, string, pointers, memory management, malloc, development) - Quora
Answer (1 of 4): You have 3 choices: malloc, calloc, and realloc. malloc [code ]void *malloc(size_t size);[/code] malloc gives you raw bytes of memory, uninitialized, for any kind of storage you might need.
Discussions

How to dynamically allocate an array and add strings to it? (In C)
Important question: Do you want to return an array of characters, or an array of strings? More on reddit.com
๐ŸŒ r/learnprogramming
8
1
February 17, 2022
How to dynamically allocate string size according to user input in C? - Stack Overflow
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 ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
c - Dynamically allocate an array of strings - Stack Overflow
In this way the allocation isn't only for one string? I understand better the first code, can you explain me better how the second one allocate memory for all n words and not for only a single one as i think? How can i read this: "char (*arr)[40]"? And what's the difference with "char *arr [40]"? 2020-08-20T05:43:46.197Z+00:00 ... @Boninissimo, it's a bit more crypric but it's +- easy to ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
Dynamically Allocate and Store Array of Strings in C - Stack Overflow
I would like to dynamically allocate and store an array containing words of a certain length determined by the user. I am not sure that I completely understand how it works. My code doesn't seem to create this array and only stores the last string that it encounters. More on stackoverflow.com
๐ŸŒ stackoverflow.com
Top answer
1 of 5
32

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.

2 of 5
14

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.

๐ŸŒ
RIT
se.rit.edu โ€บ ~swen-250 โ€บ slides โ€บ instructor-specific โ€บ Rabb โ€บ C โ€บ 06-C-Memory-Alloc-Strings.pdf pdf
Personal Software Engineering Memory Management in C (Dynamic Strings)
The dynamic strings (p1 and p2) are in dynamically allocated space. Dynamically allocated space should eventually be freed or memory ... Example: suppose we only want the concatenated result in p3. Then: ... Reference to deallocated space!
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ how to dynamically allocate an array and add strings to it? (in c)
r/learnprogramming on Reddit: How to dynamically allocate an array and add strings to it? (In C)
February 17, 2022 -

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

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ how-to-create-a-dynamic-array-of-strings-in-c
How to Create a Dynamic Array of Strings in C? - GeeksforGeeks
July 23, 2025 - To create a dynamic array of strings in C, we can use the concept of double pointer and dynamic memory allocation. The double pointer is the pointer that stores the memory address of another pointer.
๐ŸŒ
Cprogramming
cboard.cprogramming.com โ€บ c-programming โ€บ 113508-dynamically-allocated-strings.html
dynamically allocated strings??
March 19, 2009 - - LEAF Questions posted by these ... cmake buildsystem generator. ... Cool. Thank you. I'll post again if I run into any problems ... char *string = malloc(sizeof(char) * 100); ....
Find elsewhere
๐ŸŒ
Distortionbyte
distortionbyte.com โ€บ computer science โ€บ programming languages โ€บ c language โ€บ dynamic string allocation in c
Dynamic String Allocation in C
November 22, 2025 - ... The function takes as input a size specified by the size parameter which is expressed in bytes. It returns a void * pointer to the allocated memory block. In case the function fails to allocate the requested memory, it will return the NULL value. Using malloc to allocate a string is very simple.
๐ŸŒ
Ubaldo Acosta Soto
ictdemy.com โ€บ c-language โ€บ dynamic-memory-allocation โ€บ dynamic-strings-and-structures-in-the-c-language
Lesson 4 - Dynamic strings and structures in the C language
In this tutorial, we'll learn to allocate strings dynamically exactly that long as we need them. Then we'll focus on passing structures by reference.
Top answer
1 of 2
4
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.

2 of 2
0

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.

Top answer
1 of 2
2

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;
}
2 of 2
1

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.

๐ŸŒ
Diveintosystems
diveintosystems.org โ€บ book โ€บ C2-C_depth โ€บ strings.html
2.6.1. C's Support for Statically Allocated Strings (Arrays ...
When dynamically allocating space to store a string, itโ€™s important to remember to allocate space in the array for the terminating '\0' character at the end of the string.
๐ŸŒ
TutorialKart
tutorialkart.com โ€บ c-programming โ€บ how-to-dynamically-allocate-memory-for-strings-in-c
How to Dynamically Allocate Memory for Strings in C - Examples
February 20, 2025 - In this example, we will allocate memory dynamically for a string using the malloc() function, store a string in it, and print the string. ... #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *str; // Allocating memory ...
๐ŸŒ
YouTube
youtube.com โ€บ portfolio courses
Dynamically Allocate Memory For An Array Of Strings | C Programming Example - YouTube
How to dynamically allocate memory for an array of strings using C. Source code: https://github.com/portfoliocourses/c-example-code/blob/main/dynamic_array_...
Published ย  January 21, 2022
Views ย  49K
๐ŸŒ
Sankalandtech
sankalandtech.com โ€บ Tutorials โ€บ C โ€บ dynamic-memory-string-reverse-c.html
a program in C that dynamically allocates memory for a string and reverses the string in place.
Program Step by Step Description 1.Use malloc() function characteristic to dynamically allocate memory for a string. 2.Read the enter string from the user using scanf() or fgets() function. 3.Use strlen() string function to calculate the length of the string.
๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ dynamically allocated array of strings?
r/C_Programming on Reddit: Dynamically Allocated Array of strings?
April 9, 2018 -

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.

๐ŸŒ
Reddit
reddit.com โ€บ r/c_programming โ€บ what's the best way to dynamically allocate memory when reading strings from a file?
r/C_Programming on Reddit: What's the best way to dynamically allocate memory when reading strings from a file?
November 24, 2021 -

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?

๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c language โ€บ storage-for-strings-in-c
Storage for Strings in C - GeeksforGeeks
July 23, 2025 - Suppose you have "CppNuts"-> a string that you seriously want to allocated on heap and to do that you need to override new operator which will always allocate memory on heap.
๐ŸŒ
Wikiversity
en.wikiversity.org โ€บ wiki โ€บ C_Programming โ€บ Strings
C Programming/Strings - Wikiversity
There is no string type in C. Instead we have arrays or constants, and we can use them to store sets of characters. The string "Hello World!" can be stored like in the example below ยท This creates an array of chars so that s[0] is 'H', s[1] is 'e', etc. In addition, because C allows us to ...