
c/c++
随心感悟,解析一些知识点
浩南煮面
这个作者很懒,什么都没留下…
展开
-
内联函数与宏定义区别
宏定义是对字符串的替换,所以在使用时要非常谨慎,如经典程序:#include <iostream>#include<stdlib.h>using namespace std;#define SQ(y) ((y)*(y))int main(){ int n, sq; cin >> n; sq = 200 / SQ(n + 1); cout << sq << endl; system("pause"); return 0;原创 2021-11-26 00:28:37 · 505 阅读 · 0 评论 -
动态内存分配
c语言:int *p = (int *)malloc(sizeof(int)*10);//分配10个int型的内存空间free( p ) ;//释放内存c++:int *p = new int;//分配1个int型的内存空间delete p;//释放内存int *p = new int(2);//分配1个int型的内存空间,并初始化delete p;int *p = new int[10];//分配10个int型的内存空间delete[] p;...原创 2021-11-25 22:48:57 · 237 阅读 · 0 评论 -
extern的区别
普通全局变量的作用域是当前文件,在一个文件中定义,在另一个文件中声明一下不定义就可以直接用源文件1#include<stdio.h>#include<stdlib.h>int n = 10;void func();int main(){ func(); printf("main:%d\n", n); system("pause"); return 0;}源文件2#include<stdio.h>extern int n;void fu原创 2021-11-25 22:32:32 · 107 阅读 · 0 评论 -
头文件中< >与“ ”,加不加.h的区别
c++中囊括了c,所以区分一下c和c++的语法很有必要< >:表示引用标准头文件,头文件是系统路径" ":表示使用自己写的头文件,先从当前文件夹中找,找不到再从系统库环境中找加.h:编程时不用再加命名空间不加.h:编程时要加上命名空间...原创 2021-11-23 08:49:00 · 641 阅读 · 0 评论 -
命名空间 namespace
相比c语言,c++中引入了命名空间的概念就是我们经常看到的namespace我们以一个代码片段来讲解使用Li::#include "stdio.h"#include "stdlib.h"namespace Li{ class Student{ public: char *name; int age; int score; void display(){ printf("%s,%d,%d", name, age, score); } };}int main(原创 2021-11-20 11:26:28 · 505 阅读 · 0 评论 -
结构体与类的区别
结构体与类都是各种数据类型的集合,区别是结构体里只能是各种变量类型的集合,而类除了各种变量类型外,还可以有各种函数结构体举例:#include "stdio.h"#include "stdlib.h"struct Student{ char *name; int age; int score;};void display(struct Student stu){ printf("%s,%d,%d", stu.name, stu.age, stu.score);}int main原创 2021-11-20 10:31:20 · 521 阅读 · 0 评论 -
常量指针与指针常量
常量指针指向常量的指针,指针指向可以变,指针不能变const char *p = “csdn”;p=“msdn”;//是对的*p=“msdn”//是错的指针常量指针是常量,指针不能变,指针指向可以变char * const p = “csdn”p=“msdn”;//是错的,指针只能指向“csdn”*p=“msdn”//是对的指针即p指针指向即*p...原创 2021-11-19 22:09:02 · 204 阅读 · 0 评论 -
指针数组与数组指针
指针是与关键字组合,有许多用法,在此记下,供自己理解整型数组: int arr[] = {1,2,3,4 };字符数组: char str[] = “csdn”;数组指针: 整型数组指针: int arr[] = {1,2,3,4 };int *p = arr;字符型数组指针: char str[] = “csdn”;char *p = str;指针数组: 整型指针数组: int a,b,c,d;int arr[ ] = {&a,&b,原创 2021-11-18 23:52:23 · 425 阅读 · 0 评论