• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

C Programming question, regarding structures/arrays

Yohhan

Senior member
Question on C for anyone who can answer.

I have an array of unsigned chars representing bytes called memory[], and a structure called data. I want to be able to assign the address of the first item in memory[], to the structure so subsequent indeces (sp?) will be represented in fields. I've tried the following:

data *Ins_RR = memory;
data *Ins_RR = &memory[0];

But the compiler complains about both with the following error message:
-- RR *Ins_RR = &memory[0];
-- ^
-- cp.c(113) : Error: need explicit cast to convert
-- from: char (*)[1]
-- to : struct Register_to_Register*
-- --- errorlevel 1

So I try to cast:
data *Ins_RR = (data)memory;

But then it tells me:
-- RR *Ins_RR = (RR)memory;
-- ^
-- cp.c(113) : Error: illegal cast
-- from: char (*)[1]
-- to : struct Register_to_Register
-- --- errorlevel 1


Can anyone show me how to do this properly? Thanks ahead.
 
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.
 
BFG10K:

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?



juiio:
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!
 
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.
 
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.
 
Originally posted by: Yohhan

juiio:
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?

 
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.
 
Back
Top