0

I want to allocate memory for in_array as double array of length 100, how can i do it as a function in the simplest form.

typedef struct{
int message;
void*in_array;
}data_pack;

im trying like this to allocate memory for double array of 100 elements

struct data_pack* createpack()
{
struct data_pack* packet = (struct data_pack*)malloc(sizeof(struct data_pack));
packet->in_array= (double **)malloc(100 * sizeof(double));

return packet ;
}

is this correct?

Altris
  • 39
  • 3

2 Answers2

2

There are a couple of issues:

Because you used a typedef, you don't need to add 'struct' in front of your data type:

data_pack* packet = malloc(sizeof(data_pack));

And because you've declared in_array as a void ptr, you shouldn't cast it to a double:

packet->in_array= malloc(100 * sizeof(double));

(In fact, don't cast the result of malloc to anything..)

EDIT:

In response to the comment, if you wanted an array of type double you can change the declaration of data_pack to:

typedef struct{
int message;
double *in_array;
}data_pack;

Then treat it as an array:

packet->in_array[0] = 0.1;
packet->in_array[1] = 0.2;

and so on; but if you wanted an array of pointers to double then you change the declaration of in_array to:

typedef struct{
int message;
double **in_array;
}data_pack;

And, because you are now storing an array of pointers that point to doubles instead of doubles, you change your malloc of in_array to:

packet->in_array= malloc(100 * sizeof(double *));

Then, you can add pointers to doubles to that array, for example:

double foo = 0.1;
packet->in_array[0] = &foo;

EDIT 2

To copy the contents of one array to another, use memcpy:

#include <string.h>

double foo[100] = {0.1,0.2};
memcpy( packet->in_array, foo, sizeof(foo));

It works both ways:

double foo[100] = {};
memcpy( foo, packet->in_array, sizeof(foo));
secret squirrel
  • 802
  • 9
  • 13
  • of course, you could go the other way and remove the typedef, and declare in_array as a ptr to double – secret squirrel Jul 29 '20 at 11:37
  • what if if i need to assign a double array of 100 elements to packet->in_array, how can i do that – Altris Jul 29 '20 at 12:05
  • thank you so much, to make this clear , i have an array say, double data[100]; so how can i assign data to packet->in_array, can we do it like this packet->in_array=data; ? and how can i take data out of my struct? – Altris Jul 29 '20 at 12:33
  • **cough** **cough** quote: `As a clarification, note that I said "you don't cast", not "you don't need to cast".` – Shark Jul 29 '20 at 12:44
  • @Shark oooo sorry, you are perfectly correct, I have misquoted you; i'll change it :) – secret squirrel Jul 29 '20 at 12:51
  • hey, it wasn't my answer, i just looked it up and noticed the discrepancy :D – Shark Jul 29 '20 at 12:55
  • why i used void* is that, i can allocate memory of double if im going to assign a double to it, or i can allocate memory of int, if im assigning int to it. for one particular code i will be assigning only one type – Altris Jul 29 '20 at 13:07
  • you can do that, it's perfectly valid, but you have to be very careful in keeping track of whats what; it's much safer to do double *double_in_array; int * int_in_array; etc, and keep things seperate, because of the "what can go wrong, will go wrong" principle :) – secret squirrel Jul 29 '20 at 13:20
  • if i define in_array as void*in_array, and another double array as double data[100]; can i assign it as packet->in_array=data; or memcpy( packet->in_array, data, sizeof(data)); – Altris Jul 29 '20 at 13:45
  • you can do a cast in this case: packet->in_array = (void *)data; if you use this approach you dont malloc packet->in_array; you are only storing an address in packet – secret squirrel Jul 29 '20 at 13:50
  • sorry for repeated doubts, im not familiar with this, i assigned with packet->in_array = (void *)data, but when i try to print data on console, its showing "0". my printf statement is printf("data is %f",packet->(double*)in_array[0]); "im putting star but its not showing in comment" – Altris Jul 29 '20 at 14:02
  • Ah, ok, sorry try this, be careful about brackets: packet->in_array = data; printf("%f", ((double *)(packet->in_array))[1] ); – secret squirrel Jul 29 '20 at 14:21
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/218829/discussion-between-altris-and-secret-squirrel). – Altris Jul 29 '20 at 14:24
0

If you want in_array to be an array of double, then declare it as

double *in_array;

and allocate it as

packet->in_array = malloc( sizeof *packet->in_array * 100 );

Yes, malloc returns void *, but in C a void * can be assigned to any other object pointer type (and vice-versa) without needing a cast1. And, you were casting the result to "pointer to pointer to double", which doesn’t match what you said you wanted.


  1. This is not true in C++, but if you’re writing C++ you shouldn’t be using malloc anyway.
John Bode
  • 119,563
  • 19
  • 122
  • 198