473,836 Members | 2,093 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 7830
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(t ag) + 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
5154
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 example, given: $data = serialize(array('4.50','0.00','0.00'));
4
8831
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 a * occurs in the string). This split function should allocate a 2D array of chars and put the split results in different rows. The listing below shows how I started to work on this. To keep the program simple and help focus the program the...
11
30587
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 char into a hex value and place that value into the byte. Thanks for your help
2
1817
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 specifies that 'the only valid values of a constant declarator constant expressions'. A constant expression is defined as 'an expression that can be fully evaluated at compile-time ... Therefore, the only possible values for constants of reference...
4
13415
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 be read and the string representation of the byte array has to be converted back to the original byte array and converted back to the original string - confused yet? in psuedo
2
3596
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 that is displayed in a text box. Once displayed, the text will be copied, transmitted and then pasted (all manually by humans) into a second utility where the string must then be reverse-engineered into the *original* byte array. The byte array will...
6
53732
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 do I convert it to: dim myStr as string = "11,22,33,44"
4
3399
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() (BinaryWriter.Write) from the string (String.ToCharArray), then use Stream.ToArray to convert everything to byte(). It works most of the time, but it happens that an error tells me something like "Additional information: Caractère de substitution faible trouvé...
25
5730
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 the constant 0x00010203040506070809 where array == 0x00, array == 0x01, etc. How do I do that. Obviously I can't use memcmp() with the 0x00010203040506070809 since the compiler will use that constant as an address and give me a segfault. Other...
0
9810
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, 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...
0
9656
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10819
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, 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...
0
10526
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 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...
0
9349
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5641
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5811
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4437
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
2
4000
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.