Home >Backend Development >C++ >Why Do `strlen` and `sizeof` Produce Different Outputs for Pointers and Arrays in C?
Different Outputs from Strlen and Sizedof Due to Pointer and Array Initialization
In C programming, sizeof and strlen functions provide information about the size and length of data types, respectively. However, when dealing with pointers and arrays, the outputs of these functions can differ, leading to confusion.
Consider the following example:
<code class="c">char *str1 = "Sanjeev"; char str2[] = "Sanjeev"; printf("%d %d\n", strlen(str1), sizeof(str1)); printf("%d %d\n", strlen(str2), sizeof(str2));</code>
The output from this code is:
7 4 7 8
Explanation:
The above is the detailed content of Why Do `strlen` and `sizeof` Produce Different Outputs for Pointers and Arrays in C?. For more information, please follow other related articles on the PHP Chinese website!