Magix said:
Also, how can I empty my mymsg[ ] and data[ ], in order to receive new data
? Assign ot NULL ?
*mymsg = NULL;
*data = NULL; ?
From reading your posts, it is really a guess, but, I believe that
you want a struct, type msg_type, that has a member, type datatype *,
that points to an array of datatype. Also, a member, type word, that
indicates the count of the number elements of the array.
You can make your struct
typedef struct
{
word payLen;
datatype *data;
}
Usage:
#include <stdio.h>
#include <stdlib.h>
typedef unsigned word;
typedef struct
{
word myData;
} datatype;
typedef struct
{
word payLen;
datatype *data;
} msg_type;
int AddMsgType(msg_type *p, unsigned value);
void PrintMsgs(msg_type *p);
void FreeMsgs(msg_type *p);
int main(void)
{
msg_type my_msg = {0};
AddMsgType(&my_msg,56);
AddMsgType(&my_msg,72);
puts("\tMessages");
PrintMsgs(&my_msg);
FreeMsgs(&my_msg);
puts("\n\tNew Messages");
AddMsgType(&my_msg,2);
AddMsgType(&my_msg,1);
PrintMsgs(&my_msg);
FreeMsgs(&my_msg);
return 0;
}
int AddMsgType(msg_type *p, unsigned value)
{
datatype *tmp;
if((tmp = realloc(p->data,
(p->payLen+1)*(sizeof *tmp))) == NULL)
return 0;
p->data = tmp;
p->data[p->payLen++].myData = value;
return 1;
}
void PrintMsgs(msg_type *p)
{
unsigned i;
for(i = 0; i < p->payLen; i++)
printf("p->data[%u].myData = %u\n",i,p->data
.myData);
return ;
}
void FreeMsgs(msg_type *p)
{
free(p->data);
p->data = NULL;
p->payLen = 0;
return;
}