Exercises to practice C language

Insert image description here


1. offsetof macro

First we need to understand what the offsetof macro is:
Insert image description here

. This macro in functional form returns the offset value, in bytes, of a member member of a data structure or union type.
.The returned value is an unsigned integer value of type size_t with the number of bytes between the specified member and the beginning of its structure.

What does it mean? You can see the picture below:
Insert image description here
Let’s see this exercise:

Exercise content:
Write a macro to calculate the offset of a variable in the structure relative to the first address.

Problem-solving ideas:
According to the meaning of the question, we need to first define a macro OFFSETOF. Because we want to return the offset of the member variable relative to the starting position, we need to pass in the structure name and member variable name; then how to calculate the offset
? ?
We can take the starting address as 0x00000000, and then return the address of the member variable, and the result is the offset;

Code demo:

#include<stdio.h>
#define OFFSETOF(STN,MEN) (int)&(((struct S*)0)->MEN)
typedef struct S
{
    
    
	int a;
	char b;
	char c;
	int d;
}node;
int main()
{
    
    
	printf("%d\n", OFFSETOF(node S, a));
	printf("%d\n", OFFSETOF(node S, b));
	printf("%d\n", OFFSETOF(node S, c));
	printf("%d\n", OFFSETOF(node S, d));
	return 0;
}
}

Output result:
Insert image description here
parsing:
Insert image description here

2. Exchange parity bits

Exercise content:
Write a macro that can swap the odd and even bits of the binary digits of an integer.

Problem-solving ideas:
According to the meaning of the question, we need to first define a macro EXCHANGE and pass in the name of the variable to be exchanged;
because we need to exchange the odd and even bits, we can take out the odd and even bits respectively, and then shift the odd bits to the left by one bit,
the even-numbered bits are shifted one bit to the right, and the result of the addition is the result after the exchange;

Code demo:

#include<stdio.h>
#define EXCHANGE(n) (((n&0x55555555)<<1)+((n&0xaaaaaaaa)>>1))

int main() {
    
    
	int a = 21;
	int b = EXCHANGE(a);
	printf("%d", b);
	return 0;
}

Running results:
Insert image description here
Analysis:
Insert image description here

3. Remove the array in place

Exercise content:
Remove all elements val in the array in place. The time complexity is O(N) and the space complexity is O(1). Requirement: After
removing the elements that need to be removed from the array, return the new array length.

Problem-solving ideas:
According to the meaning of the question, we can use the double pointer method for this question. First let one pointer point to the starting position of the array, and then the second pointer moves with the for loop. If it finds the number that is not the number to be removed, it will be removed. Move to the position of the first pointer, and then move the first pointer backward, so that the first pointer will finally point to the next digit of the final array, and its position subscript is the length of the new array;

Code demo:

#include<stdio.h>

int removeElement(int* nums, int numsSize, int val) {
    
    
    int pos = 0;
    for (int i = 0; i < numsSize; i++) {
    
    
        if (nums[i] != val) {
    
    
            nums[pos] = nums[i];
            pos++;
        }
    }
    return pos;

}

int main() {
    
    
    int arr[] = {
    
     0,1,2,2,3,0,4,2 };
    int val = 2;
    int len = sizeof(arr) / sizeof(arr[0]);
    int ret=removeElement(arr,len,val);
    for (int i = 0; i < ret; i++) {
    
    
        printf("%d ", arr[i]);
    }
    return 0;
}

Running results:
Insert image description here
Analysis:
Insert image description here


Summarize

These test questions are what I think are typical examples, so I will show them to you here;
I hope you all study hard and make progress every day!

Guess you like

Origin blog.csdn.net/mdjsmg/article/details/131967576