1.指針
2.動态配置設定記憶體
3.結構體
4.檔案讀寫
5.項目分析:電腦
6.項目分析: 字元串查找
1.指針
指針是c語言中專門用于存放存放記憶體位址的變量
指針的定義
資料類型 * 變量名,例如:int *a,定義了一個指向整型變量的指針;char *b,定義了一個指向字元型變量的指針
指針的初始化
int a = 10;
int *p = &a;//定義了一個指針變量p,它指向了a所對應的記憶體位址
char b = 'c';
char *pp;
pp = &b;//定義了一個指針變量pp,它指向了b所對應的記憶體位址
char *p = NULL;//定義了一個指向為空的指針
//以上是三種常用初始方法,定義完指針後一定要初始化,否則是很“危險”的
與指針相關的定義
int *array[10];
int (*array)[10];
指針運算

image.png
運作結果如下:

image.png
說明,指針變量是可以運算的。在本機中,定義一個指向整型變量的指針,增一運算後,指針位址偏移4個位元組,定義一個指向字元型變量的指針,增一運算後,指針位址偏移1個位元組;偏移量與定義一個整型變量或字元型變量時,配置設定的記憶體大小是相同的。
2.動态配置設定記憶體
定義指針變量後要為所指向的位址配置設定記憶體,在我們不确定配置設定多少記憶體的時候,會擔心配置設定過多,造成記憶體的浪費,配置設定過少又會不夠用,這時我們需要動态配置設定記憶體,用代碼讓計算機動态配置設定記憶體,用多少分多少。要實作動态配置設定記憶體我們需要用到及格函數:malloc,free,realloc,他們被定義在這個頭檔案中。
char *name;
name = (char*)malloc( 10*sizeof(char));
name = (char*)realloc(name,20*sizeof(char));//使用realloc為name,重新配置設定記憶體。
free(name);//釋放記憶體
結構體
結構體可以定義c語言中不存在的資料類型。
結構體的定義
struct student{//用struct 定義一個student資料類型
int age;
int sex;
char name[10];
}
struct student LiMing;//聲明一個student型的資料變量
struct student *p = &LiMing;//定義一個指針變量,指向LiMing所對應的位址
定義完的資料類型可以像一般資料類型一樣使用,和int,char等類型定義
4.檔案讀寫操作
FILE *fopen("檔案路徑/檔案名","模式");
FILE *fp = fopen("檔案路徑/檔案名","模式")

image.png
fputc(int c,FILE *fp);//向檔案寫入單個字元
fputs(char *s,int size,FILE *fp);//向檔案寫入整個字元串
fclose(fp);//關閉檔案
正确的檔案讀寫方法是将檔案寫入後關閉檔案,否則寫入的内容會留着緩沖區,不會儲存到檔案中。
5.頭檔案與實作檔案
A.建立一個空項目将它命名為電腦
B.在新項目下建立幾個檔案
C.分别命名為mian.c;Calculator.c,Calculator.h
D.在每個檔案中寫入以下代碼:
//Calculator.h
#include
int add(int,int);
int minus(int,int);
int multiply(int,int);
int devide(int,int);
//Calculator.c
#include
#include "Calculator.h"
int add(int a,int b){
return a+b;
}
int minus(int a,int b){
return a-b;
}
int multiply(int a,int b){
return a*b;
}
int devide(int a,int b){
if (b != 0){
return a / b;
}else{
return 0;
}
//main.c
#include
#include "Calculator.h"
int main(){
printf("%d\n",add(1,2));
printf("%d\n",minus(5,3));
printf("%d\n", multiply(9,7));
printf("%d\n",devide(4,2));
return 0;
}
項目中main()函數為入口,到計算函數時,計算從頭檔案中查聲明,到對應.c檔案中查找函數主體,通過函數主體運算将運算結果傳回給main函數
6.項目分析: 字元串查找
使用同樣的方法建立一個查找字元串的項目
//myString.c
#include
#include "myString.h"
void myScanf(char *p){
int i = 0;
while (1) {
char c = getchar();
if (c == '\n') {
p[i] = '\0';
break;
}
p[i] = c;
i++;
}
}
void input(char *p, char *des){
//提示使用者操作
printf("%s:", des);
//輸入語句
myScanf(p);
}
//計算字元串的長度
int length(char *p){
int i = 0;
//for (; p[i] != '\0'; i++);
while (1) {
if (p[i] == '\0') {
break;
}
i++;
}
return i;
}
int find(char *sentence, char *word){
//1.擷取兩個字元串的長度
int sLength = length(sentence);
int wLength = length(word);
//2.判斷查詢的字元串長度是否比句子短
if (sLength < wLength) {
return 0;
}
int start = 0;
int count = 0;
for (int i = 0; i < sLength; i++) {
//記錄目前開始的位置
start = i;
//從目前位置開始去和查找的單詞進行比較
int j = 0;
for(; j < wLength; j++){
//判斷j對應的值和start+j比較
if (sentence[start+j] != word[j]) {
break;
}
}
//判斷怎麼出來的
if (j == wLength){
//都相同
//将i的值定位到start+j的位置
i = start + j-1;
count++;
}
}
return count;
}
//main.c
#include
#include "myString.h"
int main(int argc, const char * argv[]) {
char sentence[100] = {};
char word[20] = {};
input(sentence, "請輸入語句");
input(word, "請輸入查找的單詞");
int count = find(sentence, word);
printf("出現%d次\n", count);
return 0;
}
//myString.h
#include
void myScanf(char *p);
void input(char *p, char *des);
int length(char *p);
int find(char *sentence, char *word);
實作結果如下:

image.png