Q: Question on C, for anyone who may have an array of chars answer.
I who called memory unsigned bytes [], and a data structure called. I want to be able to address the first item in the memory [] allocate the structure, so that future indeces (sp?) Will be represented in fields. Ive tried the following: *
data Ins_RR = memory;
data * Ins_RR u0026 = memory [0];
But the compiler complains about both the error message: 977 503 – RR * = u0026 Ins_RR memory [0];
-
^ – cp.c (113): Error: need explicit cast convert
– from char (*) [1] 977 503 – to: * struct Register_to_Register 977 503 – — errorlevel 1
So I try to throw:
data Ins_RR = * (data ) memory;
But tells me: 977 503 – RR * = Ins_RR (RR) memory,
-
^ – cp.c (113): Error: illegal cast
– from char (*) [1] 977 503 – to: struct Register_to_Register
– — errorlevel 1
Can me how good someone do this? Thanks in advance.
Best Answer: 1.
You can use new with both structures and classes the same you would for any default type in c++
struct Employee
{
int id;
string name;
};
Employee* x =new Employee[size]; //allocate
//do important stuff with x
cin>>x[n].name;
delete [] x;// free memory
2. I'm not sure what you mean but if you're talking about the last example you would access it the same way as you would access a regular array. Do need more detail on this…
The Three Money Raising Questions
Re:Strangely enough it's compiling now, and working. I have absolutely no idea what I changed. I think it's time to take a break…
Thanks for the help.
52 Model Questions For Bible Teachers.
Re:Originally posted by: guy
guy:
When using:
data *Ins_RR = (data *)memory;
It doesn't seem to let me access items in the structure, such as with:
Ins_RR->someobj
I get an error if I even try it. Why won't it let me access fields this way? How can I access fields?
Thanks!
You will need to post more code. Was it a compile time error or a runtime error?
50 Home Business Clarity Questions
Re:It's a class project and we're making something of a simulated machine, with 3 instruction formats. Three different structures represent each of these formats, each unsigned char field specifying a byte in the instruction.
I read data representing instructions (in hex) from a file, and put them into an array called memory. Then I look at the first byte and determine what kind of instruction I'm dealing with. Once I know, I need to be able to point the appropriate structure to that location of the array, so I can access consecutive indices (bytes) as fields from within the structure.
ie one of my structures looks like this:
struct Register_to_Register {
unsigned char OC; // first byte (opcode)
unsigned char RS; // source register
unsigned char RD; // destination register
} RR;
I have two other structures with varying numbers of fields, representing the other two instructions. If the array contains:
FA
A9
15
B9
11
for example, then RR.OC would need to refer to hex FA, RR.RS to hex A9, RR.RD to hex 15. Another structure representing a different instruction may contain two more fields, and then use the next two bytes as well.
The problem is, I can't figure out how to make this work in C if I can't point a structure at an array.
Thanks.
Cfa Study Notes, Practice Questions
Re:Will byteone and bytetwo be the first and second values of the array, if pointer is set to the array of unsigned characters?
Absolutely not, unless you specifically say byteone = memory [0] and bytetwo = memory [1]. However if you do this then why not simply use two normal variables and forget about the structure and the pointers?
I must confess, I don't understand why you need a structure or pointers at all.
Also I'd be extremely careful in using the (data *) cast because even if it compiles it'll likely produce meaningless results. Casting usually only works reliably when you cast from a stronger variable to a weaker one (eg char to int or int to float). Casting the other way can cause a loss of data and casting to arbitrary types that you've invented can produce a wide range of different results.
Gold Standard Trivia Pub Quiz Questions and Answers
Re:guy:
If I use,
struct data
{
unsigned char *pointer;
unsigned char byteone;
unsigned char bytetwo;
// etc
} data_instance;
Will byteone and bytetwo be the first and second values of the array, if pointer is set to the array of unsigned characters?
guy:
When using:
data *Ins_RR = (data *)memory;
It doesn't seem to let me access items in the structure, such as with:
Ins_RR->someobj
I get an error if I even try it. Why won't it let me access fields this way? How can I access fields?
Thanks!
24 Questions Equally Yoked in Thought and Deed
Re:data *Ins_RR = (data *)memory;
Meditation and Yoga Certification Program
Re:It won't work because your structure pointer is not the same type as the memory array. A data * pointer can only point to a data variable.
You need to make a pointer of the same type as memory inside the structure, eg:
struct data
{
unsigned char *pointer;
} data_instance;
And then
data_instance.pointer=memory;
Note that memory must already be defined as a static array, otherwise you'll need to use new if you want to create it dynamically as you assign it to the pointer in the structure.
I'm a little rusty but that should work OK.
0 Comments.