顯示具有 程式語言 標籤的文章。 顯示所有文章
顯示具有 程式語言 標籤的文章。 顯示所有文章

2021年1月11日 星期一

C語言 - 字串反轉

void reverse(char str[]){

int i=0,j=0;

while(str[i]!='\0'){

i++;

}

printf("轉換後的字串: ");

for(j=i-1;j>=0;j--){

printf("%c",str[j]);

}

printf("\n");

}

int main()

{

    printf("Hello World \n");

    

    char Input[50] = {"Hello World"};

    

    printf("%c, %c \n", Input[0],Input[1]);

    

    

    reverse(Input);


    return 0;

}




輸出結果:

Hello World                                                                                                                                    
H, e                                                                                                                                           
轉換後的字串: dlroW olleH 

2016年10月7日 星期五

32位元位址移到64位元環境造成死機怎修正

簡易改法
把原本定義成32位元的位址空間 改用intptr_t


Why?
stdint.h 內定義了 intptr_t 這個type
當你需要把pointer 轉型成 integer 的時候 你不確定是32 or 64bit
就可以使用intptr_t


if偵測到是64bit
intptr_t 就是long int  ( 64位元的long int 是64bit, 但32位元的long int是32bit)
否則就是 int   ( 64位元的 int 是32bit, 32位元的int 也是32bit)


此外size_t 定義成unsigned integer
而且是平台相關的
在64bit machine  size_t 是unsigned long
但在32bit machine size_t 是unsigned int


Reference:
http://nickhung0807.blogspot.tw/2014/05/blog-post.html

2015年6月9日 星期二

C語言 - how to check big or little endian

Q: big endian vs little endian, how to check ?

sample code:
int main(void)
{
    short int a = 0x1234;
    char *p = (char *)&a;
   
    printf("p=%#hhx\n",*p);

    if(*p == 0x34)
        printf("Little endian \n");
    else if(*p == 0x12)
        printf("Big endian \n");
    else
        printf("Unknow endian \n");

    return 0;
}

C語言 - sizeof(), strlen()


static void tim_test(void)
{
    char str1[]="12345\n";
    char *p1=str1;

    UINT8 a,b,c,d,e;

    a=sizeof(str1);
    b=sizeof(p1);
    c=sizeof(*p1);

    d=strlen(str1);
    e=strlen(p1);

    libc_printf("%s(): a=%u, b=%u, c=%u, d=%u, e=%u \n",__FUNCTION__,a,b,c,d,e);
 
}

/*
sizeof(str1)=? 7  ("有"包括terminated null character)
sizeof(p1)=?  4 (指標變數)
sizeof(*p1)=? 1   char型態的指標 1 byte
*/

--
程式輸出:
tim_test(): a=7, b=4, c=1, d=6, e=6

C語言 - printf str pointer

void tim_test_str_printf(void)
{
    char *str = "abcde";

    libc_printf("%s(): str = %s, *str=%c \n",__FUNCTION__,str,*str);

}

/*
*str --> 1 byte, char型態的指標
%s: 印出string
%c: 印出一個character
*/


程式輸出:
tim_test_str_printf(): str = abcde, *str=a

--
面試被問到 紀錄一下

2014年3月22日 星期六

secure coding

最近公司在推行這個,雖然很煩瑣,但其實有些不錯又常常忽略導致埋下不定時炸彈小地方值得注意. 整理好來分享下好了

2013年4月25日 星期四

C語言 - gcc: error: macro names must be identifiers

這個錯誤是指說當code中使用 #ifdef XXX 時 XXX不可以為數字開頭

The #ifdef directive is used to check if a pre-processor symbol is defined. The standard (C11 6.4.2 Identifiers) mandates that identifiers must not start with a digit.

ex.
#ifdef 123_SUPPORT ->  /* error!!! */
.
.
.
#endif

正確的:

#ifdef TEST_123_SUPPORT -> /* ok!!!! */
.
.
.
#endif

Reference:

2013年3月18日 星期一

C語言 - i++ vs. ++i

結果而言是相同,只是過程不同

i++:是先顯示i後再去做+1
++i:則是先做+1後再顯示

[1] [2]有例子寫得很好

以compiler而言,++i的效能會比較好[3]

[1] http://lagunawang.pixnet.net/blog/post/10425717-i%2B%2B-%E8%88%87-%2B%2Bi-%E7%9A%84%E5%B7%AE%E5%88%A5
[2] http://blog.roodo.com/sayaku/archives/14912893.html
[3] http://www.programmer-club.com.tw/showSameTitleN/homework/4354.html

2012年10月12日 星期五

VC++ - char[]轉CString

CString m_bKeyName;
char key_file[256]

/* copy from char[] to CString */
m_bKeyName = key_file;

Reference:
http://www.programmer-club.com.tw/ShowSameTitleN/vc/14293.html

MFC - Checkbox

/* IDC_CHECK_1: check box's ID */
CButton* bCheck1Obj=(CButton*)GetDlgItem(IDC_CHECK_1);

if(bCheck1Obj->GetCheck() == BST_CHECKED)
{
          /* current state is checked. */
}
else if(bCheck1Obj->GetCheck() == BST_UNCHECKED)
{
         /* current state is un-checked. */
}
else
{

}

/* 改變checkbox的state, 用SetCheck() */
bCheck1Obj->用SetCheck(BST_CHECKED); /* 選取checkbox */


bCheck1Obj->用SetCheck(BST_UNCHECKED); /* 選取checkbox */

Reference:
[1] http://mqjing.blogspot.tw/2007/04/mfc-checkbox.html
[2] http://msdn.microsoft.com/en-us/library/31deb72x(v=vs.80).aspx

2012年8月27日 星期一

C語言 - #include<> vs. #include” ”

#INCLUDE 前置處理有兩種形式:

<1> #INCLUDE <檔名>:
指示 compiler 只到系統的INCLUDE目錄去找要含括的檔案。這種形式主要適用來函括標準函式庫的標頭檔。EX: #include < aa.h >

<2>#INCLUDE "檔名":
指示compiler 先到目前的工作目錄去找,若找不到,再到系統的INCLUDE目錄去找。
EX: #include "Userdef.h"

Reference:
http://tw.myblog.yahoo.com/josh-chang/article?mid=1406&next=1348&l=f&fid=8

2012年6月16日 星期六

C語言 - gcc: warning: no newline at end of file


C++標準規定,一個源文件,除非是空的,否則最後一行必須是空行。這不是為了語法分析,而是為了連接文件的時候方便。

解決方式:試著在程式最後一行多按一個enter,存檔即可解決


References:
[1] http://cross-script.blogspot.tw/2011/07/gcc-warning-no-newline-at-end-of-file.html
[2] http://luabear.blogspot.tw/2009/04/warningno-newline-at-end-of-file.html
[3] http://blog.xuite.net/casio515/blog/21528505

2012年6月3日 星期日

C語言 - 簡單小考題(2) - C強制轉型

What's the output in this function?

void foo(void)
{
    unsigned int a = 6;
    int b = -20;
    (a+b > 6) ? puts("> 6") : puts("<= 6");
}


The answer is "> 6"
Why?

原因是當表達式中存在有符號類型和無符號類型時所有的操作數都自動轉換為無符號類型(unsigned)。因此-20變成了一個非常大的正整數,所以該表達式計算出的結果大於6

Reference:

2012年5月22日 星期二

C語言 - assert()

#include "assert.h"  /* include .h file */

void assert(int expression);
主要是用來除錯。在這個macro中當expression為FALSE的時候就直接結束程式,並且輸出一些必要的資訊 /* 輸出哪些資訊可以參考Reference */ 而這應該大多都用只是純粹RD內部開發code時debug用。

如果今天不想要release的code會因為觸發assert()莫名錯誤而dump出些莫名資訊給客戶,就只要在include "assert.h"之前加上#define NDEBUG就好,compile的時候就會略過assert() [2]


References:
[1] http://www.cplusplus.com/reference/clibrary/cassert/assert/
[2] http://en.cppreference.com/w/cpp/error/assert
[3] http://guguclock.blogspot.com/2008/09/assert.html

2012年2月27日 星期一

C語言 - 簡單小考題(1)

void main(void)
{
  int a=5, b=0, c=6;

  a=(a=b)&&(c=b);

  printf("a=%d, b=%d, c=%d \n",a,b,c);
}


最後答案是a=0,b=0,c=6



Q:為什麼最後答案c不是=0???

答案是因為&&在做怪。
&&前面如果是0的話就不會做後面的了,ex. A&&B, if A==0, B就會skip掉判斷

Note: OR (A||B) 也是一樣

2012年2月21日 星期二

字串處理

被這個卡了半天,實在是太可恥了。趕快來寫下心得筆記

字串處理有幾個主要的方式。分別討論如下:

1)字串長度
2)字串複製
3)字串串連

1)字串長度
size_t strlen(const char*); /* size_t 為unsigned integer. but will be unsigned long in 64bit system */

/* sample code */

void main(void)
{
size_t string_len = 0;
char *string = "AlubaGood";

string_len = strlen(string);
printf("Lengtg of input string = %u \n\r",string_len);
}

如上sample code, 我們會得到string_len = 9


2)字串複製
如果要進行字串複製,可以使用strcpy()函式,若要複製字串中若干字元內容,則可以使用strncpy():
char* strcpy(char*, const char*);
char* strncpy(char*, const char*, size_t);

/* sample code */
void main(void)
{
char *string_1="abcdefg";
char *string_2="hijklmn";

strcpy(string_1,string_2);
printf("After copy, string_1=%s, string_2=%s \n\r",string_1,string_2);
}

上面的結果會show出 After copy , string_1=hijklmn, string_2=hijklmn
strcpy()是會把source string整個「蓋掉」destinaion string. 如果只是想要copy「某些長度」的string的話則要用strncpy()

上面的code如果把strcpy()換成strncpy(string_1,string_2,3); 則會show出 After copy, string_1=hij, string_2=hijklmn


3)字串串連
如果今天要做的是把兩個字串「相連/相加」在一起的話則是要用strcat(), 相連特定長度的話用strncat()

/* sample code */
void main(void)
{
char *string_1="abcdefg";
char *string_2="hijklmn";

strcat(string_1,string_2);
printf("After connect, string_1=%s, string_2=%s \n\r",string_1,string_2);
}
上面的結果會show出 After copy , string_1=abcdefghijklmn, string_2=hijklmn

如果把strcat()換成strncat(string_1,string_2,3); 則上面結果會變成 After copy , string_1=abcdefghij, string_2=hijklmn


Reference:
http://caterpillar.onlyfun.net/Gossip/CGossip/StringLengthCopyCat.html
http://www.cplusplus.com/reference/clibrary/cstring/strncat/

2012年2月3日 星期五

C語言- 一行搞定判斷是不是2的n次方

if_power_of_2(n) (n != 0 && ((n & (n -1)) == 0))

Reference: http://nano-chicken.blogspot.com/2011/03/in-c.html

C語言- 三行搞定swap

剛看到的一段code,真的很神!! 快來筆記一下
用XOR作swap;

void swap(int *x, int *y) {
    if (x != y) {
        *x ^= *y;
        *y ^= *x;
        *x ^= *y;
    }
}

Reference:
[1] http://nano-chicken.blogspot.com/2011/03/in-c.html
[2] http://en.wikipedia.org/wiki/Operators_in_C_and_C%2B%2B

2012年2月1日 星期三

C語言- structure pointer

struct Data
{
int a;
int b;
char* name;
}Mydata;

struct Data *ptr = NULL;

/* assign pointer to structure parameter Mydata */
ptr = &Mydata;


如果要存取structure parameter a;b;*name, 可以用 ptr->a; ptr->b;ptr->name

C語言- dynamic memory allocation

void *malloc(size_t size);
http://www.cplusplus.com/reference/clibrary/cstdlib/malloc/

void free(void *ptr);
http://www.cplusplus.com/reference/clibrary/cstdlib/free/

透過malloc()所分配出來的空間必須由使用者呼叫free()才能歸還給系統。初學者常犯的錯誤之一,就是忘了用free()歸還空間,這會造成程式佔用太多記憶體,此現象稱為memory leakage。相反的,如果空間已用free()歸還了,卻還試著去使用那塊記憶體,則會發生Segmentation Fault (core dumped)的錯誤。


Reference:
http://programming.im.ncnu.edu.tw/Chapter13.htm