-1

i have a struct defined as

typedef struct  BSP_AREA {

    struct BSP_AREA  *otherBspArea;         /* Pointer to itself */
    long             BSPReturnCode;                
    short            sGroupNum;             /* default to 0 */
    char             filler4[6];            /* alignment bytes */
    unsigned char    byCobolWorkArea[20];   /* Cobol Work Area */
    // .... and lots of others fields

} * PBSP_PASS ;

So basically a BSP_AREA can hold a pointer reference to itself, to create a sort of link list.

In the code, i have a method createNode() which returns a BSP_AREA with method signature BSP_AREA createNode(). So in this method i just initialize a BSP_AREA and return it. Now after calling this method, to set the pointer it contains to itself i am doing this, but getting an error.

BSP_AREA cobolPassArea2 = createNode(data1);
cobolPassArea->otherBspArea = &cobolPassArea2;

How do i initialize the pointer otherBSP to the BSP_PASS_AREA returned from createNode()? Please dont advise to make createNode return a pointer, instead of a concrete BSP_AREA, as this is the requirement.

M.M
  • 138,810
  • 21
  • 208
  • 365
Saad Ahmed
  • 49
  • 5

2 Answers2

1
cobolPassArea->otherBspArea = &cobolPassArea2;

should be

cobolPassArea2.otherBspArea = &cobolPassArea2;

Notice that the -> has been changed to a ., in addition to adding the 2 to the variable name, on the left side.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
downhillFromHere
  • 1,967
  • 11
  • 11
  • 1
    I've edited in the points about what was changed. It would probably help to also explain the difference between the `->` and `.` operators there. – Andrew Barber Jun 09 '14 at 20:57
  • http://stackoverflow.com/questions/1238613/what-is-the-difference-between-the-dot-operator-and-in-c – macfij Jun 09 '14 at 21:24
-1
#include <cstdio>
#include <cstdlib>

typedef struct  BSP_AREA 
{

  struct BSP_AREA  *otherBspArea;   
  int something;
} *PBSP_PASS ;


struct BSP_AREA create()
{
   BSP_AREA x;
   x.something = 5;
   return x;
}

// this main you need to force the compiler to accept (-fpermissive on g++)

int main()
{
   BSP_AREA s;
   s.otherBspArea = &(create());
   printf("%d", s.otherBspArea->something);
}

// this one should work fine... but still, those requirements are a little odd

 int main()
{
   BSP_AREA s, r;
   r = create();
   s.otherBspArea = &r;
   printf("%d", s.otherBspArea->something);
}

Is that what you want? This compiles with a warning: "taking address of temporary"

vmp
  • 2,370
  • 1
  • 13
  • 17
  • It's not permitted to do `&(create())`, if your compiler generates an executable then it is a compiler extension. – M.M Jun 09 '14 at 23:39
  • i use g++... Well, c sort of let you do almost anything you want... It gave a warning tho (g++ -fpermissive file.cpp), and didn't compile without -fpermissive – vmp Jun 09 '14 at 23:42
  • @vmp: It didn't compile without permissive, because _it's not permitted to compile that code_. – Mooing Duck Jun 09 '14 at 23:52
  • yes I know that... that's why I posted 2 mains... And those requiriments are little bit weird... I wouldn't do either way – vmp Jun 10 '14 at 00:30