Different answers from strlen and sizeof for Pointer & Array based init of String

strlen vs sizeof
sizeof strlen

Because sizeof gives you the size in bytes of the data type. The data type of str1 is char* which 4 bytes. In contrast, strlen gives you the length of the string in chars not including the null terminator, thus 7. The sizeof str2 is char array of 8 bytes, which is the number of chars including the null terminator.

See this entry from the C-FAQ: http://c-faq.com/~scs/cclass/krnotes/sx9d.html

Try this:

 char str2[8];
 strncpy(str2, "Sanjeev", 7);
 char *str1 = str2;
 printf("%d %d\n",strlen(str1),sizeof(str1));    
 printf("%d %d\n",strlen(str2),sizeof(str2));

c++, Basically, I did this char *str1 = "Sanjeev"; char str2[] = "Sanjeev"; printf("%d %d\​n",strlen(str1),sizeof(str1)); printf("%d %d\n",strlen(str2),sizeof(str2));. and my  The strlen() Function. Syntax: size_t strlen (const char* str); Note: For this chapter ignore the keyword const. It will be discussed later. The strlen() accepts an argument of type pointer to char or (char*), so you can either pass a string literal or an array of characters.


str1 is a pointer to char and size of a pointer variable on your system is 4. str2 is a array of char on stack, which stores 8 characters, "s","a","n","j","e","e","v","\0" so its size is 8

Important to note that size of pointer to various data type will be same, because they are pointing to different data types but they occupy only size of pointer on that system.

Concept of Computer and C Programming, Answer: 16 16 16 Explanation: fl and f2 both refer to the same memory location a. sTzeof(*p). strlen(p)): printf("\n sizeof(a) = %6. strlen(a) = %6" . sizeof(a) . strlen(​a)) : } Answer: sizeof(p) = 2. sizeof(*p) = 1. sizeof the pointer variable. sizeof operator is used to fetch the size a data structure will occupy in memory whereas strlen() is a function used to know thw length of a character array some details are: Usage sizeof() operator is used in different way according to the operand type. 1. When operand is a Data Type.


sizeof(str1) is sizeof(char*) whereas sizeof str2 is sizeof(char array)

sizeof(char array) should be equal to strlen(char array) + 1 (NULL termination). Well if you pass the char array to a function, its size as shown by sizeof will change. The reason is char array is passed as char* only.

C Programs with Solutions, printf(“\n%d %d %d”,a,*f1,*f2); } Answer: 16 16 16 Explanation: f1 and f2 both refer to the strlen(a) = %d”, sizeof(a), strlen(a)); } Answer: sizeof(p) = 2, sizeof(*​p) = 1, the sizeof the array and it is not the same as the sizeof the pointer variable. The main task of strlen() is to count the length of an array or string. sizeof vs strlen() Type: Sizeof operator is a unary operator whereas strlen() is a predefined function in C; Data types supported: Sizeof gives actual size of any type of data (allocated) in bytes (including the null values) whereas get the length of an array of chars/string.


 char *str1 = "Sanjeev";
 printf("%d %d\n",strlen(str1),sizeof(str1)); 

strlen() gives the length of the string which is 7. sizeof() is a compile time operator and sizeof(str1) gives the size of pointer on your implementation (i.e 4).

char str2[] = "Sanjeev";  
printf("%d %d\n",strlen(str2),sizeof(str2));

sizeof(str2) gives the size of the array which is 8*sizeof(char) i.e. 8 [including the NUL terminator]

C in a Nutshell: The Definitive Reference, sizeof(iArray) yields the size of the whole array, not the size of a pointer. How‐​ever, the same identifier iArray is implicitly converted to a pointer in the other three n", strlen(msg)); // Answer: 21. printf("The array named msg is %d bytes long. The function sizeof() is a unary operator in C language and used to get the size of any type of data in bytes. Here is the syntax of sizeof() in C language, sizeof( type ); Here, type − Any type or data type or variable, you want to calculate the size of. Here is an example of sizeof() in C language, Example. Live Demo


Differences between sizeof operator and strlen(), strlen() accepts a pointer to an array as argument and walks through memory at run time from the address we give it looking for a NULL character and counts up  strlen () is a run-time function that counts the number of bytes in a string. sizeof () is a compile-time pseudo-function that counts the number of bytes reserved for the variable. Both of the values that you see are correct. You should note that sizeof (str) returns 7 which is the number of bytes that the compiler reserved for the string.


Array Size - sizeof() vs. strlen() - C Tutorials, Answer: Let's learn this by considering an example, and analysing it's output when sizeof_vs_strlen.c -- program shows difference between using sizeof and * strlen sizeof returns size of pointer which is 8 bytes on Linux, although strlen()​  thể trùng lặp: Different answers from strlen and sizeof for Pointer & Array based init of String Ai có thể giúp tôi trong việc tìm hiểu sự khác biệt giữa sizeof phương pháp và strlen phương


strlen() & sizeof() | Practice, What is the difference between strlen() & sizeof() ? Login to Answer strlen() is a function that takes a pointer to a character, and walks the memory from this  char *str1 = "Sanjeev"; printf("%d %d ",strlen(str1),sizeof(str1)); strlen() gives the length of the string which is 7. sizeof() is a compile time operator and sizeof(str1) gives the size of pointer on your implementation (i.e 4). char str2[] = "Sanjeev"; printf("%d %d ",strlen(str2),sizeof(str2)); sizeof(str2) gives the size of the array which is 8*sizeof(char) i.e. 8 [including the NUL terminator]