We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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"};
code char code *CHNLTable[] = { Num1, Num2, Num3};
void func(void){ unsigned int *myptr; myptr = &CHNLTable; ...code that uses myptr }
"I get a C182 "Pointer to different objects" warning ... I don't understand the warning." See the description in Chapter 7, Error Messagee in the C51 manual: "A pointer was assigned the address of a different type."
unsigned int *myptr; myptr = &CHNLTable;
Really? Now I'm more confused because I think CHNLTable isn't full of chars, it's full of pointers which are pointing to arrays of characters. So each entry in CHNLTable is an int, and to increment through it using pointer math the compiler needs to know each offset is two bytes. I guess I'm all screwed up now. Can you fix it for me? I'll buy you a beer!
Hello:
char *myptr; myptr = CHNLTable[2];
Hi Tony, Does replacing: myptr = &CHNLTable; with: myptr = (int *) &CHNLTable[0][0]; work for you?
It doesn't change any of the warnings AND it breaks the code. (I just tried it)
Right, that's what I thought. Isn't that what I was saying? So it seems to me that you and Andrew are telling me different things? "myptr" points to an array of pointers (actually the first one as show in my original post) which are 16 bit objects; and THEY point to arrays of chars. I'm grateful for everyone's replies, now only if I could make some sense out of them! :) Now if Jon Ward would chime in!!
I think everyone will agree with Walt Conley: CHNLTable[] is an array of pointers. '&CHNLTable' is equivalent to 'CHNLTable' as well as '&(CHNLTable[0])'. That is, '&CHNLTable' is a pointer to the first element of the array and it has the type 'char code**'. So if you want to convert it to pointer to int (for whatever reason), use 'int* myptr = (int*)&CHNLTable;'. Actually, it's all well described in K&R. Reading this book can only do good. Hopefully, I didn't mess up here :-)) Regards, Mike
Oops - sorry, you're right!
char code Num1[] = {"One "}; char code Num2[] = {"Two "}; char code Num3[] = {"Three"}; code char code *CHNLTable[] = { Num1, Num2, Num3}; void func(void){ unsigned int *myptr; myptr = &CHNLTable; ...code that uses myptr }
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!!!
That is, '&CHNLTable' is a pointer to the first element of the array and it has the type 'char code**' I think this is too imprecise. &CHNLTable is the address of CHNLTable. Jon
I only ment that address of array is a pointer to the first element of the array (in terms of both type and actual binary value). Actually, that is how I would define the term 'address of array'.
Yes, however, a pointer has an address whereas an address does not. Jon