foralex / picoc

Automatically exported from code.google.com/p/picoc
0 stars 0 forks source link

Structures and arrays #108

Closed GoogleCodeExporter closed 8 years ago

GoogleCodeExporter commented 8 years ago
What steps will reproduce the problem?
1.Execute code:
struct TestStruct
{
int b;
};
typedef struct TestStruct TestStruct;

TestStruct*structs=malloc(sizeof(TestStruct)*200);
for(int i=0;i<200;i++)
    {
    structs[i].b=1;
    }

I get the error:
structs[i].b=65;
tempCode.c:16: can't use '.' on something that's not a struct or union:it's a 
int

Original issue reported on code.google.com by demonmit...@rambler.ru on 13 Aug 2010 at 7:37

GoogleCodeExporter commented 8 years ago
What is the expected result and where does this error occur in the code?

Original comment by Mj.Marcu...@gmail.com on 15 Dec 2010 at 10:01

GoogleCodeExporter commented 8 years ago

THE CORRECT FORM IS:
//-----------------------------------------------------
#define SIZE 200

typedef struct TestStruct TestStruct;

struct TestStruct
{
  int b;
};

TestStruct **structs = (TestStruct**)malloc( sizeof(TestStruct*) * SIZE );

// SET ALL NULL
for(int i=0;i<SIZE;i++)
  structs[i] = NULL;

for(int i=0;i<SIZE;i++)
    {
          structs[i] = malloc( sizeof(TestStruct) );
          structs[i].b = 1;
    }

//-----------------------------------------------------

gokernel
gokernel@hotmail.com

Original comment by goker...@gmail.com on 16 Dec 2010 at 12:52

GoogleCodeExporter commented 8 years ago
This seems to be a bug in expression parsing. The [] and . operators have equal 
operator precedence. This is leading to right-to-left evaluation by default, 
which is incorrect - it should be left-to-right for these operators.

I'm still thinking about how to fix this.

Original comment by zik.sale...@gmail.com on 7 Jan 2011 at 4:29

GoogleCodeExporter commented 8 years ago
Turns out it was because the expression stack wasn't being collapsed on a 
closing square bracket.

Original comment by zik.sale...@gmail.com on 14 Feb 2011 at 4:05

GoogleCodeExporter commented 8 years ago
Fixed in revision r520

Original comment by zik.sale...@gmail.com on 14 Feb 2011 at 6:06

GoogleCodeExporter commented 8 years ago
Verified

Original comment by zik.sale...@gmail.com on 14 Feb 2011 at 6:06