0

I'm having some trouble while working with pointers in C. It´s been a long time that I don´t use it so my head is a mess right now.

Function "newstage" creates a stage (just a struct) and it passes for "addintopollingtable". This function will just get the pointers and start to build a linked list.

I'm receiving "assignment from incompatible pointer type" in "(*currentStage)->next=s;" but I don´t know if this is the only problem.

First stage that I build must be pointed by "firstStage", the others must be pointed in "next".

"newstage" will be called many times..

stage_t * firstStage = NULL;
stage_t * currentStage = NULL;
int stagesCount = -1;

static void addintopollingtable (stage_t * s) { 
    stagesCount++;

    // First stage
    if (firstStage == NULL)
    {
        firstStage = s;
        currentStage = s;
        return;
    }

    (*currentStage)->next=s;
    currentStage = s;
}

static int newstage() { 
    stage_t * stage=NULL;

    (*stage)=malloc(sizeof(struct lstage_Stage));   
    (*stage)->instances=lstage_lfqueue_new();

    ...

    addintopollingtable(stage);
    return 1;
}

My stage_t:

typedef struct lstage_Stage * stage_t;

struct lstage_Stage {
   LFqueue_t instances;   
   LFqueue_t event_queue; 
   Pqueue_t ready_queue;  
   pool_t pool;      
   int init_time;     
   int processed;     
   int enabled;       
   char * env;
   size_t env_len;
   volatile unsigned int flags;
   volatile int priority;
   stage_t parent;
   stage_t next;
   int lock;
};

Thank you very much! =)

briba
  • 2,857
  • 2
  • 31
  • 59
  • 1
    Don't you think you should tell us the definition of `stage_t`? Also, a standalone program not using Lua would help. – John Zwinck Oct 08 '14 at 01:47
  • I included my stage_t @JohnZwinck =) Thank you! – briba Oct 08 '14 at 01:50
  • Guessing that `(*currentStage)->next` should be `currentStage->next`, but as John said, it's impossible to know without seeing the definition of `stage_t`. – user3386109 Oct 08 '14 at 01:52
  • I received this error including like this @user3386109 "request for member ‘next’ in something not a structure or union" ..and thanks for your help =) – briba Oct 08 '14 at 01:53
  • 1
    Style recommendation: never put a `*` in a typedef. `s` is a pointer to a pointer, but `next` is simply a pointer. First step is to remove the `*` from the typedef, and then explicitly declare which items are to be pointers. – user3386109 Oct 08 '14 at 01:57
  • It looks like you're confused about the semantics of `->`. You could say `(*currentStage).next=s;`. That would be fine, but (as K&R say) it's cumbersome and ugly. The shorthand is `currentStage->next=s;`. – Gene Oct 08 '14 at 01:58
  • 3
    As the code is currently written, `currentStage` is a `pointer-to-a-pointer`, so he has the syntax correct. The problem is that `next` is a plain old pointer, whereas `s` is a pointer-to-a-pointer. And all of this confusion comes from the `*` in the typedef. – user3386109 Oct 08 '14 at 02:03
  • 1
    See [`typedef` pointers — a good idea?](http://stackoverflow.com/questions/750178/typedef-pointers-a-good-idea/) — succinctly, it is not a good idea. – Jonathan Leffler Oct 08 '14 at 02:09
  • 1
    There is no way of easing in to this, so I'm not going to bother trying. *Don't bury indirection in typedefs*. There are only two places where it is warranted, and *neither* of those should be attempted until you're **fluent** in C indirection vernacular. (Library authors needing opaque "handle" types and formal callback function specifications are the two exceptions noted). @user3386109 is absolutely correct. Were you forced to spend the extra keystrokes typing those asterisks where needed, problems would have surfaced considerably earlier, and with considerably more *clarity*, before now. – WhozCraig Oct 08 '14 at 02:14

1 Answers1

0

I think the main problem is resulted from your typedef

 typedef struct lstage_Stage * stage_t;

This means stage_t is type which points to struct lstage_Stage. Therefore, I think the simplest way to correct the error is just declare the variable stage_t rather than stage_t *. Since they are pointers already.

stage_t firstStage = NULL;
stage_t currentStage = NULL;
int stagesCount = -1;

static void addintopollingtable (stage_t s) { 
    stagesCount++;

    // First stage
    if (firstStage == NULL)
    {
        firstStage = s;
        currentStage = s;
        return;
    }

    currentStage->next=s;
    currentStage = s;
}

static int newstage() { 
    stage_t stage=NULL;

    stage=malloc(sizeof(struct lstage_Stage));   
    stage->instances=lstage_lfqueue_new();

    ...

    addintopollingtable(stage);
    return 1;
}
JHT
  • 72
  • 5