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

C182 error kicking my butt

Hopefully this makes sense. I've cut down the actual code to make this short.

I have a series of strings as shown:


char code Num1[] =  {"One  "};
char code Num2[] =  {"Two  "};
char code Num3[] =  {"Three"};


I then have an array of pointers that have all the address of the start of those strings:

code char code *CHNLTable[] = { Num1, Num2, Num3};


I then have a pointer in a function that I want to assign to an element in the array and I get a C182 "Pointer to different objects" warning.

void func(void){	
  unsigned int *myptr;
   myptr = &CHNLTable;

   ...code that uses myptr

}

I don't understand the warning AND the code seems to work fine. Can someone explain to me the dumb thing I'm doing??

Parents
  • Oh...Just ask...

    Here's what I came up with.

    #include <stdio.h>
    #include <reg51.h>
    
    char code Num1[] =  {"One  "};
    char code Num2[] =  {"Two  "};
    char code Num3[] =  {"Three"};
    
    // Num1, 2, and 3 are arrays (in code) of characters (strings).
    
    code char code *CHNLTable[] = { Num1, Num2, Num3};
    
    // CHNLTable is an array of pointers (in code) to characters (in code)
    // So, each element takes 2 bytes (because the pointer is a code pointer)
    
    void main (void)
    {
    unsigned int *myptr;
    // myptr is a pointer to an unsigned int (a generic, 3-byte pointer)
    
    myptr = &CHNLTable;
    // I'm not sure what myptr should point to...
    // the strings or the pointers to the strings...
    // so, I'm inclined to say this is probably wrong...
    // It yields this warning:
    // *** WARNING C182 IN LINE 21 OF MAIN.C: pointer to different objects
    
    
    // Setup the serial port (stolen from HELLO.C)
    SCON  = 0x50;
    TMOD |= 0x20;
    TH1   = 221;
    TR1   = 1;
    TI    = 1;
    
    
    // Here's one of my interpretations of this...
      {
      char code *jons_ptr;
    
    // jons_ptr is a pointer to a character in code space
    // so, the sizeof jons_ptr is 2
    
      jons_ptr = CHNLTable[0];   // jons_ptr points to the string "One  "
      printf ("jons_ptr = %s\n", jons_ptr); // This prints it
    
      jons_ptr = CHNLTable[1];   // jons_ptr points to the string "Two  "
      printf ("jons_ptr = %s\n", jons_ptr); // This prints it
    
    // If you are trying to get a pointer to an individual string...
    // this is the way to do it...
      }
    
    // Here's another interpretation...
      {
      char code * code *jons_dptr;
    
    // jons_dptr is a ptr to a ptr (in code space) to a char in code space
    // I think this is more like what we really want to get!
    // the sizeof jons_dptr is 2
    
      jons_dptr = CHNLTable;
    // jons_dptr points to the ptr to the string "One  "
    
      printf ("*jons_dptr = %s\n", *jons_dptr);
    // *jons_dptr points to the string "One  "
    
      jons_dptr++;
    // Increment jons_dptr so that it points to the ptr to the string "Two  "
    
      printf ("*jons_dptr = %s\n", *jons_dptr);
      }
    
    while (1);  
    }
    
    // Viola!!!
    

    I tested this with uVision2 V6.21 and got the results that I expected.

    The real problem in the original code (which I think I kept intact) is a clear definition of what you want to point to (the string or the table).

    If you use a table and you want to get a pointer to it, you need to remember that [] => *. In other words a pointer to an array of pointers is really a double pointer (or a pointer to a pointer to a thing).

    Jon

Reply
  • Oh...Just ask...

    Here's what I came up with.

    #include <stdio.h>
    #include <reg51.h>
    
    char code Num1[] =  {"One  "};
    char code Num2[] =  {"Two  "};
    char code Num3[] =  {"Three"};
    
    // Num1, 2, and 3 are arrays (in code) of characters (strings).
    
    code char code *CHNLTable[] = { Num1, Num2, Num3};
    
    // CHNLTable is an array of pointers (in code) to characters (in code)
    // So, each element takes 2 bytes (because the pointer is a code pointer)
    
    void main (void)
    {
    unsigned int *myptr;
    // myptr is a pointer to an unsigned int (a generic, 3-byte pointer)
    
    myptr = &CHNLTable;
    // I'm not sure what myptr should point to...
    // the strings or the pointers to the strings...
    // so, I'm inclined to say this is probably wrong...
    // It yields this warning:
    // *** WARNING C182 IN LINE 21 OF MAIN.C: pointer to different objects
    
    
    // Setup the serial port (stolen from HELLO.C)
    SCON  = 0x50;
    TMOD |= 0x20;
    TH1   = 221;
    TR1   = 1;
    TI    = 1;
    
    
    // Here's one of my interpretations of this...
      {
      char code *jons_ptr;
    
    // jons_ptr is a pointer to a character in code space
    // so, the sizeof jons_ptr is 2
    
      jons_ptr = CHNLTable[0];   // jons_ptr points to the string "One  "
      printf ("jons_ptr = %s\n", jons_ptr); // This prints it
    
      jons_ptr = CHNLTable[1];   // jons_ptr points to the string "Two  "
      printf ("jons_ptr = %s\n", jons_ptr); // This prints it
    
    // If you are trying to get a pointer to an individual string...
    // this is the way to do it...
      }
    
    // Here's another interpretation...
      {
      char code * code *jons_dptr;
    
    // jons_dptr is a ptr to a ptr (in code space) to a char in code space
    // I think this is more like what we really want to get!
    // the sizeof jons_dptr is 2
    
      jons_dptr = CHNLTable;
    // jons_dptr points to the ptr to the string "One  "
    
      printf ("*jons_dptr = %s\n", *jons_dptr);
    // *jons_dptr points to the string "One  "
    
      jons_dptr++;
    // Increment jons_dptr so that it points to the ptr to the string "Two  "
    
      printf ("*jons_dptr = %s\n", *jons_dptr);
      }
    
    while (1);  
    }
    
    // Viola!!!
    

    I tested this with uVision2 V6.21 and got the results that I expected.

    The real problem in the original code (which I think I kept intact) is a clear definition of what you want to point to (the string or the table).

    If you use a table and you want to get a pointer to it, you need to remember that [] => *. In other words a pointer to an array of pointers is really a double pointer (or a pointer to a pointer to a thing).

    Jon

Children
No data