In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "C language sizeof how to work in the custom function", detailed content, clear steps, details handled properly, I hope that this "C language sizeof how to work properly in the custom function" article can help you solve doubts, following the editor's ideas slowly in-depth, together to learn new knowledge bar.
1. In the main function, sizeof can work properly, such as:
Int main () {int n [5]; printf ("input:\ n"); int I; for (I = 0; I < 5; iTunes +) {scanf ("% d", n + I);} int len = sizeof (n) / sizeof (n [0]); printf ("% d\ n", len); return 0;}
2. However, it is not allowed in custom functions, as follows:
# include void print_1 (int n []) {int i; int len = sizeof (n) / sizeof (n [0]); printf ("len:% d\ n", len); for (I = 0; I < len; iTunes +) {printf ("% d", n [I]);} printf ("\ n") } int main () {int n [5]; printf ("input:\ n"); int I; for (I = 0; I < 5; iTunes +) {scanf ("% d", n + I);} print_1 (n); printf ("\ n"); return 0;}
3. As for the reasons and solutions, I feel that there is a better blog.
The reason is: first, in c, the array is reduced to a pointer when it is used as a parameter, and the size of an address is 4 for a 32-bit system and 8 for a 64-bit system, so sizeof gets the length of the pointer rather than the length of the array in the function. Second, in the function, the processing time of sizeof is in the compilation time, that is to say, the size of dynamically generated arrays cannot be calculated by sizeof.
4. Solution
The first way
When the array is passed as a parameter to the array, it is also passed in the length of the array. However, this method needs to get the length of the array outside the function before passing it in, which is a little troublesome. For example, the function name of the print function mentioned above is print (int * n); change it to print (int * n, int len), where len represents the length of the array n
The second way
The macro definition is adopted, but this method feels a bit verbose.
The third way
C++ can use containers such as vector to avoid most array operations, string can be used instead of character arrays, and there are miracles that can pass array sizes. If you need a function to calculate the length of an array, you can use a macro definition or, in the case of clocked definitions, use the corresponding template technique, which is safer than a macro definition.
Template long calc (long (& array) [N]) / / using the template to pass the parameter array is a reference to the array {long res = 0L; for (size_t I = 0 return res; I < NittI + +) {res + = array [I];} return res;} int main () {long longarr [] = {1,23,4,45,46,57,}; long sum = calc (longarr) } after reading this, the article "how sizeof works in custom functions in C language" has been introduced. If you want to master the knowledge points of this article, you still need to practice and use it yourself. If you want to know more about related articles, welcome to follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.