struct array

M

Magix

Hi,

what is wrong with following code ?

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct
{
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;
}
 
D

dbtid

Magix said:
Hi,

what is wrong with following code ?

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct
{
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;
}


Well,

a) what's "word"?
b) datatype is used before it's defined.

typedef int word;
typedef struct { word myData; } datatype;
typedef struct { word payLen; datatype data[160]; } msg_type;
msg_type mymsg[10];

Now,
mymsg.payLen.data[k].myData
is CLEARLY wrong because payLen is a word, and you're trying to
use it as a structure.
mymsg.data[k].myData
is correct.

That's about all I can find.
HTH

dbtid
 
M

Magix

word is 16 bit unsigned integer.
My purpose is to have msg_type has a member to hold an int array of data
e.g mymsg.data[k].myData

so that I can have:
mymsg[0].data[0].myData = xxx
mymsg[0].data[1].myData = yyy
mymsg[0].data[2].myData = zzz
mymsg[0].data[3].myData = www
... so on

mymsg[1].data[0].myData = aaa
mymsg[1].data[1].myData = bbb
mymsg[1].data[2].myData = ccc
... so on




dbtid said:
Magix said:
Hi,

what is wrong with following code ?

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct
{
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;
}


Well,

a) what's "word"?
b) datatype is used before it's defined.

typedef int word;
typedef struct { word myData; } datatype;
typedef struct { word payLen; datatype data[160]; } msg_type;
msg_type mymsg[10];

Now,
mymsg.payLen.data[k].myData
is CLEARLY wrong because payLen is a word, and you're trying to
use it as a structure.
mymsg.data[k].myData
is correct.

That's about all I can find.
HTH

dbtid
 
M

Magix

Also, how can I empty my mymsg[ ] and data[ ], in order to receive new data
? Assign ot NULL ?

*mymsg = NULL;
*data = NULL; ?


dbtid said:
Magix said:
Hi,

what is wrong with following code ?

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct
{
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;
}


Well,

a) what's "word"?
b) datatype is used before it's defined.

typedef int word;
typedef struct { word myData; } datatype;
typedef struct { word payLen; datatype data[160]; } msg_type;
msg_type mymsg[10];

Now,
mymsg.payLen.data[k].myData
is CLEARLY wrong because payLen is a word, and you're trying to
use it as a structure.
mymsg.data[k].myData
is correct.

That's about all I can find.
HTH

dbtid
 
C

CBFalconer

Magix said:
word is 16 bit unsigned integer.
My purpose is to have msg_type has a member to hold an int array of data
e.g mymsg.data[k].myData


..... snip ....

Another topposter. To be ignored until he learns.
 
K

kal

Magix said:
what is wrong with following code ?

It is easier to state what is not wrong.
typedef struct
Ok.

{

Ok. Spelled correctly, I think.
word payLen;
datatype data[160];

God only knows what these two lines mean.
What are "word" and "datatype"?
} msg_type;
Ok.

msg_type mymsg[10];
Ok.

typedef struct
{
word myData;
} datatype;

No idea what "word" means!
mymsg.payLen = xxxx


What are "i" and "xxxx" ?
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;


Is "payLen" a structure/union that contains an array
of structures/unions named "data" each of which in
turn contain an element named "myData"?

What is "yyyy"?

Ok.


The following code may compile but what it does is
not clear to me.

<code>

typedef unsigned short word;

typedef struct
{
word myData;
} datatype;

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

int main(void)
{
int i, k;

i = 1234;
i = (i < 0) ? 0 : ((i > 9) ? 9 : i);

mymsg.payLen = 1234;
if (mymsg.payLen > 160)
mymsg.payLen = 160;

for (k=0; k< mymsg.payLen; k++)
{
mymsg.data[k].myData = 5678;
}

return 0;
}

</code>
 
E

Eric Sosman

Magix said:
Hi,

what is wrong with following code ?
[snipped; see up-thread]

The biggest problem with this code is that it
isn't the code that's giving you trouble. Instead,
you've posted some pseudo-code that you think is
"something like" the code that's giving you trouble.
But the pseudo-code is riddled with errors -- some
of them may be the sources of your difficulty, but
others were certainly introduced by the process of
"pseudo-codification." I'm not going to waste time
trying to figure out which is which in such a mess.

Post the *actual* code that's giving you trouble,
after snipping it down to the smallest possible
complete demonstration of your problem. Remember:
since you don't understand the error (or you wouldn't
be asking about it), you are in a poor position to
decide what features of your code are and are not
relevant.
 
A

Al Bowers

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;
}
 
M

Martin Ambuhl

Magix said:
Hi,

what is wrong with following code ?

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct
{
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;
}


/* mha:
* "what's wrong?"
* (1) using a type with no meaning in C ('word')
* (2) using the type 'datatype' before it is defined.
* (3) using the variables 'i', 'xxxx', 'yyyy', and 'k' with no declaration
* (4) failing to have a main function
* (5) omitting ';' between statements
* (6) treating the 'data' member of a msg_type struct as if it were a
* member of a struct 'payLen' which is in fact a scalar member of
* the msg_type struct.
* (7) making no effort to learn C, resulting in nonsense code.
*/

typedef unsigned long word; /* mha: added */

typedef struct
{
word myData;
} datatype; /* mha: moved up to here */

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

int main(void)
{ /* mha: added */
unsigned i = 0, k; /* mha: added */
unsigned xxxx = 0, yyyy = 0; /* mha: added */
mymsg.payLen = xxxx; /* mha: added ';' */
for (k = 0; k < mymsg.payLen; k++)
mymsg.data[k].myData = yyyy; /* mha: fixed */

return 0; /* mha: added */
} /* mha: added */
 
M

Martin Ambuhl

Magix topposted:
Also, how can I empty my mymsg[ ] and data[ ], in order to receive new data
? Assign ot NULL ?

*mymsg = NULL;
*data = NULL; ?

Learn not to toppost.
And you can overwrite existing values. There is no need to "empty"
first. What time between 9 and 10 AM today did you decide to start
learning to program?
 
J

Joe Wright

Magix said:
Hi,

what is wrong with following code ?

typedef struct
{
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct
{
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++)
{
mymsg.payLen.data[k].myData = yyyy;
}


Is this a contest? Is there a prize? How many errors must I
identify? Is it C? Where's 'int main()'? Who do you think you are?
 
E

E. Robert Tisdale

Something said:
What is wrong with following code?
> cat code.c
typedef struct msg_type {
word payLen;
datatype data[160];
} msg_type;

msg_type mymsg[10];

typedef struct datatype {
word myData;
} datatype;

mymsg.payLen = xxxx
for (k=0; k< mymsg.payLen; k++) {
mymsg.payLen.data[k].myData = yyyy;
}
> gcc -Wall -std=c99 -pedantic -c code.c
code.c:2: error: syntax error before "word"
code.c:2: warning: no semicolon at end of struct or union
code.c:3: warning: type defaults to `int' \
in declaration of `data'
code.c:3: error: ISO C forbids data definition \
with no type or storage class
code.c:4: error: syntax error before '}' token
code.c:4: warning: type defaults to `int' \
in declaration of `msg_type'
code.c:4: error: ISO C forbids data definition \
with no type or storage class
code.c:6: error: syntax error before "mymsg"
code.c:6: warning: type defaults to `int' \
in declaration of `mymsg'
code.c:6: error: ISO C forbids data definition \
with no type or storage class
code.c:9: error: syntax error before "word"
code.c:9: warning: no semicolon at end of struct or union
code.c:10: warning: type defaults to `int' \
in declaration of `datatype'
code.c:10: error: ISO C forbids data definition \
with no type or storage class
code.c:12: error: `i' undeclared here (not in a function)
code.c:12: error: syntax error before '.' token
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top