随笔分类 - C++学习路程
珍爱C++,远离加班。
摘要:main.cpp源码如下:#includeiostream/*如果没用extern "C"包含#include "My.h",编译器会报错*//*如果不用extern "C"包含#include "My.h",将My.c的后缀名改为cpp(My.cpp)也能编译成功*/ //extern "C"//{ #include "My.h"//}void main(){ Print();}My.h源码如下://如果在此处声明为extern "C" void Print(); 那么在main.cpp中也不需要用extern "C"包含#include "My.h"extern void Print
阅读全文
摘要:1.string Fun1(){ string num="ly"; return num;} //在程序中使用此方法不会报错,因为方法执行完毕以后虽然num被释放,但是num的值会被保存到一个临时存储单元,然后从临时存储单元复制到调用此方法给其赋值的变量中去(如:在main方法中:string mynum=Fun1(),num的值就会被保存到mynum)2.string& Fun2() { string num="ly"; return num; } //在程序中使用此方法会报错,因为返回的是对num的引用,而Fun2()方法执行完毕以后num被释放了,所以返回的引用试图引用已经被
阅读全文
摘要:// strtref.cpp -- using structure references#include iostreamusing namespace std;void Fun(const int *num){ //num=435; cout *num",const fun"endl;}void Fun(int *num){ //num=435; cout *num",not const fun"endl;}int main(){ const int num=999; const int *p=# int avg=123; int *p2=&avg;
阅读全文
摘要:#include "stdio.h"#include "my.h"void Sqrt(int &num){ num*=num;}void main(){ int count=5; const int *p=&count; const int *&r=p; //*r=123; 这一句编译器会报错 printf("%d\n",count);} void*& Fun(){ int t=123; int *p=&t; int *&r=p; return (void*&)r;} 改变书上的例子:// strtref.cpp -- using structure references#include i
阅读全文
摘要:#include stdarg.h#include stdio.hint sum(int num, int arg, ...); void main(){ int num=sum(5,1,2,3,4,5); printf("%d\n",num);}int sum(int num, int arg, ...){ int n,summary; va_list ap; if(num 1) return 0; n = num; va_start(ap, arg); /*init ap*/ summary = arg; while(--n 0) { summary += va_a
阅读全文
摘要:int main(){ char ch; //运行程序,直接回车,无论执行下面哪一句代码后发现ch的值是10,即换行符\n (下面两句代码值能执行一句对比结果) cin.get(ch); //ch=cin.get();}int main(){ char str[10],ch; cin.get(str,10); //运行程序,直接回车(下面两句代码值能执行一句对比结果) //cin.get(ch); //若执行这一句,执行完以后发现ch的值为-52,即未初始化 ch=cin.get(); //若执行这一句,执行完以后发现ch的值为-1,即输入流已经读取到流尾 /* 之所以出现上面的情况,是因
阅读全文
摘要:参考网址:1.http://blog.csdn.net/miyunhong/archive/2010/09/24/5903857.aspx2.http://developer.51cto.com/art/201001/180130.htm3.http://developer.51cto.com/art/201002/182348.htm4.http://tech.sina.com.cn/s/200...
阅读全文
摘要:// 指针专题.cpp : 定义控制台应用程序的入口点。//#include "stdio.h"#define Wr(num) printf("%d\n",num)void Fun(int *p){ int num=555; *p=99; p=#}void Fun2(){ int tem=123; int *pt=&tem; Fun(pt); printf("%d,%d\n...
阅读全文
摘要:C#:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 结构{ class Program { static void Main(string[] args) { A a; a.age = 111; a.arr = new int[] { 11, 22, 33 }; A a2 = a; a2.age = 99; a2.arr[0] = 66; B b = new B(); b.age = 111; b.arr = new int[] { 11, 22, 33 }
阅读全文
摘要:C/C++:#include "stdio.h"struct People{ int age; int arr[3];};void main(){ //printf("%d\n",sizeof(People)); struct People m={3,{4,5,6}}; struct People m2=m; m2.arr[2]=99; printf("%d\n",m.arr[2]);}C#:using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 结构{ class
阅读全文
摘要:#include "stdio.h"struct People{ int age; int arr[3];//这里如果不指定数组大小,在turboc中编译不能通过,但在vs2008中可以};void main(){ struct People m={3,{4,5,6}}; struct People p,p2; p.age=9; p2=p; p2.age=456; printf("%d\n",p.age); printf("%d\n",m.arr[2]); //在vs2008中,如果上面的结构定义时不指定数组元素的大小,则sizeof(People)为4 printf("%d\n",siz
阅读全文
摘要:// Test0921.cpp : 定义控制台应用程序的入口点。//#include <iostream>using namespace std; void Fun1(){ char str[6]={'h','e','l','l','o'}; //这一句编译的时候会报错“数组界限溢出”(因为数组没有空间存储\0):char str[5]="hello"; //...
阅读全文
摘要:先看一段C的代码(注意,如果在vs中编译此代码,需要将后缀名设置为.c而不是.cpp,因为后缀名为c的话vs才会使用C的编译器)#include<stdio.h>intmain(intargc,constchar*argv[]){//在C语言中const修饰的是一个只读变量,并不是一个常量constintnum=99;//这一句会编译报错。因为ANSIC规定数组定义时维度必须是常量,只读变量也是不可以的//intarr[num];int*p=#//因为num的只读仅仅是编译的限制,所以可以通过指针的方式改变其值*p=123;//输出123,说明num被改变了prin
阅读全文
摘要:#includestdio.hstruct stu{ char name[10]; int num; int age; char addr[15];}boya[2],boyb[2],*pp,*qq;void main(){ FILE *fp; char ch; int i; pp=boya; qq=boyb; if((fp=fopen("d:\\stu_list","wb+"))==NULL) { printf("Cannot open file strike any key exit!"); } printf("\ninput data\n"); for(i=0;i2;i++,pp+
阅读全文
摘要:C代码如下:#include "stdio.h"__declspec(dllexport) int Call(int (*qq)(int num),char * str){ printf(str); return qq(123);}多次验证发现在C#中传委托给C中的函数指针,如果委托不带参数则都能成功运行,但是委托一带参数不管是int参数还是string参数或者其他参数,都会报“尝试读取或写入受保护的内存。这通常指示其他内存已损坏”的错误,找了一天才找到解决方法,既在C#的委托声明上加[UnmanagedFunctionPointer(CallingConvention.Cdecl)],正确
阅读全文
摘要:编写C程序如下:#include "stdio.h"__declspec(dllexport) void MyFun(){ printf("this is a dll\n");}保存,取名为My.C运行 VS 命令提示,Cl /c 路径/My.c运行以后会生成 My.Obj,默认在vs安装文件夹的VC目录下再运行 link/dll 路径/My.obj在同一个目录会生成My.dll 在C#中调用:将dll复制到bin目录,编写如下C#代码:static void Main(string[] args) { MyFun(); } [DllImport("My.dll")] public ext
阅读全文
摘要:#include "stdio.h"#include "malloc.h"#include sys/stat.h//获取文件大小long GetSize(char* path){ //第一种方法获取文件大小 long currentPosition; long size; FILE *fp; fp=fopen(path,"rb"); /* Save the current position. */ currentPosition = ftell(fp); /* Jump to the end of the file. */ fseek( fp, 0L, SEEK_END ); /
阅读全文
摘要:http://blog.chinaunix.net/u1/37000/showart_338364.html// C语言读写文件.cpp : 定义控制台应用程序的入口点。//#include "stdio.h"#include "string.h"void write(){ FILE *fp; //只写打开或建立一个文本文件,只允许写数据 fp=fopen("D:\\c.txt","wt"); if(fp!=NULL) { char *str="C语言创建txt"; //fputs(str,fp); fwrite(str,strlen(str),1,fp); } //fflush(fp)
阅读全文
摘要:#include "stdio.h"#include "malloc.h"struct Student{ int id; char *name; };void errorFun(){ struct Student arr[5]; struct Student *parr[5]; for(int i=0;i5;i++) { struct Student stu; stu.id=i; stu.name="wq"; arr[i]=stu; parr[i]=&stu; }}void main(){ errorFun(); }
阅读全文
摘要:// 分配内存空间.cpp : 定义控制台应用程序的入口点。//#include "stdio.h"#include "malloc.h"struct Student{ int id; char *name; int score; struct Student *next;};void errorFun(){ struct Student stu={1,"wq",99}; struct Student *head,*s1; for(int i=0;i3;i++) { //这一句,只在第一次执行的时候给s分配了内存地址,第一次以后执行都是用相同的内存地址,也就是说从第一次循环过后每次都是给同一
阅读全文