Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Structures and arrays #108

Closed
GoogleCodeExporter opened this issue Oct 14, 2015 · 6 comments
Closed

Structures and arrays #108

GoogleCodeExporter opened this issue Oct 14, 2015 · 6 comments

Comments

@GoogleCodeExporter
Copy link

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 [email protected] on 13 Aug 2010 at 7:37

@GoogleCodeExporter
Copy link
Author

What is the expected result and where does this error occur in the code?

Original comment by [email protected] on 15 Dec 2010 at 10:01

@GoogleCodeExporter
Copy link
Author


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
[email protected]

Original comment by [email protected] on 16 Dec 2010 at 12:52

@GoogleCodeExporter
Copy link
Author

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 [email protected] on 7 Jan 2011 at 4:29

  • Changed state: Started

@GoogleCodeExporter
Copy link
Author

Turns out it was because the expression stack wasn't being collapsed on a 
closing square bracket.

Original comment by [email protected] on 14 Feb 2011 at 4:05

@GoogleCodeExporter
Copy link
Author

Fixed in revision r520

Original comment by [email protected] on 14 Feb 2011 at 6:06

@GoogleCodeExporter
Copy link
Author

Verified

Original comment by [email protected] on 14 Feb 2011 at 6:06

  • Changed state: Verified

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant