As I can understand from your assignment statement in while loop I think you need array of strings instead:

char** new_array;
new_array = malloc(30 * sizeof(char*)); // ignore casting malloc

Note: By doing = in while loop as below:

new_array [i] = new_message->array_pointers_of_strings [i];

you are just assigning address of string (its not deep copy), but because you are also writing "only address of strings" so I think this is what you wants.

Edit: waring "assignment discards qualifiers from pointer target type"

you are getting this warning because you are assigning a const char* to char* that would violate the rules of const-correctness.

You should declare your new_array like:

const  char** new_array;      

or remove const in declaration of 'array_pointers_of_strings' from message stricture.

Answer from Grijesh Chauhan on Stack Overflow
🌐
Cprogramming
cboard.cprogramming.com › c-programming › 177497-array-pointers-pointers-pointers.html
Array of pointers, pointers to pointers.
You can use *a to access the element at position 0 of this array or a[0], which is the same as writing *(a+0). When you declare a "double pointer", like: ... char **p; The variable p will store a memory address where is stored a memory address "pointing" to a char. When you do: ... char **p; p = malloc( sizeof( char * ) * 10 ); You are allocating a buffer big enough to acomodate 10 "memory addresses" and writing the address of this buffer to p.
🌐
George Washington University
www2.seas.gwu.edu › ~simhaweb › C › modules › module3 › module3.html
Module 3: Pointers, strings, arrays, malloc
Once we've allocated the array of pointers, each of those pointers must be made to point to an array of ... // Declare a pointer to an int. int *p; simply declares the pointer variable. The pointer may itself is invalid since it hasn't been assigned a value yet. ... Assign it an address of a known variable, e.g. ... Suppose the space assigned is at location 1023. The picture of memory after the allocation is: ... int main () { int *p; p = (int*) malloc (4); *p = 5; printf ("Int value: %d\n", *p); } This works if we happen to know that an
🌐
Quora
quora.com › Can-you-allocate-an-array-of-pointers-with-malloc-in-C
Can you allocate an array of pointers with malloc in C? - Quora
You can allocate an array of anything in C, using the malloc, calloc, or realloc functions in C. It’s up to you to tell these functions how much total memory you need in the block, and it’s up to you to perform appropriate assignments and/or ...
🌐
UCI
ics.uci.edu › ~dan › class › 165 › notes › memory.html
Pointers and Memory Allocation
Passing arrays of this type is simple, you declare the parameter the same way, as "int ***arrayname". Now the fun begins: how to allocate memory for a pointer-vector array. We get memory with the function · char *malloc( nbytes ); malloc returns a character pointer to a contiguous block of nbytes bytes, or a NULL pointer (NULL is defined in the library package <stdio.h>) if it cannot get the space.
🌐
Swarthmore College
cs.swarthmore.edu › ~newhall › unixhelp › C_arrays.html
Swarthmore
Allocate an array of arrays: allocate 1 array of N pointers to arrays, and allocate N M bucket array of values (on for each row). You could also allocate each column independently and have an array of column arrays. Depending on the method, the declaration and access methods differ.
🌐
University of Exeter
newton.ex.ac.uk › teaching › resources › jmr › p2.html
Home - Coding - LibGuides at University of Exeter
This guide provides you with access to self-study resources from Coding for Reproducible Research (CfRR), and information about Excode, a beginner-friendly Python bootcamp at the University of Exeter.
🌐
Programiz
programiz.com › c-programming › c-dynamic-memory-allocation
C Dynamic Memory Allocation Using malloc(), calloc(), free() & realloc()
And, the pointer ptr holds the address of the first byte in the allocated memory. The expression results in a NULL pointer if the memory cannot be allocated. The name "calloc" stands for contiguous allocation. The malloc() function allocates memory and leaves the memory uninitialized, whereas the calloc() function allocates memory and initializes all bits to zero.
Find elsewhere
Top answer
1 of 4
49

array is a slightly misleading name. For a dynamically allocated array of pointers, malloc will return a pointer to a block of memory. You need to use Chess* and not Chess[] to hold the pointer to your array.

Chess *array = malloc(size * sizeof(Chess));
array[i] = NULL;

and perhaps later:

/* create new struct chess */
array[i] = malloc(sizeof(struct chess));

/* set up its members */
array[i]->size = 0;
/* etc. */
2 of 4
23

There's a lot of typedef going on here. Personally I'm against "hiding the asterisk", i.e. typedef:ing pointer types into something that doesn't look like a pointer. In C, pointers are quite important and really affect the code, there's a lot of difference between foo and foo *.

Many of the answers are also confused about this, I think.

Your allocation of an array of Chess values, which are pointers to values of type chess (again, a very confusing nomenclature that I really can't recommend) should be like this:

Chess *array = malloc(n * sizeof *array);

Then, you need to initialize the actual instances, by looping:

for(i = 0; i < n; ++i)
  array[i] = NULL;

This assumes you don't want to allocate any memory for the instances, you just want an array of pointers with all pointers initially pointing at nothing.

If you wanted to allocate space, the simplest form would be:

for(i = 0; i < n; ++i)
  array[i] = malloc(sizeof *array[i]);

See how the sizeof usage is 100% consistent, and never starts to mention explicit types. Use the type information inherent in your variables, and let the compiler worry about which type is which. Don't repeat yourself.

Of course, the above does a needlessly large amount of calls to malloc(); depending on usage patterns it might be possible to do all of the above with just one call to malloc(), after computing the total size needed. Then you'd still need to go through and initialize the array[i] pointers to point into the large block, of course.

🌐
Quora
quora.com › How-does-allocating-an-array-with-a-type-pointer-work-using-malloc
How does allocating an array with a type pointer work using malloc? - Quora
Answer (1 of 7): In C and C++, arrays are stored contiguously in memory. That is, when you declare an array such as: [code]int array[5]; [/code]You are guaranteed that [code ]array[0][/code] is physically contiguous with [code ]array[1][/code] and that subsequent elements will also be contiguous...
🌐
GeeksforGeeks
geeksforgeeks.org › c language › dynamically-allocate-2d-array-c
How to dynamically allocate a 2D array in C? - GeeksforGeeks
January 10, 2025 - After creating an array of pointers, we can dynamically allocate memory for every row. ... #include <stdio.h> #include <stdlib.h> int main() { int r = 3, c = 4, i, j, count; int* arr[r]; for (i = 0; i < r; i++) arr[i] = (int*)malloc(c * sizeof(int)); // Note that arr[i][j] is same as *(*(arr+i)+j) count = 0; for (i = 0; i < r; i++) for (j = 0; j < c; j++) arr[i][j] = ++count; // Or *(*(arr+i)+j) = ++count for (i = 0; i < r; i++) for (j = 0; j < c; j++) printf("%d ", arr[i][j]); /* Code for further processing and free the dynamically allocated memory */ for (int i = 0; i < r; i++) free(arr[i]); return 0; }
🌐
HowStuffWorks
computer.howstuffworks.com › tech › computer software › programming
Pointers to Structures - The Basics of C Programming | HowStuffWorks
March 8, 2023 - The call to malloc allocates an array of whatever size you desire, and the pointer points to that array's first element. You can either index through the array pointed to by p using normal array ...
🌐
Medium
ksw2000.medium.com › how-to-use-malloc-and-free-in-c-9ffea31f56bd
How to Use malloc and free in C?. The article shows the concepts of… | by Kashiwa | Medium
August 22, 2023 - Nevertheless, we can use a system call to request our operating system (OS) to give us a contiguous space in the heap segment at runtime. malloc() returns the first pointer of the contiguous spaces.
🌐
O'Reilly
oreilly.com › library › view › understanding-and-using › 9781449344535 › ch04.html
4. Pointers and Arrays - Understanding and Using C Pointers [Book]
We start with a quick review of arrays and then examine the similarities and differences between array and pointer notation. Arrays can be created using malloc type functions.
🌐
Grinnell
eikmeier.sites.grinnell.edu › csc-161-fall-2023 › lists › readings › pointers-malloc.shtml
Pointers and Memory Allocation
Overall, the heap tends to grow ... as malloc is called, and the heap may contract toward the right with free. In the above code, the program declares two variables, arr1 and arr2. Since these are declared variables, both are stored on the run-time stack. arr1 is an array of 5 double values, so the entire block of these values is allocated on the run-time stack. The subsequent assignment statement places values within this array. arr2 is only a pointer to a double, ...
🌐
OverIQ
overiq.com › c-programming-101 › the-malloc-function-in-c
The malloc() Function in C - C Programming Tutorial - OverIQ.com
Before you can use the pointer you must cast it to appropriate type. So malloc() function is generally used as follows: ... where the p is a pointer of type (datatype *) and size is memory space in bytes you want to allocate.
🌐
cppreference.com
en.cppreference.com › w › c › memory › malloc
malloc - cppreference.com
On success, returns the pointer to the beginning of newly allocated memory. To avoid a memory leak, the returned pointer must be deallocated with free() or realloc(). On failure, returns a null pointer. ... #include <stdio.h> #include <stdlib.h> int main(void) { int *p1 = malloc(4*sizeof(int)); // allocates enough for an array of 4 int int *p2 = malloc(sizeof(int[4])); // same, naming the type directly int *p3 = malloc(4*sizeof *p3); // same, without repeating the type name if(p1) { for(int n=0; n<4; ++n) // populate the array p1[n] = n*n; for(int n=0; n<4; ++n) // print it back out printf("p1[%d] == %d\n", n, p1[n]); } free(p1); free(p2); free(p3); }