How to get array size within function?

前端 未结 1 1672
情书的邮戳
情书的邮戳 2021-01-26 01:22

I am having trouble finding a way to get the array size from within a function. Here is my code:

#include 

void printBuff(char *buf);
int main()
         


        
相关标签:
1条回答
  • 2021-01-26 02:05

    so here are my answers for your questions:

    1. The array is "converted" into char* type when passed into the function (with the char* parameter).
    2. AFAIK there is no such way. You could use strlen function for strings. Otherwise, you have to pass the length as parameter.
    3. See How does an array pointer store its size?

    Don't use sizeof(buf)/sizeof(buf[0]) to get length of an array when passing array parameters. See this. for more information.

    0 讨论(0)
提交回复
热议问题