Summary of interview questions filled by Xiyou Linux interest group in 2019

Operation results: 

Hexadecimal small end sorting method is used during initialization of structure num:  

The hexadecimal address of xiyouLinux is (0x78 0x69 0x79 0x6f 0x75 0x4c 0x69 0x6e 0x75 0x78)

You can refer to the article on the large and small ends: Byte order: big endian and small endian - Broglie - blog Garden

  Method 1:

#include <stdio.h>
int main()
{
    const int i = 10;//If const i=10, the compiler will also default that i is of type int
    int* pi = (int*)(&i);//Note that you need to add int * type cast before forcing & I (equivalent to P = & I)
    *pi = 100;//Modify const constant value through pointer
    printf("i = %d\n",i);//Modified successfully i=*pi=100 
    return 0;
}

Method 2:

#include<stdio.h>
void b(int *a)//Modify the value of a directly in the word function
{
    *a=*a+1;
}
int main(void)
{
    const int a=1;
    int *c = (int *)(&a);
    b(c);//Pass the a address pointed to by the pointer
    printf("%d\n",a);
    printf("%d\n",*c);
    return 0;
}

#include<stdio.h>
struct node{
    char a;
    int b;
    short c;
};//According to the memory alignment, its size is 12
struct n{
    char a;
    int b;
};//According to the memory alignment, its size is 8
int main(int argc,char*argv[])
{
    struct node a[2]={{1,2,3},{4,5,6}};
    *(int *)((char*)a+2*sizeof(struct n))=100;
    printf("%d\n",a[1].b);
    return 0;
}//a[1].b is changed to 100

2*sizeof(n)=2*8=16;

(char*)a+2*sizeof(struct n) is equivalent to a moving 16 bytes from the first address

Sizeof (node) = 12 (byte Complement 4 + 4 + 4), that is, a node structure element accounts for 12 bytes

Therefore, after moving 16 bytes from a, it happens to the first address of a[1].b (memory alignment)

Therefore, * (int *)((char*)a+2*sizeof(struct n))=100 is equivalent to a[1].b=100

That is, 5 becomes 100

Memory alignment: Detailed explanation of C/C + + memory alignment - Zhihu

 

#include<stdio.h>
#include<string.h>
void func(char*a)
{
    printf("%lu\n",sizeof(a));//8(sizeof calculates the size of the pointer (char *), which is 8 in 64 bits)
    printf("%lu\n",strlen(a));//11
}
int main(int argc,char*argv[])
{
    char a[]="hello world";
    char*b="hello world";
    printf("%s\n",b);
    func(a);
    printf("%lu\n",sizeof(a));//12 (automatically add '\ 0' to the end of the string)
    printf("%lu\n",strlen(a));//11
    return 0;
}


The result type of sizeof operator is size_t (it is defined as unsigned int with typedfe in the header file), and it calculates the actual number of bytes of allocated space.

The strlen result type is also size_t, but strlen is the number of characters in the calculated space (excluding '\ 0').

For details, please refer to: The difference between strlen and sizeof drifting calf CSDN blog the difference between strlen and sizeof

#include<stdio.h>
#define f(a,b) a##b
#define g(a) #a
#define h(a) g(a)
int main(int argc,char*argv[])
{
    printf("%d\n",f(1,2));//12
    printf("%s\n",h(f(1,2)));//12
    printf("%s\n",g(f(1,2)));//f(1,2)
    return 0;
}
/*#a Yes, replace a in g(a) with a string, and replace #a with "f(1,2)" string*/
/*f(n) x##n Expand as xn,f(1,2) expand as ab=12(a=1,b=2)*/
/*Replace h(a) with g(a),a=f(1,2)=12,g(a)=#a=12*/

For details, please refer to: The meaning of ## connector and # connector in the definition of c macro

#include<stdio.h>
int sum;
int main()
{
    int n,m=0,cnt=0;
    scanf("%d",&n);
    int s[n+1];
    for(int i=0;i<n;i++)
    {
        scanf("%d",&s[i]);
        m+=s[i];//Total mass
    }
    if(m%2!=0)
    {
        printf("no!\n");//The singular must not be divided equally
        return 0;
    }
    for(int i=0;i<n;i++)
    {
        sum+=s[i];
        if(sum==m/2)//If you find the number of cakes with a total quality of half, exit directly
        {
            printf("ok!\n");
            return 0;
        }
/*Because this question refers to cakes with different quality, only one person can be traced back when (sum > m / 2). If there is a cake with repeated quality, it is not necessary to trace back only one person*/
        else if(sum>m/2)//If the total mass is more than half, return one cake number and look for the next one
        {
            sum-=s[i];
        }
        else//If the total quantity is less than half, continue to find
        {
            continue;
        }
    }
    printf("no!\n");//If a combination with half quality has not been found at the end of the cycle, it indicates that such a combination does not exist
    return 0;
}

If the cake quality is repeated, different lengths of backtracking are required, please refer to:

And array elements for a specific value (whether there is / number / all combinations) _Boston_Kimisong's blog - CSDN blog

Find out all combinations _brucehb of elements in the array and the specified value - CSDN blog

Use stack to complete:

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
char * reverseWords(char * s)
{
    int len = strlen(s);                                 
    int top = 0;                                         //Stack top pointer
    int n = 0;
    if(len == 0)                                         //The string is empty
        return s;
    char *res = (char*)malloc(sizeof(char) * (len + 1)); //Allocate space for the output array
    res[len] = '\0';
    char *stack =(char*)malloc(sizeof(char) * (len + 1));//Allocate space for stack
    for(int i=len-1; i >= 0; i--){                       //Traversal string
        while(i >= 0 && s[i] == ' '){                    //Skip when space is encountered
            i--;
        }
        while(i >= 0 && s[i] != ' '){                    //Push string onto stack
            stack[top] = s[i];
            top++;
            i--;
        }
        while(top > 0){                                  //Remove characters from stack
            res[n] = stack[top-1];
            top--;
            n++;
        }
        res[n] = ' ';                                    //Each word is separated by a space
        n++;
    }
    n--;
    while(n >= 0 && res[n] == ' ')              //There will be spaces at the end of the output string, which will be removed circularly
        n--;
    res[n+1] = '\0';                                     //End of output string
    return res;
}
int main()
{
    char s[101];
    gets(s);
    printf("%s\n",reverseWords(s));
    return 0;
}

The output is as follows:

 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

void func(char*p)
{
    p=(char*)malloc(sizeof(char));//Memory is requested in another space, and the space is released at the end of the function
    /*Amend as follows*/
    strcpy(p,"i love xiyou_linux!");//But it can be output directly in the sub function
    puts(p);
}

int main(int argc,char*argv[])
{
    char*s=NULL;
    func(s);//After returning, s still no memory space is allocated
    /*
    strcpy(s,"i love xiyou_linux!");
    puts(s);//(Segment error) accessed inaccessible memory. This memory area either does not exist or is protected by the system.
    */
    return 0;
}

In situ inversion method:

//List With Header node 
void reverse(Node*head)
{
    Node*p=NULL,*q=NULL;
    p=head->next;
    head->next=NULL;
    while(p)
    {
        q=p;//Keep the position of the node to be reversed (otherwise, the position of the next node cannot be found after connecting the current node p to the head node)
        p=p->next;//Find the next node
        q->next=head->next;//Connect the saved current node to the back of the head node
        head->next=q;//The current node becomes the initial node
    }
}
//Linked list without leading node 
void reverse(Node*head)
{
    Node* p=NULL, * q=NULL;
    p = head->next;
    head->next = NULL;
    while (p)
    {
        q = p;
        p = p->next;
        q->next = head;
        head = q;//The current node is directly used as the head node
    }
}

#include<stdio.h>
int main(int argc,char*argv[])
{
    char c=-1;
    printf("%x\n",c);//%x outputs data in int type (char c will be cast to int type first)
    printf("%x %d\n",(unsigned char)c,(unsigned char)c);//FF 255 (true value of C)
                                                        //0 ~ 255 (- 1 is 255)
    return 0;
    /*
    c=-1 It exists in memory in the form of complement,
    1 If the original code of is 0000 0001, the inverse code of - 1 is 1111 1110
    -1 The complement of is the inverse code + 1, that is 1111 1111
    Convert to hexadecimal, that is, ffffffff
    */
}

First look at ls the meaning of each column:

File attribute reference: ls -l command under linux displays the results. What does each column mean - Advanced notes of coder programming - blog Garden 

Or directly above:    Under Linux, the ls -l command executes to display the meaning of each column of the result_ zhuoya_ Blog - CSDN blog

chmod can modify file permissions in two ways: alphabetic method and numerical method

(1) Alphabetic method:

chmod     u/g/o/a     +/-/=    rwx     file name

u/g/o/a      meaning
u      user indicates the owner of the file
g      Group refers to those who belong to the same group as the owner of the file, that is, the user group
o      other means someone else
a      All means all three

+/-/=      meaning
+      Add permissions
-      Revoke permissions
=      Set permissions
rwx      meaning
r       Read means that a directory can be read. If a directory does not have R permission, it means that the contents of the directory cannot be viewed through ls
w       write means writable. For a directory, if you do not have w permission, it means that you cannot create new files in the directory
x        excute means executable. For a directory, if you don't have X permission, it means you can't enter the directory through cd

1. For example, add the write permission (+) of the directory Music to the team members:

  2. Only group members are allowed to have the read permission of test (=):

  3. Set the permissions of the owner, the same group and others at the same time (= empty is equivalent to canceling any permissions of u/g/o/a):

(remember to link with commas)

(2) Digital method:

rwx is represented by numbers  

rRead permission, numeric code "4"
wWrite permission, numeric code "2"
xExecution Authority, numeric code "1"
-No permission, numeric code "0"

For example: chmod u=rwx,g=rx,o=r test is equivalent to: chmod u=7,g=5,o=4 test, that is: chmod 754 test, which is more concise.

7 = 4 + 2 + 1       Read write run permission

5 = 4 + 1           Read and run permissions

4 = 4               Read-only rights

This command grants the read, write and run permissions of the test file to the file owner, the read and run permissions to the group user, and the read permissions to other users.

You can also recursively and quickly add the same permissions to all subdirectories or files under a directory. You need to add the parameter - R after it:

1. First use the tree to view the branches under the target directory:

  2.-R recursive modification permission:

  Modify permission reference:

linux modify file permissions (chmod)_ Wisteria & code blog - CSDN blog_ linux modify file permissions

Common Linux commands: chmod modify file permissions 777 and 754_ Python blog - CSDN blog

Tags: C Linux Algorithm data structure linked list

Posted on Mon, 29 Nov 2021 23:35:08 -0500 by vividona