This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

C51: Pointer to array type

I have defined a type for objects we use. The details of the type should not be relevant for the users of the type, so I am "hiding" it behind a typedef. As it happens, this type is currently implemented as an array.
This is problematic when we want to pass around pointers to variables of this type.

Example:

#define OBJECT_SIZE 5

typedef char   object_t[OBJECT_SIZE];  // A type that happens to be an array

int main( void )
{
  object_t     my_object;
  object_t   * p_object;

  p_object = my_object;      // Compiler warning - pointer to different objects
                             // (Correct warning - array decays to pointer to char. )

  p_object = &my_object;     // Compiler warning - pointer to different objects
                             // Why this warning?
                             // According to knowledgeable people, this should be
                             // OK with respect to the C spec
  return 0;
}

So, the question is: how can I obtain the address of the variable in a way that will give me a pointer to the given type? That is, I need a pointer to array[OBJECT_SIZE] of char, not a plain pointer to char.

With kind regards
Asbj.S.

Parents

  • Not only does the programming language have such a thing as "pointer to array"; it even works exactly the same as it would for every other "pointer to something".

    That is my understanding too. But how do I get the pointer to the array?
    As seen from my original example (relevant part of it included below) C51 objects when I try to take the address of the array and assign it to a pointer to the type of the array, giving a warning about "pointers to different objects".

    With kind regards
    Asbj.S.

    typedef char   object_t[OBJECT_SIZE];  // A type that happens to be an array
    
    int main( void )
    {
      object_t     my_object;    // An object of the given type
      object_t   * p_object;     // And a pointer to an object of the same type
    
    
      p_object = &my_object;     // Compiler warning - pointer to different objects
    
    }
    

Reply

  • Not only does the programming language have such a thing as "pointer to array"; it even works exactly the same as it would for every other "pointer to something".

    That is my understanding too. But how do I get the pointer to the array?
    As seen from my original example (relevant part of it included below) C51 objects when I try to take the address of the array and assign it to a pointer to the type of the array, giving a warning about "pointers to different objects".

    With kind regards
    Asbj.S.

    typedef char   object_t[OBJECT_SIZE];  // A type that happens to be an array
    
    int main( void )
    {
      object_t     my_object;    // An object of the given type
      object_t   * p_object;     // And a pointer to an object of the same type
    
    
      p_object = &my_object;     // Compiler warning - pointer to different objects
    
    }
    

Children