C language - preliminary notes 2

Keywords: C Back-end

Initial order 6: function

  1. Function declaration void func();
Return type name (parameter);
  1. Function pointer: void (*pfunc)();
type (*Function name) (parameter)
  1. Assignment: pfunc = & func; Or abbreviated as pfunc=func;
  2. The function name is the address and the starting position for executing the function code
  3. Call 1: dereference (* pfunc)()// Parentheses must be added in the front, because the latter parentheses have high priority. Only when * and pfunc are packaged as a whole is the function pointer dereference
    Call 2: pfunc();
  4. Function pointers can be used as function parameters, which is called callback functions
  5. Pointer function: void* func() {}// The return value is a pointer type
    Example:
#include<stdio.h>

void func();

int main(){
    printf("%p %p\n",func,&func);
    func();
    int n;
    int* p;
    int** pp;

    void(*pfunc)();//Function pointer
    pfunc = &func;//Assignment pfunc=func
    (*pfunc)();//pfunc() 
    //Dereference is also the address of func
}

void func(){
    printf("hello world\n");
}

Important examples!!!

#include<stdio.h>

int add(int a,int b){
    return a+b;
}
int subtract(int a,int b){
    return a-b;
}
int multipy(int a,int b){
    return a*b;
}
int divide(int a,int b){
    return a/b;
}

//The int(*pfunc)(int,int) function pointer is used as a parameter to call the add,subtract,multipy,divide with uniform style as the callback function
int operation(int arr[],int n,int(*pfunc)(int,int)){
   int* p=arr;
   int res = *p;
   while(++p<arr+n){
       res = (*pfunc)(res,*p);//It can also be written as res = pfunc(res,*p);
   }
   return res;
}

int print_array(int arr[],int n){
    /* for(int i=0;i<n;i++){
         printf("%d ",arr[i]);
     }
     printf("\n");*/
    int* p =arr;
    while(p<arr+n){
      printf("%d ",*p++);
    }
    printf("\n");
}

int main(){
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++){
       scanf("%d",arr+i);
    }

    //Function pointer array, put the unified style function into this array to facilitate traversal and call
    int (*opt[])(int,int) = {add,subtract,multipy,divide};
    for(int i=0;i<4;i++){
       printf("%d\n",operation(arr,n,opt[i]));
    }
    print_array(arr,n);
}

Example (integer comparison):

#include<stdio.h>
#include<stdlib.h>
void print_array(int arr[],int n){
    /* for(int i=0;i<n;i++){
         printf("%d ",arr[i]);
     }
     printf("\n");*/
    int* p =arr;
    while(p<arr+n){
      printf("%d ",*p++);
    }
    printf("\n");
}

int cmp(const void* p,const void* q){
    return *(int*)p-*(int*)q;//If * P > * q is positive, change the order so that it is L
 Sequence, if*p<*q Returns a negative value in ascending order
 /* int* pn = (int*)p;
    int* qm = (int*)q;
    int n = *pn;
    int m = *qm;
    if(m == n){
       return 0;
    }else if(n<m){
       return 1;//Just a positive number
    }else{
       return -1;//Negative numbers will do
    }*/
}

int main(){
    int n;
    scanf("%d",&n);
    int arr[n];
    for(int i=0;i<n;i++){
       scanf("%d",arr+i);
    }

    //qsort(void* base,size_t nmemb,size_t size,int (*cmper)(const void*,const void *);
    //Qsort (array name, array size, array element type size, a function pointer in the above form: you can pass this style of comparison function, return a negative value when the conditions are met, pass a positive value when the conditions are not met, and change the element order)
    qsort(arr,n,sizeof(int),cmp);
    print_array(arr,n);
}

Example (floating point comparison):

#include<stdio.h>
#include<stdlib.h>
void print_array(float arr[],int n){
    float* p =arr;
    while(p<arr+n){
      printf("%f ",*p++);
    }
    printf("\n");
}

int cmp(const void* p,const void* q){
    return *(float*)p>*(float*)q?1:-1;//If * P > * q is a positive value, change the order >, which is in ascending order. If * P < * q returns a negative value, it is in ascending order
}

int main(){
    int n;
    scanf("%d",&n);
    float arr[n];
    for(int i=0;i<n;i++){
       scanf("%f",arr+i);
    }

    print_array(arr,n);
    //qsort(void* base,size_t nmemb,size_t size,int (*cmper)(const void*,const void *);
    //Qsort (array name, array size, array element type size, a function pointer in the above form: you can pass this style of comparison function, return a negative value when the conditions are met, pass a positive value when the conditions are not met, and change the element order)
    qsort(arr,n,sizeof(float),cmp);
    print_array(arr,n);
}

Example (string sorting)

#include<stdio.h>//printf()
#include<stdlib.h>//qsort()
#include<string.h>//strcmp()

void print_array(char* arr[],int n){
   char** p =arr;
   while(p<arr+n){
      printf("%s ",*p++);
   }
   printf("\n");
}

int cmp(const void* p,const void*q){
    char* a = *(char**)p;
    char* b = *(char**)q;
    return strcmp(a,b);
}

int main(int argc,char* argv[]){
    print_array(argv,argc);
    qsort(argv,argc,sizeof(char*),cmp);
    print_array(argv,argc);
}

C initial stage 7: structure

struct Point{
   int x;
   int y;
   int z;
};//3 * 4 = 12 bytes
  1. Structure definition object struct Point p ={1,3,6};
  2. The structure address is the address of its first member
  3. Structure pointer: struct point * q = & P;
    P - > x is equivalent to (* q).x note: - > comes with dereference
    3.1 access structure object members. For example: p.x=100
    3.2 access structure pointer members with - > for example: Q - > x = 1000;
  4. When defining structure objects in c language, struct cannot be omitted, such as struct Point q;

Example:

#include<stdio.h>

struct Point{
   int x;
   int y;
   int z;
};//3 * 4 = 12 bytes

void Point_Print(struct Point p){
   printf("(%d,%d,%d)\n",p.x,p.y,p.z);
}

void PointPtr_Print(struct Point* p){
    printf("(%d,%d,%d)\n",p->x,p->y,p->z);
}

int main(){
    struct Point p ={1,3,6};
    Point_Print(p);
    printf("%d\n",sizeof(p));
    struct Point* q=&p;//q is a pointer, accounting for 8 bytes in 64 bits and 4 words in 32 bits
 section
    printf("%d\n",sizeof(q));
    Point_Print(*q);
    q->x=100;
    q->y=300;
    q->z=600;
    Point_Print(p);
    PointPtr_Print(q);
}
  1. Structure array struct point points [] = {1,2,3}, {3,3,3}, {2,3,4}, {7,7,7};
  2. Structure nested structure
struct Line{
     struct Point p1;
     struct Point p2;
}

Example:

#include<stdio.h>
#Include < math. H > / / sqrt() pow (a, b) use - lm when compiling the linked library
struct Point{
   int x;
   int y;
   int z;
};//3 * 4 = 12 bytes

//Structure nested structure
struct Line{
    struct Point p1;
    struct Point p2;
};

float GetDistance(struct Line line){
     return sqrt(
                     pow(line.p1.x-line.p2.x,2)+
                     pow(line.p1.y-line.p2.y,2)+
                     pow(line.p1.z-line.p2.z,2)//pow(a,b) represents the b > power of A
                     );
}
float GetDistance2(struct Line* line){//A parameter is a structure pointer that accesses its pair
 as line Member to use->   The call is the address of the structure to be passed
     return sqrt(
                     pow(line->p1.x-line->p2.x,2)+
                     pow(line->p1.y-line->p2.y,2)+
                     pow(line->p1.z-line->p2.z,2)//pow(a,b) represents the of A
b Power
                     );
}
void Point_Print(struct Point p){
   printf("(%d,%d,%d)\n",p.x,p.y,p.z);
}

void PointPtr_Print(struct Point* p){
    printf("(%d,%d,%d)\n",p->x,p->y,p->z);
}

int main(){
    struct Point p ={1,3,6};
    Point_Print(p);
    printf("%d\n",sizeof(p));
    struct Point* q=&p;//q is a pointer, accounting for 8 bytes in 64 bits and 4 words in 32 bits
 section
    printf("%d\n",sizeof(q));
    Point_Print(*q);
    q->x=100;
    q->y=300;
    q->z=600;
    Point_Print(p);
    PointPtr_Print(q);

   //Structure array, traversal is like array traversal
    struct Point points[] = {{1,2,3},{3,3,3},{2,3,4},{7,7,7}};
    for(int i=0;i<4;i++){
       Point_Print(points[i]);
       PointPtr_Print(points+i);
    }

    //Structure nested structure
    struct Line line = {{0,0,0},{3,4,5}};
    Point_Print(line.p1);
    Point_Print(line.p2);
    printf("%f\n",GetDistance(line));
    printf("%f\n",GetDistance2(&line));
}
  1. Structure contains an array of structures:
struct Triangle{
   struct Point p[3];
};
struct Triangel t = {{{1,2,2},{1,1,1},{8,0,0}}};
  1. Traversal:
for(int i=0;i<3;i++){
   Point_Print(t.p[i]);
}

Example:

#include<stdio.h>
#include<time.h>  //struct tm*  time() ... 

int main(){
    time_t now = time(NULL);
    struct tm* utc = gmtime(&now);//Green time 
    struct tm* local = localtime(&now);//Local time is 8 hours more than green world

    char temp[20]={'\0'};//  YYYY/MM/DD HH:mm:SS
    //Put the contents of struct tm * structure pointer object in the format of% F year% T time (defined in strftime) into the temp array space with size of 20
    strftime(temp,20,"%F %T",local);

    printf("%s\n",temp);

    struct tm test;
    //Put the time string in% F% t format into the struct object test of struct tm type
    strptime("2020-01-01 21:03:10","%F %T", &test);
    printf("%d/%d/%d %d:%d:%d\n",test.tm_year+1900,test.tm_mon+1,test.tm_hour,test.tm_min,test.tm_sec);
}

C initial stage 8: Consortium

Example:

#include<stdio.h>

struct SPoint{
   int x;
   int y;
   int z;
};//3 * 4 bytes = 12 bytes of memory
union UPoint{
   int x;
   int y;
   int z;
};//The members of the consortium share memory, and the space occupied is the space occupied by the largest member, which accounts for 4 bytes, because the int type accounts for 4 bytes

union Integer{
  int data;
  char bytes[4];
};

int main(){
    printf("%d\n",sizeof(struct SPoint));
    printf("%d\n",sizeof(union UPoint));

    union UPoint up;
    up.x = 100;//Assign a value to a member of the consortium. Because members share a piece of memory, other members are assigned the same value
    printf("(%d,%d,%d)\n",up.x,up.y,up.z);

    union Integer n;
    n.data = 10113234;
    printf("%#x\n",n.data);//%#x output in hexadecimal
    for(int i=0;i<4;++i){
       printf("%#hhx\n",n.bytes[i]);//%x default integer
    //Single h is to expand int(4 bytes) into short(2 bytes)
    //The two h's extend int to char(1 byte)
   }

C initial level: Enumeration

  1. Magic Number: a number with unknown meaning in a program.
  2. It is usually represented by symbols. Solutions: const and #define
    const int SUN = 0;//SUM is a constant 1 with an equal sign
    #define SUN 0; / / replace SUN with 0 during preprocessing. Use the space - E to see the preprocessing
  3. Difference between const and #define (macro definition):
No.Comparison item#defineconst
1Compilation processingPretreatment stageCompile and run phase
2working principleSimple string replacementThere is a corresponding data type
3Storage modeExpand to have several backups in memoryRead only variables have only one copy in memory
4Type checkNo type safety checkType checking at compile time
5ScopeFrom definition, it can be accessed anywhereCan only be within the scope of a variable

#define MON 1; replace all MON with 1 when compiling preprocessing
4. Macro definition has no type
const is compiled and processed in the compilation run stage

  1. Enum: enum enum name {name 0, name 1, name 2,...}
    5.1. The type is int, and the value is from 0 to n at a time
    5.2. Enumeration can be regarded as a pile of macro definitions. Putting them together (but not) is to specify a name for some constant values
    5.3. Enumerators can be used directly as values
    5.4. Enumeration types can be directly used as types. When defining types, only a limited number of constants in enumeration values can be used
    5.5. You can specify values when declaring, such as:
enum R{Bin=2,Oct=8,Dec=10,Hex=16};

If a value is determined, then add one in turn, such as:

enum M{Jan=1,Feb,Mar,Apr,May,Jun,Jul,Aug,Sept,Oct,Nov,Dec};

Namely: Feb=2,Mar=3

Posted by gumby51 on Fri, 05 Nov 2021 16:51:19 -0700