1. 數(shù)組與指針
數(shù)組使用
二維數(shù)組
int ** arr;
三維數(shù)組
int *** arr;
void * 可以任意轉(zhuǎn)換成int * ..... 任意指針
遍歷和指針
int arr[] = {1,2,3};
int i=0;
for (i = 0; i < sizeof(arr) / sizeof(int); ++i) {
printf("%d\n",*(arr+i));
}
正常情況下
c語言常量不能修改,但是可以通過指針修改
const int num = 100;
int *pnum = #
*pnum = 10000;
2. 函數(shù)和指針
指針函數(shù)
void add (int num1,int num2){
printf("num1+num2=%d ",num1+num2);
}
void mins(int num1,int num2){
printf("num1-num2=%d ",num1-num2);
}
void operate(*method(int,int),int num1,num2){
method(num1,num2);
}
函數(shù)回調(diào)
void callBackMethod(char * file_name,int current,int progress){
printf("%s的圖片壓縮進度%d/%d\n",file_name,current,progress);
}
void compress(char * filename,void(* callBackP)(char *,int,int)){
callBackP(filename,50,100);
}
調(diào)用
void (* call)(char *,int,int);
call = callBackMethod;
compress("david.png",call);
3. 內(nèi)存模式
全局區(qū),
全局&靜態(tài)&常量:全局(global)變量蟀拷,靜態(tài)(static)變量,各種常量(字符串字面量争涌,const修飾的量)
代碼區(qū)嘉冒,
你寫的代碼削葱,各種函數(shù)(包括類成員函數(shù))
棧才睹,
局部變量&函數(shù)參數(shù):在函數(shù)內(nèi)聲明的變量(包括main函數(shù))
堆徘跪,
使用new,malloc砂竖,colloc等關(guān)鍵字手動開辟的內(nèi)存
磁盤,寄存器
4. 申請內(nèi)存函數(shù)區(qū)別
棧區(qū)
局部變量&函數(shù)參數(shù):在函數(shù)內(nèi)聲明的變量(包括main函數(shù))
int arr[(int) (1.5 * 1024 * 1024)];
注意棧區(qū)大小有限制比較小鹃答,根據(jù)系統(tǒng)不同不同
堆區(qū)
使用new乎澄,malloc,colloc等關(guān)鍵字手動開辟的內(nèi)存
windows 給編譯器空間比較大测摔,80% 大概置济,看實際情況
malloc:
堆中申請內(nèi)存,一般情況锋八,配合memset 對內(nèi)存初始化
alloca:
棧中申請內(nèi)存浙于,不需要師范
realloc
擴容,重置指針指向內(nèi)存大小挟纱,堆
calloc
malloc + memset羞酗,堆
靜態(tài)分配
void foo(){
int arr[5];
...
}
main函數(shù)進棧,調(diào)用foo紊服,foo函數(shù)進棧檀轨,foo執(zhí)行完畢彈出棧,釋放arr[5] 的20 空間
動態(tài)分配
char * str = malloc(100)... free(str)
void malloctest(){
int * arr = malloc(10);
printf("arr 棧%p欺嗤,堆参萄,%p\n",&arr,arr);
free(arr);
arr = NULL;
printf("2222arr 棧%p,堆煎饼,%p\n",&arr,arr);
}
5. inline的用法和解釋
參見C++ 常用小點
6. 友元函數(shù)
參見C++ 常用小點
7. 頻率比較高的函數(shù)
C語言速查
srand 隨機數(shù)
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
int main(void)
{
int i;
time_t t;
srand((unsigned) time(&t));
printf("Ten random numbers from 0 to 99\n\n");
for(i=0; i<10; i++)
printf("%d\n", rand() % 100);
return 0;
}
strcpy 拷貝函數(shù)
#include <stdio.h>
#include <string.h>
int main(void)
{
char string[10];
char *str1 = "abcdefghi";
strcpy(string, str1);
printf("%s\n", string);
return 0;
}
sleep
#include <dos.h>
#include <stdio.h>
int main(void)
{
int i;
for (i=1; i<5; i++)
{
printf("Sleeping for %d seconds\n", i);
sleep(i);
}
return 0;
}
sticmp
strcmpi
strstr
atoi
查看c文檔讹挎,小白級別
8. 字符串
char * str = "David";
char str[] = {'D','a','v','i','d','\0'}
注意‘\0’結(jié)尾
str[2] = 'z' 只能修改str[] 他是棧區(qū)
- str 不行,因為他是全局靜態(tài)區(qū),不能更改
C語言 字符串截取的幾個思路
void substr(char *result, char *str, int start, int end) {
char *temp = str;
int count = 0;
while (*temp) {
temp++;
count++;
if (count >= start && count <= end) {
*result = *temp;
result++;
}
}
}
void substr2(char *result, char *str, int start, int end) {
char *temp = str;
// 截取字符串筒溃,截取多少用多少
char resultArr[end - start];
int count = 0;
int i;
for (i = start; i < end; ++i) {
resultArr[count] = *(temp + i);
count++;
}
// *result = resultArr;//x 錯誤一級指針指向容器马篮,因為容器結(jié)束方法會棧回收
strcpy(result, resultArr);//1. copy字符串
}
void substr2_2(char **result, char *str, int start, int end) {
char *temp = str;
// 截取字符串铡羡,截取多少用多少
char resultArr[end - start];
int count = 0;
int i;
for (i = start; i < end; ++i) {
resultArr[count] = *(temp + i);
count++;
}
strcpy(*result, resultArr);//1. copy字符串
}
void substr3(char **result, char *str, int start, int end) {
char *temp = str;
// 截取字符串积蔚,截取多少用多少
char *resultArr = malloc(end - start);
int count = 0;
int i;
for (i = start; i < end; ++i) {
resultArr[count] = *(temp + i);
count++;
}
*result = resultArr;
}
void substr4(char *result, char *str, int start, int end) {
int i;
for (i = start; i < end; ++i) {
*(result++) = *(str + i);
}
}
void substr5(char **result, char *str, int start, int end) {
strncpy(*result, str+start, end-start);
}
int main() {
char *str = "David is good is better man ! Alex eat shit!";
char *result;
// substr(result,str,2,5);
// substr2(result, str, 2, 5);
// substr3(&result, str, 2, 5);
// free(result);
// substr2_2(&result, str, 2, 5);
// substr4(result, str, 2, 5);
substr5(&result, str, 2,5);
printf("result === %s", result);
}
9. 用指針獲取長度,有的時候傳遞數(shù)組sizeof不靈
int getLen(char * str){
int count=0;
while(*str){
str++;
count++;
}
return count;
}
int getLen2(int intarr[]){
int len = sizeof(intarr) / sizeof(int);
printf("getLen2 len:%d\n",len);
}
int getLen3(int * intarr){
int len = sizeof(intarr) / sizeof(int);
printf("getLen2 len:%d\n",len);
}
int getLen4(int * intarr){
int count=0;
while(*intarr){
intarr++;
count++;
}
printf("getLen2 len:%d\n",count);
return count;
}
int main(){
char str[] = {'A','B','C','\0'};
int len = getLen(str);
printf("length str:%d\n" ,len);
int arr[]={1,2,3,4,5,6,7};
getLen2(arr);
getLen3(arr);
getLen4(arr);
}
10 結(jié)構(gòu)體
struct Dog {
char name[10];
int age;
char sex;
};
struct Person {
char *name;
int age;
char sex;
} ppp = {"David", 33, 'M'};
struct Study {
char *studyContent;
};
struct Student {
char *name;
int age;
char sex;
struct Study study;
struct Wan {
char *wanContent;
} wan;
};
struct Cat{
char name[10];
int age;
};
int main() {
struct Dog dog;
strcpy(dog.name, "xiao miao");
dog.sex = 'w';
printf("name %s,%c\n", dog.name, dog.sex);
printf("name %s,%c,%d\n", ppp.name, ppp.sex, ppp.age);
struct Student student = {
"David", 'M', 33,
{
"C++"
}, {
"王者榮耀"
}
};
Struct Student *student1 = &student;
student1->age = 3;
struct Cat * cat = malloc(sizeof(struct Cat));
strcpy(cat->name,"Cat Cat");
free(cat);
cat = NULL;
return 0;
}
struct 兼容
struct Worker_{
char name[10];
int age;
char sex;
};
typedef struct Worker_ Worker_;
typedef struct Worker_ * Worker;
//=======
typdef struct Worker_{
char name[10];
int age;
char sex;
} worker;
`枚舉`類似enum的兼容
typedef enum CommenType{
TEXT = 10,
TEXT_IMAGE,
IMAGE
}CommenType;
int main() {
Worker worker;
return 0;
}
很多源碼烦周,大量使用typedef 別名尽爆,原因是為了平臺的兼容問題。
結(jié)構(gòu)體定義與使用
struct Book1{
char title[10];
char author[10];
int id;
} book = {.title="c lang",.author="av",.id=1};
struct Book book1;
strcpy(book1.title,"c++ lang");
strcpy(book1.author,"david");
book1.id = 2;
-> 指針使用
struct Book *p;
p = &book1;
printf("book id:%d\n",p->id);
printf("book title:%s\n",p->title);
printf("book author:%s\n",p->author);
printf("book id:%d\n",(*p).id);
printf("book title:%s\n",(*p).title);
printf("book author:%s\n",(*p).author);
Union 類似Struct
union Data{
int i;
float f;
char str[20];
} data ={.i=1,.f=1.0,.str="ddd"};
-> 占用內(nèi)存方式有區(qū)別
printf("%lu",sizeof(book1));
printf("%lu",sizeof(data));
11. 文件操作讀寫读慎,加解密
int main15() {
//FILE *fopen(const char * __restrict __filename, const char * __restrict __mode) __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fopen));
//r,w,rb,rw
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFile.txt";
FILE * file = fopen(filename, "r");
if(!file){
printf("文件打開失敗漱贱,請檢%s文件查路徑",filename);
exit(0);
}
char buffer[10];
while(fgets(buffer,10,file)){
printf("%s",buffer);
}
//close
fclose(file);
return 0;
}
int main17() {
//FILE *fopen(const char * __restrict __filename, const char * __restrict __mode) __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fopen));
//r,w,rb
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileW.txt";
FILE * file = fopen(filename, "w");
if(!file){
printf("文件打開失敗,請檢%s文件查路徑",filename);
exit(0);
}
fputs("David Success",file);
//close
fclose(file);
return 0;
}
int main18() {
//FILE *fopen(const char * __restrict __filename, const char * __restrict __mode) __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fopen));
//r,w,rb
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFile.txt";
char *filenameCopy = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileCopy.txt";
//讀取二進制
FILE * file = fopen(filename, "rb");
FILE * fileW = fopen(filenameCopy,"wb");
if(!file || !fileW){
printf("文件打開失敗夭委,請檢%s文件查路徑",filename);
exit(0);
}
int buf[512];
int len;//每次讀取長度
while((len = fread(buf, sizeof(int),
sizeof(buf)/ sizeof(int),file)) !=0){
fwrite(buf, sizeof(int), len, fileW);
fflush(fileW);
}
//close
fclose(file);
fclose(fileW);
return 0;
}
int main19(){
//FILE *fopen(const char * __restrict __filename, const char * __restrict __mode) __DARWIN_ALIAS_STARTING(__MAC_10_6, __IPHONE_2_0, __DARWIN_ALIAS(fopen));
//r,w,rb
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFile.txt";
//讀取二進制
FILE * file = fopen(filename, "rb");
if(!file){
printf("文件打開失敗幅狮,請檢%s文件查路徑",filename);
exit(0);
}
// 指針挪動讀取大小,SEEK_SET株灸,SEEK_CUR,SEEK_END
fseek(file,0,SEEK_END); //file 有了更豐富的值
long file_size = ftell(file); //file 賦值記錄信息
printf("%s文件的大小是%ld\n",filename,file_size);
//close
fclose(file);
return 0;
}
int main20(){
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFile.txt";
char *filenameEncode = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileEncode.txt";
//讀取二進制
FILE * file = fopen(filename, "rb");//文件已經(jīng)有了
FILE * fileEncode = fopen(filenameEncode, "wb");//創(chuàng)建0kb文件
if(!file || !fileEncode){
printf("文件打開失敗崇摄,請檢%s文件查路徑",filename);
exit(0);
}
//TODO
//加密,破壞文件慌烧,解密逐抑,還原之前的樣子
// 每個字節(jié)都拿出來,對每一個字節(jié)處理
// 部分加密一部分拿出來處理
// while 循環(huán)遍歷取出來的10
// 10 異或 5 變成 1111
//TODO
// 解密 1111 異或 5 還原10
int c;//接受讀取值,fget 返回EOF=end of file
while((c = fgetc(file)) != EOF ){
fputc(c ^ 5,fileEncode);
}
//close
fclose(file);
fclose(fileEncode);
return 0;
}
int main21(){
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileEncode.txt";
char *filenameDecode = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileDecode.txt";
//讀取二進制
FILE * file = fopen(filename, "rb");//文件已經(jīng)有了
FILE * fileDecode = fopen(filenameDecode, "wb");//創(chuàng)建0kb文件
if(!file || !fileDecode){
printf("文件打開失敗屹蚊,請檢%s文件查路徑",filename);
exit(0);
}
int c;//接受讀取值,fget 返回EOF=end of file
while((c = fgetc(file)) != EOF ){
fputc(c ^ 5,fileDecode);
}
//close
fclose(file);
fclose(fileDecode);
return 0;
}
int main(){
// char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFile.txt";
// char *filenameDecode = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileEncode2.txt";
char *filename = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileEncode2.txt";
char *filenameDecode = "/Users/zcw/dev/c_workspace/TempC/src/cpp/DavidFileDecode2.txt";
char * password = "123456";
//讀取二進制
FILE * file = fopen(filename, "rb");//文件已經(jīng)有了
FILE * fileDecode = fopen(filenameDecode, "wb");//創(chuàng)建0kb文件
if(!file || !fileDecode){
printf("文件打開失敗厕氨,請檢%s文件查路徑",filename);
exit(0);
}
int c;//接受讀取值,fget 返回EOF=end of file
int index = 0;
int pass_len = strlen(password);
while((c = fgetc(file)) != EOF ){
char item = password[index % pass_len];
index++;
//1 2 3 4 5 1 2 3 4 5
printf("%c\n",item);
fputc(c ^ item,fileDecode);
}
//close
fclose(file);
fclose(fileDecode);
return 0;
}