473,395 Members | 1,613 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,395 software developers and data experts.

assign constant string to BYTE array

hi,

How to initialize the data variable.

typedef struct
{
long len;
BYTE data[];
}tag;

Eventhough i know it is error, i tried like this
t.data = "text";

help me to solve this problem

urs,
Orange

Jul 12 '06 #1
5 7784
Orange said:
hi,

How to initialize the data variable.

typedef struct
{
long len;
BYTE data[];
}tag;
Since BYTE is not defined, this is not going to compile.
Eventhough i know it is error, i tried like this
t.data = "text";
typedef struct
{
long len;
const char *data;
} tag;

tag t;

t.data = "text";

will work, but it may not do what you want it to do. Since you don't specify
what you want it to do, I'll leave it there.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at above domain (but drop the www, obviously)
Jul 12 '06 #2

Orange wrote:
hi,

How to initialize the data variable.

typedef struct
{
long len;
BYTE data[];
}tag;

Eventhough i know it is error, i tried like this
t.data = "text";

help me to solve this problem

urs,
Orange
hi

first of all I want to know about BYTE type.

anyways as u are trying to initialize it with "text" so I assume it as
char pointer.
so ur problem is
typedef struct
{
long len;
char* data;
}tag;

now u want to initialize member variable data.
do u know y ur method is wrong......?????
bcos initialization can be done only when we are defineing the
variable.

so to initialize data member variable u need to perform
struct tag variable = { 12345, "text"};

this is the way to initialize variable.
Please let you clear urself that initialization can be done only at the
defining time of variable.

like
struct tag t;
t.data = "text";
is not initializing it is a sort of assigning, which is also wrong.
you can assign only address to pointer type variables with some
exceptions.

Jul 12 '06 #3
Orange wrote:
hi,

How to initialize the data variable.

typedef struct
{
long len;
BYTE data[];
Use "char" not "BYTE" if you mean "char". Use
"char data[SIZE]" if you want to store text in there
and "char *data" if you just want to reference text
from there. IIRC, c89 won't allow this (or is
supposed to issue a diagnostic for this).
}tag;

Eventhough i know it is error, i tried like this
t.data = "text";
If you really want to initialise, see Mr. Heathfields
response below; if you just want to assign (I'm guessing
from the example usage) then do this:

typedef struct
{
long len;
char data[20]; /* Use your own maximum size here */
} tag;

tag t;

strncpy (t.data, "Text", sizeof t.data - 1);
t.data [sizeof t.data - 1] = 0;
hth
goose,

Jul 12 '06 #4
On 12 Jul 2006 02:19:17 -0700, "Orange" <tr******@gmail.comwrote:
>hi,

How to initialize the data variable.

typedef struct
{
long len;
BYTE data[];
}tag;

Eventhough i know it is error, i tried like this
t.data = "text";

help me to solve this problem

urs,
Orange
Here's the best help I can offer - get a good book about the C
language and read it. I suggest K&R.

--
Al Balmer
Sun City, AZ
Jul 12 '06 #5
Orange schrieb:
hi,

How to initialize the data variable.

typedef struct
{
long len;
BYTE data[];
}tag;

Eventhough i know it is error, i tried like this
t.data = "text";
Assuming BYTE is a typedef to char,
and you got some data and have to store it into this struct (because
some API need it this way):

tag* createTagStruct(const char* data, long length)
{
tag* result = malloc(sizeof(tag) + length);

result->len = length;
memcpy(result->data, data, length);

return result;
}

Don't forget to free the pointer (returned by the function) after you
used it.
If you don't have a variable length data, but a constant or maximum
length, look at goose's posting.

--
Thomas
Jul 12 '06 #6

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

Similar topics

7
by: richbl | last post by:
Hello all, I have a question about unserializing a single array element from a serialized array. Can this be done, or must I first unserialize the array, and then access the element? For...
4
by: Simon Schaap | last post by:
Hello, I have encountered a strange problem and I hope you can help me to understand it. What I want to do is to pass an array of chars to a function that will split it up (on every location where...
11
by: Dan C | last post by:
Is there a routine in c# that will transform a string ie"Hello Mom" into a Byte array. I have found char cTmp = pString.ToCharArray(); But I have not been able to figure out how to convert a...
2
by: JJ Feminella | last post by:
This statement is legal C#: public const string day = "Sunday"; but this statement is not: public const string days = new string { "Sun", "Mon", "Tue" }; The C# Programmer's Reference...
4
by: David Bargna | last post by:
Hi I have a problem, I have a string which needs to be converted to a byte array, then have the string representation of this array stored in an AD attribute. This string attribute then has to...
2
by: Bryan | last post by:
Apologies if this is a noob question, but I've been struggling with this for quite a while... I'm trying to convert a byte array (encrypted authorization code) into a *screen-printable* string...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
4
by: ThunderMusic | last post by:
Hi, I have to go from Byte() to String, do some processing then reconvert the String to byte() but using ascii format, not unicode. I currently use a stream to write the char()...
25
by: galapogos | last post by:
Hi, I'm trying to compare an array of unsigned chars(basically just data without any context) with a constant, and I'm not sure how to do that. Say my array is array and I want to compare it with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.