473,405 Members | 2,300 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

struct array

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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].payLen.data[k].myData = yyyy;
}
Nov 14 '05 #1
11 2454
Magix wrote:
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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].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[i].payLen.data[k].myData
is CLEARLY wrong because payLen is a word, and you're trying to
use it as a structure.
mymsg[i].data[k].myData
is correct.

That's about all I can find.
HTH

dbtid
Nov 14 '05 #2
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[i].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" <db***@dev.null.com> wrote in message
news:sg*****************@fe2.columbus.rr.com...
Magix wrote:
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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].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[i].payLen.data[k].myData
is CLEARLY wrong because payLen is a word, and you're trying to
use it as a structure.
mymsg[i].data[k].myData
is correct.

That's about all I can find.
HTH

dbtid

Nov 14 '05 #3
Also, how can I empty my mymsg[ ] and data[ ], in order to receive new data
? Assign ot NULL ?

*mymsg = NULL;
*data = NULL; ?
"dbtid" <db***@dev.null.com> wrote in message
news:sg*****************@fe2.columbus.rr.com...
Magix wrote:
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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].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[i].payLen.data[k].myData
is CLEARLY wrong because payLen is a word, and you're trying to
use it as a structure.
mymsg[i].data[k].myData
is correct.

That's about all I can find.
HTH

dbtid

Nov 14 '05 #4
Magix wrote:

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[i].data[k].myData


..... snip ....

Another topposter. To be ignored until he learns.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #5
kal
"Magix" <ma***@asia.com> wrote in message news:<40**********@news.tm.net.my>...
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[i].payLen = xxxx
What are "i" and "xxxx" ?
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].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[i].payLen = 1234;
if (mymsg[i].payLen > 160)
mymsg[i].payLen = 160;

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

return 0;
}

</code>
Nov 14 '05 #6
Magix wrote:
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.

--
Er*********@sun.com

Nov 14 '05 #7


Magix wrote:
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[i].myData);
return ;
}

void FreeMsgs(msg_type *p)
{
free(p->data);
p->data = NULL;
p->payLen = 0;
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapidsys.com (remove the x to send email)
http://www.geocities.com/abowers822/

Nov 14 '05 #8
Magix wrote:
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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].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[i].payLen = xxxx; /* mha: added ';' */
for (k = 0; k < mymsg[i].payLen; k++)
mymsg[i].data[k].myData = yyyy; /* mha: fixed */

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


Nov 14 '05 #9
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?
Nov 14 '05 #10
Magix wrote:
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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++)
{
mymsg[i].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?
--
Joe Wright mailto:jo********@comcast.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #11
Something that calls itself Magix wrote:
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[i].payLen = xxxx
for (k=0; k< mymsg[i].payLen; k++) {
mymsg[i].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
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stuart Norris | last post by:
Dear Readers, I am attempting to initialise a struct contiaing a dynamic character string. In the example below I am trying to initialise the name field so that my struct does not waste space. ...
19
by: Geetesh | last post by:
Recently i saw a code in which there was a structer defination similar as bellow: struct foo { int dummy1; int dummy2; int last }; In application the above array is always allocated at...
20
by: fix | last post by:
Hi all, I feel unclear about what my code is doing, although it works but I am not sure if there is any possible bug, please help me to verify it. This is a trie node (just similar to tree nodes)...
2
by: beetle | last post by:
Hello, I'm storing data in several different binary tree's. The root node is located in a struct containing general data about the tree. struct lnode { char *fname; int nentry;
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
5
by: Cybertof | last post by:
Hello, Is it possible to convert a VB6 Array of Struct to a C# Array Of Struct ? The test context is a C# application calling a VB6 ActiveX DLL Function using UDT (User Defined Type) and...
3
by: GrkEngineer | last post by:
I recently had to use someone's struct from a native app to receive data over Udp. The struct has a array member which looked like this: struct sensorHdr{ char sName; }; When I tried to make...
4
by: DaHool | last post by:
Hi there !!! I browsed around the Internet in search for a solution of a little difficult problem i have in VB.NET.... However, i cannot find a suitable anwser anywhere, so i thought i'll give...
9
by: AM | last post by:
Hi, I have a C++ Dll that has a function that is being exported as shown below extern "C" __declspec(dllexport) validationResult __stdcall _validateData(double dataToMat, int time); A...
11
by: abhiM | last post by:
I have a struct that has an array in it. I need to assign space to the array in a function and pass the corresponding struct by reference to another function so that it can store values into the...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.