C blog work 05- pointer

0. show PTA score (0 ---- 2)

1. This chapter summarizes the study

1. Define a pointer variable

  • The general form: type name * pointer variable name
  • Type is the type name of the variable pointed pointer variable, for example:int *p;/*定义一个指针变量p,指向整型变量*/
  • To define a pointer variable is a pointer statement symbol *, represents a pointer variable is defined, where the * operator is not, so it is desired to make the processing of the pointer, the front does not need to tape *.
  • Defining a plurality of pointer variables, the variables should be added in front of each pointer *.

2. The basic operation of the pointer

  • 2.1 The address operator
    • Operator & gives the address of a variable, for example int *p,a=3; p=&a;where a variable will assign the address of shaping plastic pointer p, p is a pointer points to a variable, p is a value of the address;
    • The function scanf & address operator is, for example: scanf("%d",&a);the operation is to take the address of a variable, the stored value of the input variable into a go;
  • 2.2 indirect access operators
    • *In addition to pointer-declarator can also be used as an indirect access operator, accessing the pointer points to a value variable. For example, int *p,a=3; p=&a; printf("%d",*p);the output is 3, when the time point a, p, p, and a visit to the same cell, * p value is the value a;
  • 2.3 Mixed Operation
    • *The priority and the same increment decrement of priority, but the binding direction is right to left, so the expression p ++ is equivalent to (p ++), to find the expression p ++, and then take * (p ++) value.
    • Adding not do the same between the pointer type!
    • Pointer p plus (or minus) i is an integer possible, a rear access to the type (or front) of variable i;
    • In doing subtraction between the same type of pointer, it will produce a value of type int, meaning the value indicates how many elements are spaced between two pointers to memory locations of note are the elements, not the number of bytes , in the calculation array pointer frequently used, it can be used to find the position where a value.
  • 2.4 Assignment
    • Pointer variable assignment first and then use, with particular attention to this point! . For example, int *a; scanf("%d",a);has not been carried out for a given initial value of the pointer on the use of it, the program errors, in order to avoid harm to referenced pointer assignment caused, when defining the pointer can be first of its initial value is set to empty, can not be used value as the initial value of the pointer variable.
    • P pointer assignment p=0;and p=NULLexpressed pointer p is a null pointer, a null pointer does not point to any cell; where 0 is the value of the ASCII character NULL;

3. pointers and arrays

3.1 Relationship between arrays and pointers

int *p;
int a[10];
p=a;
  • In that chapter we learned the array array name indicates that the function of the continuous memory address space, the first unit of assignment, that is the first address. Here p pointer points to a first element of the array;
  • Pointer p may be from the Operational, but not the array a, where a pointer can be regarded as a constant, the constant can not be done increment of decrement;

3.2 Pointers loop variable practice
if the pointer points to an array, then the loop variable can be done with a pointer to traverse the array, for example:

/*在字符数组a中查找字符ch*/
定义字符指针p;  
定义字符ch;
定义字符数组a[10];
定义变量pos保存查找到的ch的位置,初始化为-1

输入字符ch;
p=a;//使指针指向数组第一个元素;
for p to p指向a数组a的最后一个元素
  if(*p==ch)
     pos=p-a;
  end if
end for
if pos<0
   说明没有找到,输出没有找到语句;
else
   说明找到,输出位置;

operation result:

4 represents a character string pointer

  • String constant is essentially a pointer to the first character of the string constant , if one character string constant value of the received pointer, this pointer points to the first character of the string;
  • Printf function call, when the output string format% s continuously outputted from the cell start address of the specified contents, until it encounters '\ 0' so far, for example:
  • Plus or minus an integer pointer i, represents the current pointer points to the address of the i-th point before the i-th character or characters, for example:

The common string manipulation functions

Header file: #include <string.h>

6. Dynamic memory allocation

  • 6.1 allocation from the stack area (stack)
    • Storing the function parameters, local variables
    • In a function call, a local variable and the system parameter memory allocated on the stack within the function, automatically released at the end of the function execution memory.
  • 6.2 allocated from heap (heap) region
    • During program execution, dynamic memory allocation functions from the application memory is allocated on the heap, dynamic lifetime memory of the programmer.
    • Dynamic memory allocation functions

7. pointer array and its application

  • 7.1 concept
    • The general format:类型名 *数组名 [数组长度]
    • 指针数组中的每个元素都是指针类型,用于存放内存地址。
  • 7.2指针数组保存字符串
    • 数组中的每个元素保存的是字符串的首地址,每个元素分别指向一个字符串

2.PTA实验作业

2.1查找子串(函数题)

2.1.1 伪代码

/*函数search在字符串s中查找子串t,返回子串t在s中的首地址,若未找到,则返回NULL*/
定义变量i从字符串s中找到t的第一个字符开始遍历字符串s和字符串t;

while (*s)
   初始化i为0;
   while (*(s+i)==*(t+i)&&*(s+i)!='\0'&&*(t+i)!='\0')  //这里从字符串s中找到t的第一个字符开始,对i进行自增处理,保留当前s的地址,不断遍历字符串s和字符串t,直到不相等或者遍历到字符串s和t的结束标志;
          i++;
   end while
   if  遍历到字符串t的结束标志'\0'
       表示找到字符串t,直接返回s的地址;
   end if
   s++; //字符串s往下一个字符移动;
end while
程序运行到最后,还没结束,说明没有找到字符串t,返回NULL;

2.1.2 代码截图

2.1.3 总结本题的知识点

  • 设计返回指针类型的函数
    pos=search(s,t);
  • 利用两个指针相减来求出位置
    printf("%d\n", pos - s);

2.1.4 PTA提交列表及说明

编译错误:return s 语句后面忘记加分号

2.2说反话-加强版

2.2.1 伪代码

/*在函数ReverseStr中,将字符串中的每个单词倒序输出*/
定义字符指针endPtr用来寻找字符串的最后一个字符,首先对endptr赋初值为beginPtr
定义字符指针p;
定义变量len来保存单词长度;
定义变量flag来判断是否为要输出的第一个单词,对其赋初值为1;

while(*endPtr!='\n'&&*endPtr)  //逆向扫描到最后一个字符
     endPtr++;
end while
p=--endPtr; //使p指向最后一个非结束符的字符;

while(p!=beginPtr) //当p指向短剑的首地址时结束循环
  若*p不是空格,则统计单词长度;len++
  若*p不是空格,但前一个是空格,则找到单词
     根据flag的值判断是否为第一个要输出的单词,输出从p开始的len长度字符串;
     len=0;重新开始找单词;
   p--
end while

如果第一个字符(*beginPtr)不是空格的话,要考虑到短句中第一个单词的输出。

2.2.2 代码截图


2.2.3 总结本题的知识点

  • 如何查找单词
    即当前字符非空格而前一个字符为空格;
  • 逆向扫描字符
    while(p!=beginPtr) {p--}
  • 字符串指针表示字符串
    printf("%.s",len,str);

2.2.4 PTA提交列表及说明

部分正确:短句中第一个单词前面没有空格时,第一个单词不会输出;
部分正确:短句中第一个单词的输出有问题,长度判断错误导致第一个单词的最后一个字符无法输出;
部分正确:最长句子,字符数组的长度应该为500002以上。

2.3删除字符串中的子串

2.3.1 伪代码

/*在函数DeleteB中删除子串*/
定义指针ptra指向a的首地址,后边对ptra进行操作;
定义字符数组c来保存字符串中出现的子串后面的字符;
定义指针locPtr来表示出现子串的位置;
定义lenB来保存子串B的长度;

while(loPtr=strstr(a,b)!=NULL)
    ptra=locPtr;
    ptra='\0';//因为在使用strcat函数时,被连接的字符串结尾应该要有结束标志'\0'
    strcpy(c,ptra+lenB); //用字符数组c来保存后面的字符;
    strcat(ptra,c); //将子串删去,连接除去子串后的剩下的字符;
end while
输出字符串a

2.3.2 代码截图

2.3.3 总结本题的知识点

  • 掌握字符串处理函数
    strstr函数可以帮找到子串,熟练掌握字符串处理函数可以帮助我们省略很多代码;
  • 这里也要区分输入函数,gets函数和fgets函数,用fgets函数吸收字符串时,如果长度小于可输入的最大长度,字符串结尾会带换行符,在使用字符串处理函数时要把换行符去掉;

2.3.4 PTA提交列表及说明

部分错误:最长字符串错误,数组长度没有设置好;
多种错误:格式错误,最后输出语句换行了,没有处理好换行符;
编译错误:pta中不承认gets_s函数,我从vs复制过来时忘记改了;
部分错误:自己定义了一个找子串的函数,然后运行超时。
部分错误:不熟悉字符串处理函数,在找到子串的位置应该加上'\0',否则在进行函数连接时会出错;

3.阅读代码

代码优点

  • 1.运用了dict

Guess you like

Origin www.cnblogs.com/xianerbian/p/11964945.html