2

I am trying to assign an int pointer to an int array which is a member of a structre. The structure is a member of another structure, which happens to be an array of structures. And this array of structures happens to be a member of another structure. And this last structure happens to be an element of an array of structures.

typedef struct s_ptxRowProperties
{
    int lastPlotValue[134];

} ptxRowProperties;

typedef struct s_ptxRow
{   
    ptxRowProperties PtxRowProperties;

} ptxRow;


typedef struct s_workSpace
{   
    ptxRow PtxRow[100];

} workSpace;

Edit: I allocate 1 of these behemoths like this: WorkSpace[n] = (workSpace *) calloc(1, sizeof(workSpace));

I have tried the following incantations, to no avail:

int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue)[0];
int *x= (&WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue)[0];
int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties->lastPlotValue)[0];
int *x=   WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue;
int *x= *(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue);
int *x= (*WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties->lastPlotValue);

I believe the hypothetical million monkeys in a room for 100 years will have composed Hamlet before they can create the correct form for this. Any ideas?

5
  • Does the compiler at least allow WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties? Commented Sep 8, 2012 at 2:02
  • 4
    How about editing your question and adding in the definitions of the structs, arrays, etc which are in play here? Thanks. Commented Sep 8, 2012 at 2:04
  • Why not just do it in steps so you don't confuse yourself? Commented Sep 8, 2012 at 2:05
  • 1
    Given those structures, it looks like your 4th example is right. I just confirmed it by making a test program here. Commented Sep 8, 2012 at 2:22
  • @CarlNorum, You are right, 4th example is right. Why I didn't think it was has to do with a lag in VS2010, where it sometimes puts the red squigglies underneath code that is in fact correct...It compiles fine, but I didn't try to compile thinking I had just another incorrect variation. Commented Sep 8, 2012 at 5:49

2 Answers 2

1

You probably want

int *x= &(WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue[0]);

This assumes that WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue[0] would reference the first element of the int array.

Sign up to request clarification or add additional context in comments.

2 Comments

If that would work then so would the OP's 4th example, right?
I was thinking that. It may be the case that he needs ->lastPlotValue instead. Or maybe he should just post the structs so we don't need to guess...
0

If WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue is the value you are interested in, than one would use & to get its address:

int *x = &WorkSpace[i]->PtxRow[ptxRowIndex].PtxRowProperties.lastPlotValue;

4 Comments

This answer was accepted, but is wrong given the OP's example structures. How is that possible?
Well, I'm clearly in over my head, but does this explain it? I allocate 1 of these behemoths like this: WorkSpace[n] = (workSpace *) calloc(1, sizeof(workSpace)); I must be inadvertently leaving out some bit of into, but I have no idea what. But the answer does compile and run correctly.
@user994179, I don't know how - maybe you have some warnings disabled? Using this answer you should get something long the lines of Warning - incompatible pointer types initializing 'int *' with an expression of type 'int (*)[134]'.
@Carl Norum, Well, you are absolutely right! I always compile with all warnings on (actually, warnings always treated as errors). But the above answer doesn't work. Long story, but it compiled and seemed to work, but it only worked because 1) I had a var name mix-up, which is why it compiled and 2) it worked for only 1 value in the array. I unchecked here and checked the correct answer below...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.