Thread: Pointer to struct problem

  1. #1
    Registered User
    Join Date
    Jul 2016
    Posts
    17

    Lightbulb Pointer to struct problem

    Dear everyone,
    My idea is create a pointer array point to multi structs as below:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stdint.h>
    typedef struct {
        volatile unsigned char mR;
        volatile int iB;
    }sensor;
    sensor uL0 = {
        10,
        1,
    };
    sensor uL1 = {
        10,
        1,
    };
    
    sensor uL2 = {
        10,
        1,
    };
    int main(void) {
        struct sensor *ptr[3];
        ptr[0] = &uL0;
        printf("%d",ptr[0].iB);
        return EXIT_SUCCESS;
    }
    I want to use pointer ptr[0] to point to uL0, but above code has problem.
    Please help to give me some suggestions to solve.
    Thank you so much!

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    What specifically is the problem?

    Are you getting warnings or errors? If so, post them.

    Also ask yourself - if you're typedef'ing your struct name, why are you using the keyword "struct" in the declaration on line 22?

  3. #3
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Line 23, assignment from incompatible pointer type [enabled by default]
    Line 24, dereferencing pointer to incomplete type.

  4. #4
    Registered User
    Join Date
    Jul 2016
    Posts
    17
    Yes, thank you I solved my problem by removing 'struct' def.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem returning struct pointer
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 11:53 AM
  2. problem with pointer to struct
    By M9000 in forum C Programming
    Replies: 3
    Last Post: 07-31-2009, 11:23 AM
  3. Pointer to struct problem
    By gavio in forum C Programming
    Replies: 10
    Last Post: 08-26-2008, 11:14 AM
  4. Struct pointer problem
    By fkheng in forum C Programming
    Replies: 10
    Last Post: 08-08-2003, 10:59 AM
  5. struct/pointer problem?
    By Jase in forum C Programming
    Replies: 17
    Last Post: 05-31-2003, 11:09 AM

Tags for this Thread