C language array pointer

task

Job 1 parameter transfer of arrays with different dimensions and pointer arrays

  1 #include <stdio.h>
  2 
  3 void  print1(char *ptr)
  4 {
  5     printf("ptr = %s\n",ptr);
  6 }
  7 void print2(char (*ktr)[100])
  8 {
  9     int i=0;
 10     for(i=0;i<2;i++)
 11     {
 12         printf("ktr[%d] = %s\n",i,*(ktr+i));
 13     }
 14 }
 15 void print3(char (*str)[2][100])
 16 {
 17     int i=0;
 18     int j=0;
 19     for(i=0;i<2;i++)
 20     {
 21         for(j=0;j<2;j++)
 22         {
 23             printf("str[%d][%d] = %s \n",i,j,*(*(str+i)+j));
  24         }
 25     }
 26 }
 27 void print4(char **ytr)
 28 {
 29     int i=0;
 30     for(i=0;i<3;i++)
 31     {
 32         printf("ytr[%d] = %s \n",i,*(ytr+i));
 33     }
 34 }
 35 
 36 int main()
 37 {
 38     char ptr[100]="hello";
 39     char ktr[2][100]={"hello1","hello2"};
 40     char str[2][2][100]={{"hello3","hello4"},{"hello5","hello6"}};
 41     char *ytr[3]={"hi1","hi2","hi3"}; //pointer array
 42 
 43     print1(ptr);
 44     print2(ktr);
 45     print3(str);
 46     print4(ytr);
 47     return 0;
 48 }

result

[root@localhost 0708]# gcc zuoye.c
[root@localhost 0708]# ./a.out
ptr = hello
ktr[0] = hello1
ktr[1] = hello2
str[0][0] = hello3 
str[0][1] = hello4 
str[1][0] = hello5 
str[1][1] = hello6 
ytr[0] = hi1 
ytr[1] = hi2 
ytr[2] = hi3 

Homework 2 single linked list review

  • The data in the linked list is represented by nodes. The composition of each node is: element + pointer (indicating the storage location of subsequent elements). The element is the storage unit for storing data, and the pointer is the address data connecting each node. Single linked list is a structure of chain access.
  • The storage address of each node in the single linked list is stored in the next field of the leading node, while the starting node has no leading, so the head pointer should be set to point to the starting node. The linked list is uniquely determined by the head pointer. A single linked list can be named after the head pointer.
    The terminal node has no successor, so the pointer field of the terminal node is NULL, i.e. NULL.

Class notes

1. bit operation & |^ > > < <~

Clear some bits by bitwise sum operation or keep some bits clear and 0 keep and 1 set or 1 exclusive or reverse (both 0 and different 1) is specified as 1

  • demo1: enter an integer to convert it to a 32-bit binary representation:
  • demo2: input decimal to hexadecimal
2. compilation preprocessing
  • Macro variable
    #define MAX 13 improves code readability

  • Macro function \define max (a, b) (a) > (b)? (a) : (b):

  • Differences between macro functions and user-defined functions:
    Macro function: fool replacement saves the return time of function call, as well as the allocation of space and release of formal parameters. Swap compile time for memory space and runtime

  • When to use macro functions?: Frequently used functions with short and simple functions

  • Conditional compilation
    If the definition is repeated:
    FA Yi
    #ifndef __ FUNC_H __
    #define __ FUNC_H __
    ..................
    #endif
    Law II
    #pragma once
    #include "func.h"
    Int b=5;

three Coding specification
  • Empty lines between unrelated blocks
  • Quad indent
  • One variable per line
  • for while add {} no matter how many lines
  • Space between variable and assignment
  • ...
4. pointer
  • What is the pointer? Is a variable that stores the address. Its function is to directly operate the address (access the hardware)
  • The pointer type is based on the type of address stored (type + step size)
  • Int *p =&n; P+1%p get address skip 4 bytes char skip 1 byte%x hex
  • **Is value operator & address operator
  • Multi dimensional pointer function: save the address of the previous one-dimensional pointer variable
  • Pointer assignments must be of the same type
  • -Universal pointer void*: it can accept the value of any type of pointer, and can not add and jump a byte by itself for value taking and operation
  • Pointer pointer? How many data of this type are there between
  • Wild pointer: when the pointer is not assigned a value, it will be given an address at random. Now the new version is set to null (zero address) and cannot be operated on directly. Therefore, a space should be allocated first
    ptr = char(*)malloc(sizeof((char)*100)
    if(ptr ==NULL)
    {
    exit(1)// Add a judgment
    }
    free(ptr)
    ptr=NULL / / it must be set to null, which may cause memory leakage
5. array
  • One dimensional array name is the address where the pointer constant saves the first element of the array
  • Array address: get the address of the entire array by taking the address of the array name
  • *(&a)=a
    The address value of one-dimensional array is equal to the address of the first element of one-dimensional array
  • Array pointer int (*p) [max] =&a;
  • Two dimensional array name: pointer constant saves one-dimensional array address
  • One dimensional array units are elements, and two-dimensional array units are one-dimensional arrays
  • Pointer array int *p[3];
    int main (int argc ,char *argv[])
    {
    }

Tags: C linked list pointer

Posted by bhanu on Tue, 31 May 2022 05:27:33 +0530