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;
} 11 2486
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
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.c olumbus.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
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.c olumbus.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
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********@yah oo.com) (cb********@wor ldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home .att.net> USE worldnet address!
"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>
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
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_t ype *p);
void FreeMsgs(msg_ty pe *p);
int main(void)
{
msg_type my_msg = {0};
AddMsgType(&my_ msg,56);
AddMsgType(&my_ msg,72);
puts("\tMessage s");
PrintMsgs(&my_m sg);
FreeMsgs(&my_ms g);
puts("\n\tNew Messages");
AddMsgType(&my_ msg,2);
AddMsgType(&my_ msg,1);
PrintMsgs(&my_m sg);
FreeMsgs(&my_ms g);
return 0;
}
int AddMsgType(msg_ type *p, unsigned value)
{
datatype *tmp;
if((tmp = realloc(p->data,
(p->payLen+1)*(siz eof *tmp))) == NULL)
return 0;
p->data = tmp;
p->data[p->payLen++].myData = value;
return 1;
}
void PrintMsgs(msg_t ype *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_ty pe *p)
{
free(p->data);
p->data = NULL;
p->payLen = 0;
return;
}
--
Al Bowers
Tampa, Fl USA
mailto: xa******@myrapi dsys.com (remove the x to send email) http://www.geocities.com/abowers822/
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 */
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? This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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. I know if I change char
name this will work, but I will waste alot of space. (I am
learning so I want to learn the best way)
How can I define a struct the allows variable length character strings
in the
|
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 runtime using
malloc.In this last member of the structer "int last" is not
|
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) struct, I am storing an
array of 27 pointers and a void pointer that can point to anything.
typedef struct trieNode
{
struct trieNode *children; // The children nodes
void *obj; // The object stored
} TrieNode;
|
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;
|
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;
| |
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 array of UDT.
|
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 this a managed struct by adding either the value struct
or ref struct I received a compile error stating that mixed types are not
supported. I understand this, but I don't know how to create a char array in
|
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 it
a try here...
Okay, here's the deal:
I am trying to read from unmanaged memory with a class type struct, this
|
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 structure is defined in the header(.h file) as shown below
struct validationResult
{
|
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 array. When I
try it with the following code i get these errors
1. Use of possibly unassigned field 'micData'
2. The out parameter 'rem' must be assigned to before control leaves
the current method
Could someone point out how to do this and what...
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |