trouble with invalid indirection

Hello Everyone
( my condolences to the previous moderator of this forum )

I am trying to compile a program using extern struct array, the struct is in a header file and there a two files that I am using the struct with but I get an error : invalid indirection with my second source file orange2.c. I have tried changing the struct array to a typedef struct but I still get the same result. Here is my source code :

[code]//oranges.h

#ifndef ORANGES
#define ORANGES

#include

struct fruit
{
int oranges;
};

#endif[/code]


[code]/// --- orange1.c ---
#include
#include "oranges.h"

#define TREES 5

struct fruit orangetree[TREES];//array of 5 orangetree
extern void set_orange(void);
int treenumber = 0;

int main(void)
{
orangetree[0].oranges = 10;
set_orange();
return 0;
}

[/code]

[code]/// --- orange2.c ---

#include
#include "oranges.h"

extern orangetree;
extern treenumber;


void set_orange(void)
{
struct fruit harvest;
harvest = orangetree[treenumber];
printf("harvest = %d
",harvest.oranges);
}

[/code]

In orange2.c compiler complains about:
harvest = orangetree[treenumber];

This is my first time doing this and I have no idea why in the same file with main you can assign like structs to copy values but in a second C file it doesnt work with a local struct of fruit and extern struct of fruit which are the same.

Any help appreciated


K.J.Williams

Comments

  • Include proper type in the extern declarations in orange2.c
  • Thanks for helping, this is what I did to make it work:

    [code]
    /// --- orange2.c ---

    #include
    #include "oranges.h"

    //this:
    [b]#define TREES 5[/b]

    //and this:
    [b]extern fruit orangetree[TREES];[/b]

    extern int treenumber;


    void set_orange(void)
    {
    struct fruit harvest;
    harvest = orangetree[treenumber];
    printf("harvest = %d
    ",harvest.oranges);
    }

    [/code]


    K.J.Williams
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories