随笔分类 - C语言
摘要:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台 /** * Note: The returned array must be malloced, assume caller calls free(). */ int cmp(const void * a, const void * b)
阅读全文
摘要:#include <stdio.h> int main () { char* s = "hello"; // 字符串名字就是首地址 printf("%x\n",s); // s是char指针,size = 4 or 8 printf("sizeof s is %d\n", sizeof(s)); /
阅读全文
摘要:167. 两数之和 II - 输入有序数组 - 力扣(LeetCode) /** * Note: The returned array must be malloced, assume caller calls free(). */ int* twoSum(int* numbers, int num
阅读全文
摘要:力扣(LeetCode)官网 - 全球极客挚爱的技术成长平台 /** * Note: The returned array must be malloced, assume caller calls free(). */ int* intersection(int* nums1, int nums1
阅读全文
摘要:#include <iostream> #include<stdio.h> int main() { int nums[] = { 1,1,2,2,3,4,5,6,6 }; int size = sizeof(nums) / sizeof(nums[0]); // 创建一个全0的空数组 int* c
阅读全文
摘要:26. 删除有序数组中的重复项 - 力扣(LeetCode) int checkIn(int* nums, int numsSize, int target) { for (int i = 0; i < numsSize; i++) { if (nums[i] == target) { return
阅读全文
摘要:#include <iostream> #include<stdio.h> int* removeDuplicates(int numsSize) { // malloc是常用的动态内存分配 int* arr = (int*)malloc(numsSize * sizeof(int)); retur
阅读全文
摘要:27. 移除元素 - 力扣(LeetCode) int removeElement(int* nums, int numsSize, int val){ int left = 0; int right = 0; while (right < numsSize){ if (nums[right] !=
阅读全文
摘要:int search(int* nums, int numsSize, int target){ int left = 0; int right = numsSize - 1; int middle; while (left <= right){ middle = (left + right) /
阅读全文
摘要:#include <iostream> #include<stdio.h> void prinfArray(int* nums, int size) { for (int i=0; i<size; i++) { printf("%d ", nums[i]); } printf("\n"); } in
阅读全文
摘要:内存分配的类型: 在C/C++中内存分为5个区,分别为栈区、堆区、全局/静态存储区、常量存储区、代码区。 静态内存分配:编译时分配。包括:全局、静态全局、静态局部三种变量。 动态内存分配:运行时分配。包括:栈(stack): 局部变量。堆(heap): c语言中用到的变量被动态的分配在内存中。(ma
阅读全文
摘要:#include<stdio.h> #include<string.h> //结构体指针: 指针的类型为结构体 typedef struct nodeData { int a; char b[3]; }node; int main() { //结构体数组, 数组里面的每个元素都是结构体 node n
阅读全文
摘要:// 大端存储: 数据的高位存储在内存的低地址位置 //数据0x12345678, 四字节地址0x0, 0x1,0x2,0x3 //存储方式: 0x0: 存储12, 0x1:存34 0x2: 存56 0x3 : 78大端存储 //小端存储: 数据的地位存储在内存的低地址位置 //存储方式:0x0:
阅读全文
摘要:指针常量 #include<stdio.h> #include<string.h> //常量指针:是一个指针, 定义不用初始化, 能改变指向,但是指向的内容不能被修改 const int* p; //指针常量: 是一个常量,这个常量的值是一个指针, 定义的时候必须初始化, 并且不能改变指向, 可以改
阅读全文
摘要:#include<stdio.h> #include<string.h> //指针函数: 是一个函数, 但是这个函数的返回值类型是一个指针 //函数指针: 是一个指针, 这个指针的指向是一个函数//下面是指针函数 int * fun() { int a = 10;//a 是一个局部变量 int *p
阅读全文
摘要:将两个字符串合并追加在一起, 类似于python的str1+str2 #include<stdio.h> #include<string.h> #include<stdlib.h> //字符串追加, 将两个字符串结合在一起 int main() { char str1[128] = "shunguo
阅读全文
摘要:#include<stdio.h> //gets()读取字符串, 可以读取空格 int main() { char num[2] = "";//gets 也会造成内存污染 , 设置字符串长度是2, 但是如果长度超过了2还是会打印, 所以这样就会造成内存污染 gets(num);// ()里面的参数要
阅读全文
摘要:静态函数 #include<stdio.h> //这是静态函数, 静态函数只能在当前文件调用,其他文件下面的函数是没法调用到这个函数的 static void fun1() { printf("hello world"); } //如果其他文件想调用这个静态函数fun1(), 可以这样写 void
阅读全文
摘要:指针作为函数的形参, 可以改变实参的值。 #include<stdio.h> // 交换两个变量的值 int swap(int x, int y) { int k = y; y = x; x = k; printf("x is %d, y is %d",x,y); return x, y; } in
阅读全文
摘要:#include<stdio.h> int main { //对一个表达式加* , 就会对表达式减一级星花*, //如果对表达式取&, 就会加一级* int* p; int** q; q = &p //这个等式是成立的,因为q是**类型看, p是*类型, 加上&, 就变成了**。(如果对表达式取*,
阅读全文