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

About pointer to a huge table in C

Hi,

I am a bit confused about writing codes with a poitner pointed to a huge table in C with Keil compiler (for C166 MCU).

First if i want to define a pointer pointed to a huge table, naturally i would do:

int huge (*ptable)[256];

However the complier does not allow to write in this way. The only way to pass the compiling is:

int (huge *ptable)[256];

The codes seem to work like this. But i am not really sure if i defined the pointer in a correct way, Can someone confirm me about it?

Second question about type cast. If i want to convert something into a pointer, I found i can do both type casts:

long addr;
int (huge *ptable)[256];

ptable = (int huge (*)[256])(&addr);
ptable = (int (huge *)[256])(&addr);

Both of the 2 lines above seem to work. But i am confused why the complier allows to define a point only in 1 way but allows type cast in 2 different ways? Does anyone knows the story under the hood?

Thanks for the help.

Regards,
Xiang

Parents
  • Sorry, I didn't realize that one could declare a pointer to an array with specified size.

    Then, with all due respect, I suggest you take the time to revisit your C textbooks' chapters' on pointers.

    Is this a feature introduced in C99?

    Not at all. It's been like that for as long as C exists.

    A more easily mangeable way of defining an object like this, avoiding the possible misunderstandings of pointer-to-array vs. array-of-pointers as nice side effect, is this:

    typedef huge bigarray[SIZE];
    
    bigarray *pointer_to_big_array;
    

    The rationale behind this is that if you define a pointer to any even moderately complicated type, it's quite certain that you're going to do other things with that same type. So save yourself the hassle of getting the type's definition right multiple times, in different places all over the source: typedef it once and for all!

Reply
  • Sorry, I didn't realize that one could declare a pointer to an array with specified size.

    Then, with all due respect, I suggest you take the time to revisit your C textbooks' chapters' on pointers.

    Is this a feature introduced in C99?

    Not at all. It's been like that for as long as C exists.

    A more easily mangeable way of defining an object like this, avoiding the possible misunderstandings of pointer-to-array vs. array-of-pointers as nice side effect, is this:

    typedef huge bigarray[SIZE];
    
    bigarray *pointer_to_big_array;
    

    The rationale behind this is that if you define a pointer to any even moderately complicated type, it's quite certain that you're going to do other things with that same type. So save yourself the hassle of getting the type's definition right multiple times, in different places all over the source: typedef it once and for all!

Children
  • Then, with all due respect, I suggest you take the time to revisit your C textbooks' chapters' on pointers.

    Thank you, I just did. Apparently, I wasn't paying enough attention when reading it the first time. Pointers to arrays are discussed briefly in K&R in connection with multidimensional arrays and function arguments. I guess it's my turn to eat the humble pie.
    In relation to the original poster's intention, it appears that pointers to arrays are not designed to facilitate out-of-bounds checking. Moreover, it appears that they can only be useful when working with multidimensional arrays.

  • In relation to the original poster's intention, it appears that pointers to arrays are not designed to facilitate out-of-bounds checking. Moreover, it appears that they can only be useful when working with multidimensional arrays.

    Both those appearances are incorrect.