Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamic memory allocation for pointer arrays

Tags:

I'm am trying to write a program that reads in a series of strings from a text file and stores these in an array of strings, dynamically allocating memory for each element. My plan was to store each string in an array using a pointer and then grow the array size as more were read in. I am having trouble to understand why my test code below is not working. Is this a workable idea?

char *aPtr; aPtr =(char*)malloc(sizeof(char));  aPtr[0]="This is a test";   printf("%s",aPtr[0]); 
like image 560
user2826534 Avatar asked Sep 28 '13 15:09

user2826534


People also ask

How do you dynamically allocate memory for array of pointers?

Dynamically allocating an array of M pointers Dynamically allocating an array of pointers follows the same rule as arrays of any type: type *p; p = malloc(m* sizeof *p); In this case type is float * so the code is: float **p; p = malloc(m * sizeof *p);

Can we dynamically allocate memory to pointer?

Dynamically allocated memory must be referred to by pointers. the computer memory which can be accessed by the identifier (the name of the variable). integer, 8, stored. The other is the “value” or address of the memory location.

Can we use dynamic memory allocation in array?

We can create an array of pointers also dynamically using a double pointer. Once we have an array pointers allocated dynamically, we can dynamically allocate memory and for every row like method 2.


2 Answers

In C a string is a char*. A dynamic array of type T is represented as a pointer to T, so for char* that would be char**, not simply a char* the way you declared it.

The compiler, no doubt, has issued some warnings about it. Pay attention to these warnings, very often they help you understand what to do.

Here is how you can start your testing:

char **aPtr; int len = 1; // Start with 1 string aPtr = malloc(sizeof(char*) * len); // Do not cast malloc in C aPtr[0] = "This is a test"; printf("%s",aPtr[0]); // This should work now. 
like image 94
Sergey Kalinichenko Avatar answered Oct 13 '22 00:10

Sergey Kalinichenko


char *str; //single pointer    

With this you can store one string.


To store array of strings you Need two dimensional character array

or else array of character pointers or else double pointer


char str[10][50]; //two dimensional character array 

If you declare like this you need not allocate memory as this is static declaration


char *str[10];  //array of pointers  

Here you need to allocate memory for each pointer

loop through array to allocate memory for each pointer

for(i=0;i<10;i++)  str[i]=malloc(SIZE); 

char **str;    //double pointer 

Here you need to allocate memory for Number of pointers and then allocate memory for each pointer .

str=malloc( sizeof(char *)*10); 

And then loop through array allocate memory for each pointer

for(i=0;i<10;i++)  str[i]=malloc(SIZE); 
like image 37
Gangadhar Avatar answered Oct 12 '22 23:10

Gangadhar