Small knowledge, big challenge! This article is participating in the creation activity of “Essential Tips for Programmers”

This article has participated in the “Digitalstar Project” and won a creative gift package to challenge the creative incentive money

Foreword: 🛫 come to dig gold at the beginning! Welcome to support! Learn from each other and grow from each other!

🚖 introduce myself, I am a student of some two colleges and universities who is reading sophomore non computer class! We are all trying to run, we are all dreamers!

————————————-

A. Sizeof explanation

1. Sizeof () measures the memory sizeof a variable/type, in bytes

int b =sizeof(a); Int c = sizeof(int); / / 4Copy the code

Sizeof is an operator, not a function!

🧧sizeof a variable or sizeof can be used

🎃sizeof(type) Can sizeof the type is wrong

int main()
{
	int a, b, c, d;
	a = sizeof(a);
	b = sizeof a;
	c = sizeof(int);
	//d = sizeof int;	//err
	printf("%d %d %d\n", a, b, c);    // 4 4 4
	return 0;
}
Copy the code

3. The expression inside sizeof () does not participate in the real calculation! This is important! Its operands are computed at compile time

int main()
{
	int a = 10;
	int b = sizeof(a = a + 1);
	printf("%d %d \n", a, b);    // 10 4
	return 0;
}
Copy the code

🎡sizeof does not perform an actual operation even if an assignment expression is written inside it. Sizeof evaluates only to the type of the operand and does not access the corresponding space


Sizeof () in relation to the array name

🎈sizeof(array name): The array name represents the entire array

🎐**& Array name: The array name represents the entire array **

🎐 In other cases, the array name represents the address of the first element

int arr[10]; a = sizeof(arr); 4*10 = 10 b = sizeof(&arr); // Retrieve the address of the entire array, which is the address (pointer), 4/8Copy the code

5. Sizeof () The crater caused by the return type

int i ; Int main() {I --; If (I > sizeof(I)) {printf(">\n"); } else { printf("<\n"); } return 0; }Copy the code

Print result: >

🎑 Note: sizeof(I) is compared with I, sizeof returns size_t and I is int. Finally, size_t is used for comparison. The complement of -1 is a sequence of all 1s, which is larger than sizeof(I) = 4


2. Strlen explanation

🚋strlen() : library function that evaluates the length of a string, stopping at \0. We need to refer to the #include

header file. Note that size_t(unsigned int) is returned.

The function prototype

Analog implementation of strlen() :

Method 1: counters

size_t my_strlen(const char* str) { size_t count = 0; while (*str ! = '\0') { count++; str++; } return count; }Copy the code

Method 2: Recurse

Const char* STR) {// If not \0 +1(which refers to the character itself), then recurse to the next character if (* STR! = '\0') return 1 + my_strlen3(str + 1); // if \0 is encountered, return 0 else return 0; }Copy the code

Method 3: pointer – pointer

🚇 pointer – A pointer yields the number of elements in between. So as long as one pointer points to the beginning of the string and one pointer points to \0, the two subtracts to the length of the string

size_t my_strlen2(const char* str) { char* start = str; char* end = str; while (*end ! = '\0') { end++; } return end - start; }Copy the code

Strlen () returns the crater caused by the size_t argument

int main()
{
	char* p1 = "abcd";
	char* p2 = "abcde";
 
 
	if (strlen(p1) - strlen(p2)>0)
	{
		printf("p1>p2");
	}
	else
	{
		printf("p1<p2");
	}
	return 0;
}
Copy the code

🏍 The preceding code displays p1> P2

Strlen (p1) : result 4 strlen(p2) : result 5 Both are size_t unsigned integers

Subtract the two to get -1, also known as an unsigned integer, with a value greater than 0


Strlen and sizeof

🦽 Common: Both return types are size_t

🏎 differences: sizeof() is the operator that measures the sizeof the variable/type in bytes, \0 also counts in space

🚉strlen() is a library function that evaluates the string length, not \0


int main() { char arr1[] = { 'a','b','c' }; int ret1 = strlen(arr1); int ret2 = sizeof(arr1); printf("%d %d\n", ret1,ret2); Strlen () does not stop until it finds \0, so it is a random value. Arr1 has 3 elements in the array. 3*1 = 3 char arr[] = "abcdef"; int ret3 = strlen(arr); int ret4 = sizeof(arr); printf("%d %d\n", ret3, ret4); //6 7 //arr size is 7, with \0 strlen not \0 return 0; }Copy the code

🛴 If this article is helpful to you, you are welcome to support it! Thank you, thank you!