20

I have this struct in C Example:

typedef struct 
{
    const char * array_pointers_of_strings [ 30 ];
    // etc.
} message;

I need copy this array_pointers_of_strings to new array for sort strings. I need only copy adress.

while ( i < 30 )
{
   new_array [i] = new_message->array_pointers_of_strings [i]; 
   // I need only copy adress of strings
}

My question is: How to allocate new_array [i] by malloc() for only adress of strings?

4 Answers 4

20

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Yes, I need this but something on line new_array [i] = new_message->array_pointers_of_strings [i]; is wrong O_o My compilator gcc write me this warning: assgiment discards qualifiers from pointer target type
What if I user a double variable instead of 30 in malloc(30 * sizeof(char*))? Then new_array [i] syntax won't work. How do you access the element then?
8

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)), ...;

1 Comment

No. I have pointer new_message with array of pointers of strings. I need allocate char array for thirty adress strings by malloc. I dont know how to allocate new_array for copy adress of strings...
4
#include <stdio.h>
#include <stdlib.h>

#define ARRAY_LEN 2
typedef struct
{
    char * string_array [ ARRAY_LEN ];
} message;

int main() {
    int i;
    message message;
    message.string_array[0] = "hello";
    message.string_array[1] = "world";
    for (i=0; i < ARRAY_LEN; ++i ) {
        printf("%d %s\n",i, message.string_array[i]);
    }

    char ** new_message = (char **)malloc(sizeof(char*) * ARRAY_LEN);
    for (i=0; i < ARRAY_LEN; ++i ) {
        new_message[i] = message.string_array[i];
    }
    for (i=0; i < ARRAY_LEN; ++i ) {
        printf("%d %s\n",i, new_message[i]);
    }
}

Comments

1

is it mandatory for you to use Malloc? Because Calloc is the function in the C Standard Library which will make the job:

"The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory". (Source: here)

I'm just creating a hash table and it has an array of pointers to nodes, and a easy way to do it is this:

hash_table_t *hash_table_create(unsigned long int size){
hash_table_t *ptr = NULL;

ptr = malloc(sizeof(hash_table_t) * 1);
if (ptr == NULL)
    return (NULL);

ptr->array = calloc(size, sizeof(hash_node_t *)); #HERE
if (ptr->array == NULL)
    return (NULL);
ptr->size = size;
return (ptr);}

Hope it works for you guys!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.