dynamically allocating my 2d array in c

cmptUser

Any hints on how I would dynamically allocate myArray so I can enter any amount of strings and it would store correctly.

int main()
{

char myArray[1][1]; //how to dynamically allocate the memory?
counter = 0;
char *readLine;
char *word;
char *rest;

printf("\n enter: ");
ssize_t buffSize = 0;
getline(&readLine, &buffSize, stdin);//get user input 

//tokenize the strings
while(word = strtok_r(readLine, " \n", &rest )) {

            strcpy(myArray[counter], word);
            counter++;
            readLine= rest;
}

//print the elements user has entered 
int i =0;
for(i = 0;i<counter;i++){
    printf("%s ",myArray[i]);
}
printf("\n");

}
BLUEPIXY

Use realloc like this:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void){
    char **myArray = NULL;
    char *readLine = NULL;
    size_t buffSize = 0;
    size_t counter = 0;
    char *word, *rest, *p;

    printf("\n enter: ");
    getline(&readLine, &buffSize, stdin);
    p = readLine;
    while(word = strtok_r(p, " \n", &rest )) {
        myArray = realloc(myArray, (counter + 1) * sizeof(*myArray));//check omitted
        myArray[counter++] = strdup(word);
        p = NULL;
    }
    free(readLine);
    for(int i = 0; i < counter; i++){
        printf("<%s> ", myArray[i]);
        free(myArray[i]);
    }
    printf("\n");
    free(myArray);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Dynamically allocating a 2D array in C

From Dev

c: issues when allocating 2d char array dynamically?

From Dev

C segmentation fault when dynamically allocating 2d array

From Dev

Dynamically allocating a 2D string array

From Dev

Allocating 2D array dynamically

From Dev

Dynamically Allocating 2D Arrays in C

From Dev

Dynamically allocating an array in a function in C

From Dev

C - Dynamically allocating array for stdin without realloc

From Dev

Dynamically allocating array of struct from file C

From Dev

Allocating an 2D array in C in heap instead of stack

From Dev

Allocating a 2d Array

From Dev

Dynamically allocating array explain

From Dev

Dynamically Allocating Array With Datafile

From Dev

Dynamically allocating an array of a struct

From Dev

Dynamically allocating threads in C

From Dev

Dynamically allocating 1D array of structs : Two Methods

From Dev

Why is C++ allocating such a large space in memory for my dynamic array?

From Dev

Dynamically input 2d character array in C++

From Dev

How to dynamically allocate a 2D array in C++?

From Dev

Dynamically creating a 2D array of pointers using malloc in C

From Dev

Matrix of chars (dynamically allocated 2D array) in c

From Dev

Allocating 2D array with pointer to fixed-size array

From Dev

Allocating memory to my stack dynamically (only if needed)

From Dev

Allocating memory to my stack dynamically (only if needed)

From Dev

dynamically allocating an int array inside a structure

From Dev

Trouble dynamically allocating memory for string array

From Dev

Allocating memory for array of pointers of int dynamically

From Dev

Allocating a 2D contiguous array within a function

From Dev

Dynamically allocating memory for changing array size starting with unknown size C++

Related Related

  1. 1

    Dynamically allocating a 2D array in C

  2. 2

    c: issues when allocating 2d char array dynamically?

  3. 3

    C segmentation fault when dynamically allocating 2d array

  4. 4

    Dynamically allocating a 2D string array

  5. 5

    Allocating 2D array dynamically

  6. 6

    Dynamically Allocating 2D Arrays in C

  7. 7

    Dynamically allocating an array in a function in C

  8. 8

    C - Dynamically allocating array for stdin without realloc

  9. 9

    Dynamically allocating array of struct from file C

  10. 10

    Allocating an 2D array in C in heap instead of stack

  11. 11

    Allocating a 2d Array

  12. 12

    Dynamically allocating array explain

  13. 13

    Dynamically Allocating Array With Datafile

  14. 14

    Dynamically allocating an array of a struct

  15. 15

    Dynamically allocating threads in C

  16. 16

    Dynamically allocating 1D array of structs : Two Methods

  17. 17

    Why is C++ allocating such a large space in memory for my dynamic array?

  18. 18

    Dynamically input 2d character array in C++

  19. 19

    How to dynamically allocate a 2D array in C++?

  20. 20

    Dynamically creating a 2D array of pointers using malloc in C

  21. 21

    Matrix of chars (dynamically allocated 2D array) in c

  22. 22

    Allocating 2D array with pointer to fixed-size array

  23. 23

    Allocating memory to my stack dynamically (only if needed)

  24. 24

    Allocating memory to my stack dynamically (only if needed)

  25. 25

    dynamically allocating an int array inside a structure

  26. 26

    Trouble dynamically allocating memory for string array

  27. 27

    Allocating memory for array of pointers of int dynamically

  28. 28

    Allocating a 2D contiguous array within a function

  29. 29

    Dynamically allocating memory for changing array size starting with unknown size C++

HotTag

Archive