
c语言
李嗷纳多
这个作者很懒,什么都没留下…
展开
-
结构体数组查询
#include <stdio.h>typedef struct{ char num; void (*func)(void);}STATE;char tests=0;void func1(){ printf("state1");}void func2(){ printf("state2");}STATE situation[6]={ {2,func1}, {3,func2}, {4,func1...原创 2020-07-10 18:17:54 · 964 阅读 · 1 评论 -
同级传递形参达不到想要效果,平级指针传递也不行
#include <stdio.h>typedef unsigned int wchar;int i=9;//全局变量,指针固定int * changeptr(int * p)//指针加1{p=p+1;return p;}void main(){ int *addr=&i; printf("%d\-%d\n",&i,i);...原创 2018-09-17 18:15:15 · 225 阅读 · 0 评论 -
二维数组malloc与free
特别适用于C语言动态字符串数组操作#include <stdio.h>typedef unsigned int wchar;#define LENGTH 10//需求:字符串数组的二维数组动态开辟//name[n][LENGTH] 。。。。。。。。。。// 。。。。。。。。。。// 。。。。。...原创 2018-09-17 18:52:11 · 5487 阅读 · 0 评论 -
static
fun.cwchar.c一个c文件调用另一个c文件的函数,包含static变量的情况是没问题的。原创 2018-09-17 19:41:49 · 151 阅读 · 0 评论 -
结构体指针必须用->
struct student{int name;int age;}student;typedef struct student* Pstu;void main(){Pstu p;//变量定义p->name=0;p.age=0;}compile:error C2231: '.age' : left operand points to 'struct', use...原创 2018-09-17 19:46:50 · 3834 阅读 · 0 评论 -
结构体强制转换
struct student{int name;int age;}student;typedef struct student* Pstu;void main(){ int a[2]={15,16}; printf("%d",((Pstu)a)->name); printf("%d",((Pstu)a)->age);}结果:还可...原创 2018-09-17 20:14:13 · 2434 阅读 · 0 评论 -
结构体指针做形参(两个结构体互换)
对比两个程序就懂啦,我真聪明,竟然想出这种直接互换结构体的方法程序一:#include <stdio.h>typedef struct{char num1;char num2;}INFO; INFO * info=NULL; INFO * info1=NULL;void changep(INFO* p1,INFO* p2){INFO *p;p=p1;...原创 2018-09-13 14:40:56 · 8097 阅读 · 2 评论 -
结构体指针替换
#include<stdio.h>typedef struct {char name;int num;}STU;void change(STU *p1,STU *p2){*p2=*p1;}void main(){ STU stu1={0}; STU temp={12,88}; printf("%d-%d\n",temp.name,te...原创 2018-09-21 15:39:20 · 512 阅读 · 0 评论 -
结构体可以直接用=互相赋值,,,!
郁闷了,原来可以直接操作,绕那么大圈void main(){ STU stu1={0,10}; STU temp={12,88};//STU * p1=&stu1;//STU * p2=&temp;printf("%d-%d\n",temp.name,temp.num); temp=stu1;printf("%d-%d\n",temp.name,...原创 2018-09-21 16:37:23 · 17633 阅读 · 2 评论 -
函数指针,与c++类的联想
#include<stdio.h>typedef void (*functionPointerType)(void);typedef struct commandstruct{ char const* name; functionPointerType Pfunction; char const *help; };void commVersi...原创 2018-10-06 15:53:20 · 228 阅读 · 0 评论 -
在mfc中添加控制台,实现printf
#include <io.h> #include <fcntl.h>void InitConsoleWindow(){AllocConsole();HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);int hCrt = _open_osfhandle((long)handle,_O_TEXT);FILE * hf...原创 2018-09-28 17:36:07 · 1226 阅读 · 0 评论 -
malloc和free在两个函数中实现
char* creat(void){ char* p=(char*)malloc(sizeof(char)*10); return p;}void del(char *p){ free(p); p=NULL;}int main(){ del(creat());} 通过指针传递, p=NULL;这句是精华,工程做的好的会把p赋...原创 2018-09-17 17:28:01 · 976 阅读 · 0 评论 -
malloc与直接定义变量的区别
//vc6.0编译环境下代码#include "stdio.h"#include "malloc.h"#define N 5 //malloc申请内存 int *function1(){ int i; int *a=(int *)malloc(sizeof(int)*N); for(i=0;i<N;i++) { a[i]=i+1;...原创 2018-09-06 13:56:28 · 2252 阅读 · 0 评论 -
#define后面为什么不加;
举个简单的例子:#define MAX 100void main(){int a=MAX;}那么#define 后面能不能加;呢?可以的,如下#define MAX 100;void main(){int a=MAX}这样同样也可以编译成功,只是这样不符合人们日常习惯原创 2017-12-13 14:54:31 · 1991 阅读 · 0 评论 -
强制转换为结构体指针的例程
#include "stdafx.h"struct stu //定义机构体{char num; char parry[10];};int main(){char a[10]="adcadcgh";//把a数组拿来做实验struct stu * pt; //定义一个结构体指针pt =原创 2018-01-15 14:07:33 · 4298 阅读 · 4 评论 -
用指针来把数组从小到大排列输出,代码,VC++
昨日见旁边一兄弟在默默练习C语言,惊奇发现他竟然用了返回指针的函数(如此高端的用法),来做数组大小顺序输出。佩服不已,自己也练了练:乍一看挺麻烦的,不过思路清晰,就很好完成。思维方式,智商在一定程度上决定了代码精巧程度,我第一个思路只能想到用返回int值来写原创 2018-01-31 11:29:12 · 2746 阅读 · 0 评论 -
msp430串口发送中断操作之简单三步
预备知识:本人用的msp430fr6989(msp430系列大同小异),datasheet中有句非常关键的一段话:30.3.15.1 UART Transmit Interrupt OperationThe UCTXIFG interrupt flag is set by the transmitter to indicate that UCAxTXBUF is ready to acce原创 2018-01-09 19:46:57 · 6359 阅读 · 5 评论 -
msp430的串口在LPM3下才能工作,例程
删除__low_power_mode_3()后,不能进入中断服务程序;必须加上这句 __low_power_mode_3()后,进入LPM3模式,才能进入串口中断服务程序。void UART_send_str(uint8_t* data,uint8_t length)//发送一个字符串的函数{ uart_info.data=data;原创 2018-01-10 16:44:29 · 1241 阅读 · 0 评论 -
strstr用法,代码例程
#include#includevoid main(){char a[]="adv";char b[]="jjadvlj";printf("%s\n",strstr(b,a));//在b中找到a部分,并return它}在vc++6.0环境下编译运行,显示原创 2018-02-07 09:57:13 · 372 阅读 · 0 评论 -
__FILE__,__func__,__LINE__用法举例
C语言中,__FILE__,__func__,__LINE__常用于logout,打trace,debug调试。注意:其使用不需要定义,__FILE__指示当前文件名__func__指示当前函数名__LINE__指示运行当前文件的行数。实验环境:Windows 系统 vc6.0// 555.cpp : Defines the entry point for the console applicat...原创 2018-03-02 15:45:46 · 19983 阅读 · 0 评论 -
typedef函数指针最常见用法
在这之前需知道函数指针的用法,比如:#include "stdafx.h"void test(){printf("Hello World!\n");}int main(){ void (*fun)()=&test;//此处test前面加不加&都能运行正确 (*fun)(); return 0;}typedef常用于定义一组函数名,比如一个工程里的几个主要task函数。typedef...原创 2018-03-05 11:41:01 · 967 阅读 · 0 评论 -
一道简单的字符指针笔试题
一下代码纠错:int main(void){char *s="AAA";printf("%s",s);s[0]='B';printf("%s",s);return 0;}s指针指向字符串变量,不可以被改变。当s指向数组时,可以改变。原创 2018-03-19 15:20:26 · 461 阅读 · 1 评论 -
c++设计者模式,串口中断举例
其实这玩意真的简单,但是网上资料都看不明白,如果新手在工作中遇到了这种设计模式的软件,直接问源代码作者吧,他随便讲几句话就明白了。百度上的资料都是抄来吵去,辣鸡。而且代码又臭又长,根本看不懂,本文也很可能看不懂。正文:在我的实际工程中挑选了关键代码出来,来分析串口这一块。首先定义中断类包括串口,然后串口的中断来的时候,串口中断观察者-IInterruptObserver收到一个字节,同时把它发送给...原创 2018-05-30 11:51:01 · 1263 阅读 · 0 评论 -
#ifndef用法举例说明
目的:防止一个头文件被include两次比如一个app.h文件:#ifndef _APP_H_#define _APP_H_............#endif举个极端的例子在app.c文件中:(#include app.h两次,或在某一个头文件中已经#include app.h一次)#include "app.h" //在app.h的第一行判断为是,这行语句有效#incl...原创 2017-12-19 17:56:27 · 4469 阅读 · 0 评论