Time of Update: 2018-12-08
代碼如下所示:複製代碼 代碼如下:// 遞迴實現字串反轉 char *reverse(char *str) { if( !str ) { return NULL; } int len = strlen(str); if( len > 1 ) { char ctemp =str[0]; str[0] = str[len-1]; str[len-1] = '/0';
Time of Update: 2018-12-08
在物件導向的C++語言中,虛函數(virtual function)是一個非常重要的概念。因為它充分體現 了物件導向思想中的繼承和多態性這兩大特性,在C++語言裡應用極廣。比如在微軟的MFC類庫中,你會發現很多函數都有virtual關鍵字,也就是說, 它們都是虛函數。難怪有人甚至稱虛函數是C++語言的精髓。 那麼,什麼是虛函數呢,我們先來看看微軟的解釋: 虛函數是指一個類中你希望重載的成員函數,當你用一個基類指標或引用指向一個繼承類對象的時候,你調用一個虛函數,實際調用的是繼承類的版本。
Time of Update: 2018-12-08
建構函式不能是虛函數,因為在調用建構函式建立對象時,建構函式必須是確定的,所以建構函式不能是虛函數。解構函式可以是虛函數。1.父類Father.h:複製代碼 代碼如下:#pragma onceclass Father{public: Father(void); virtual ~Father(void); virtual int getCount();public: int count;};Father.cpp複製代碼 代碼如下:#include "StdAfx.h"#include
Time of Update: 2018-12-08
一、C++中的struct對C中的struct進行了擴充,它已經不再只是一個包含不同資料類型的資料結構了,它已經擷取了太多的功能。struct能包含成員函數嗎? 能!struct能繼承嗎? 能!!struct能實現多態嗎? 能!!! 最本質的一個區別就是預設的存取控制,體現在兩個方面:1)預設的繼承存取權限。struct是public的,class是private的。 寫如下的代碼:複製代碼 代碼如下:struct A{ char a;};struct B : A{ char
Time of Update: 2018-12-08
volatile 的意思是“易失的,易改變的”。這個限定詞的含義是向編譯器指明變數的內容可能會由於其他程式的修改而變化。通常在程式中申明了一個變數時,編譯器會盡量把它存放在通用寄存器中,例如ebx。當CPU把其值放到ebx中後就不會再關心對應記憶體中的值。若此時其他程式(例如核心程式或一個中斷)修改了記憶體中它的值,ebx中的值並不會隨之更新。為瞭解決這種情況就建立了volatile限定詞,讓代碼在引用該變數時一定要從指定位置取得其值。關鍵字volatile有什麼含意?並給出三個不同的例子。
Time of Update: 2018-12-08
代碼如下所示:複製代碼 代碼如下:/* * 冒泡排序 */void BubbleSort(int arr[], int n){ int temp; for (int i = 0; i < n - 1; i++) { for (int j = i + 1; j < n; j++) { if (arr[i] > arr[j]) { temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; } } }}/* *
Time of Update: 2018-12-08
不知道何時起, 非常刻意避免在 for 迴圈體內改變變數值. 似乎是受別人觀點影響, 但卻並不知曉原因.可是有時候用其他方法替代卻不方便, 自己試了一下, 或許找到了一絲差異.用這種方法賦值時, 沒有出現問題:複製代碼 代碼如下:#include <stdio.h>int main(){ int i; for(i=0; i<10; i++) { i = i+2; printf("%d/n", i); } return 0;}但是另外一種賦值方法, 卻是不行的.複製代碼
Time of Update: 2018-12-08
代碼如下所示:複製代碼 代碼如下:#include <stdio.h>int main(){ int pos = 2; int neg = -2; int mid = 0; if(pos) printf("pos./n"); if(neg) printf("neg./n"); if(mid)
Time of Update: 2018-12-08
很多時候,使用一些特別的庫, 在編譯可執行程式時, 需要添加額外的 CFLAGS 和 LIBS . 否則會提示找不到指定的標頭檔或者"undefined reference to ..." 的錯誤資訊.假如程式 test.c 中使用了 libxml 的 api, 直接複製代碼 代碼如下:$ gcc -Wall -o test test.c會提示錯誤訊息. 執行複製代碼 代碼如下:$ ls /usr/lib/pkgconfig/ | grep libxmllibxml-2.0.pc複製代碼
Time of Update: 2018-12-08
如以下代碼所示:複製代碼 代碼如下:#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/types.h>#include <sys/wait.h>#include <unistd.h>#include <errno.h>int main(int argc, char *argv[]){ pid_t pid; int stat;
Time of Update: 2018-12-08
網上有 md5.c , md5.h, 但是裡面只有 MD5Init(), MD5Update(), MD5Final() 三個函數, 只可以直接對字元進行操作, 而沒有直接求檔案md5的介面. 以下是我的實現, 可計算32位和16位的md5值.複製代碼 代碼如下:#include <stdio.h>#include <stdlib.h>#include <string.h>#include "md5.h"char *MD5_file (char *path,
Time of Update: 2018-12-08
1、linux c代碼實現複製代碼 代碼如下:#include <netdb.h>#include <stdio.h> #include <stdlib.h>#include <string.h>#include <unistd.h>#include <arpa/inet.h>#include <netinet/in.h>#include <sys/socket.h>typedef enum
Time of Update: 2018-12-08
個人實現例子:複製代碼 代碼如下:#include <stdio.h>#include <string.h>#define M 4int add(int a, int b);int sub(int a, int b);int mul(int a, int b);int div(int a, int b);int (*oper_func[])(int, int) = { add, sub, mul, div};char oper_sequence[M][10] = {
Time of Update: 2018-12-08
C++ 作為Client端view plaincopy to clipboardprint?複製代碼 代碼如下:// Client.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include #pragma comment(lib,"ws2_32.lib") #define MAX_BUF_SIZE 1024
Time of Update: 2018-12-08
今天用ftruncate截斷檔案, 但怎麼都不能達到預料的效果, 截斷後檔案中的內容比較雜, 而且檔案大小也保持原來的.添加 fflush() 和 rewind() 後OK.以下是測試代碼:複製代碼 代碼如下:#include <stdio.h>#include <sys/types.h>#include <unistd.h>int main(){ FILE *fp; char *file = "tmp"; int i; int fd; fp = fopen(
Time of Update: 2018-12-08
1、字元數組的定義與初始化字元數組的初始化,最容易理解的方式就是逐個字元賦給數組中各元素。char str[10]={ 'I',' ','a','m',' ',‘h','a','p','p','y'};即把10個字元分別賦給str[0]到str[9]10個元素如果花括弧中提供的字元個數大於數組長度,則按語法錯誤處理;若小於數組長度,則只將這些字元數組中前面那些元素,其餘的元素自動定為空白字元(即 '\0'
Time of Update: 2018-12-08
1、system(執行shell 命令)相關函數 fork,execve,waitpid,popen表標頭檔 #include<stdlib.h>定義函數 int system(const char * string);函數說明 system()會調用fork()產生子進程,由子進程來調用/bin/sh-cstring來執行參數string字串所代表的命令,此命令執行完後隨即返回原調用的進程。在調用system()期間SIGCHLD 訊號會被暫時擱置,SIGINT和SIGQUIT
Time of Update: 2018-12-08
先看下面一個例子a.c :複製代碼 代碼如下:int main(int argc, char *argv[]){ fprintf(stdout, "normal\n"); fprintf(stderr, "bad\n"); return 0;}$ ./anormalbad$ ./a > tmp 2>&1$ cat tmpbadtmp我們看到, 重新導向到一個檔案後, bad 到了 normal 的前面.原因如下:複製代碼 代碼如下:"The stream stderr is
Time of Update: 2018-12-08
fflush用於清空緩衝流,雖然一般感覺不到,但是預設printf是緩衝輸出的。 fflush(stdout),使stdout清空,就會立刻輸出所有在緩衝區的內容。 fflush(stdout)這個例子可能不太明顯,但對stdin很明顯。 如下語句: int a,c; scanf("%d",&a); c=getchar(); 輸入: 12(斷行符號) 那麼 a=12 ,c= '\n' 而: int a,c; scanf("%d",&a); fflush(stdin);
Time of Update: 2018-12-08
代碼如下所示:複製代碼 代碼如下:#include <stdio.h>#include <string.h>#include <stdlib.h>#include <signal.h>#include <unistd.h>#include <errno.h>#include <syslog.h>#include <stdarg.h>char **bak_argv;int flag = 0;void