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.
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.
This:
char** p = malloc(30 * sizeof(char*));
will allocate a buffer big enough to hold 30 pointers to char (or string pointers, if you will) and assign to p its address.
p[0] is pointer 0, p[1] is pointer 1, ..., p[29] is pointer 29.
Old answer...
If I understand the question correctly, you can either create a fixed number of them by simply declaring variables of the type message:
message msg1, msg2, ...;
or you can allocate them dynamically:
message *pmsg1 = malloc(sizeof(message)), *pmsg2 = malloc(sizeof(message)), ...;
Videos
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. */
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.
Posting comments as answer:
In C you should not to cast the return value of malloc. Please refer this post on SO for more information regarding why typecasting return value of malloc is not a good idea in C. And if for some reason you really really want to cast, it should be (int(*)[2]). (int(*)) is int *. The size passed to malloc looks fine (allocating size for 100 pointers to array of 2 ints). So you should be doing
int (*p)[2];
p=malloc(sizeof(int[2])*100);
Hope this helps!
It's not clear what you want here. If you want 100 int pairs, for example, arranged as an array of pointers to int (where each pointer points to exactly two ints), then you need to call malloc 100 times on 100 pointers to int, allocating two integers each time.
It just doesn't' make sense to "malloc a pointer to an array". You can malloc an array, and assign that address to a pointer though, or you can malloc an array of pointers. But what you've asked is not clear.