array, pointer, sizeof
时间:2010-04-25 来源:non_oop
Differences between array & pointer:
1) Array indicators is a const value, that means you can't reassgin a new value to it. Pointers are not.
2) The expression sizeof(var_array) means var_array.length * sizeof(var_array[0]); whereas the expression sizeof(a_pointer) means the amount of bytes a pointer occupys in the memory. If you want to pass an array of non-char-type data to another function, you have to explicityly pass the length at the same time. The following code segment, suppose we want to pass an int array, is an example:
By the way, don't expect sizeof() to solve the unknown-length array issue for it can't get the length dynamically. Of course, char type array is an exception, there's always an hidden '\0' at the end of your char arrays. That's why sizeof(char_array) / sizeof(char_array[0]) always equals strlen(char_array) + 1 Why sizeof? As we know, C language is a low-level language, that means it can't prevent the programmer from concerning the hardware specifications, one of which is the bus bit size. For different computer models, a given fundamental data type, such as int, long and short, locates different amount of bytes. Using sizeof may help you prevent from hardcoding, which may help enhance the platform-indenpendence. References: http://topic.csdn.net/t/20060205/18/4540750.html http://fanqiang.chinaunix.net/a4/b2/20010419/144336_b.html http://www.cnblogs.com/del/archive/2008/12/04/1347680.html
void function(int argc, int *header) |
By the way, don't expect sizeof() to solve the unknown-length array issue for it can't get the length dynamically. Of course, char type array is an exception, there's always an hidden '\0' at the end of your char arrays. That's why sizeof(char_array) / sizeof(char_array[0]) always equals strlen(char_array) + 1 Why sizeof? As we know, C language is a low-level language, that means it can't prevent the programmer from concerning the hardware specifications, one of which is the bus bit size. For different computer models, a given fundamental data type, such as int, long and short, locates different amount of bytes. Using sizeof may help you prevent from hardcoding, which may help enhance the platform-indenpendence. References: http://topic.csdn.net/t/20060205/18/4540750.html http://fanqiang.chinaunix.net/a4/b2/20010419/144336_b.html http://www.cnblogs.com/del/archive/2008/12/04/1347680.html
相关阅读 更多 +