顯示具有 C 標籤的文章。 顯示所有文章
顯示具有 C 標籤的文章。 顯示所有文章

2018年2月28日 星期三

C/C++面試題目


constant
https://stackoverflow.com/questions/10091825/constant-pointer-vs-pointer-on-a-constant-value

deep-c slides
https://www.slideshare.net/olvemaudal/deep-c/246

lvalue, rvalue
http://hamersun.blogspot.tw/2012/12/c-lvalues-and-rvalues.html

lvalue reference, rvalue reference
http://hamersun.blogspot.tw/2012/12/lvalue-reference-and-rvalue-reference.html

*pointer vs array[]

**pointer, *pointer vs arrar[][]

reference& vs address&

operator overloading

rules of zero/three/five

polymorphism

template

undefined behavior

memory layout
https://blog.gtwang.org/programming/memory-layout-of-c-program/

https://www.youtube.com/channel/UCIm-u7l65hp5jboSJrB7U5w/videos

2018年2月26日 星期一

C變數宣告之解讀方式與const

從右邊開始往回解讀
char * p;
p是個指標指向char
const char *p;
p是個指標指到char,這個char是常數
char const *p;
p是個指標指到一個常數char
const char * p; 等同於 char const * p;
char * const p;
p是個常數指標指向char
char a[16];中的a就像是個 char * const a;一樣
這是為什麼pointer跟array很接近的原因

2015年10月19日 星期一

qsort in c library

qsort shouldn't use a compare function like
int cmp(const void* a, const void* b){
    return *(int*)a - *(int*)b;
}
it would easily cause a overflow, like 2000000000-(-2000000000), it would  overflow and become a negative number.

instead, we should use a compare function like
int cmp(const void* a, const void* b){
    if(*(int*)a > *(int*)b)return 1;
    else if(*(int*)a < *(int*)b)return -1;
    return 0;
}

2015年9月25日 星期五

An easy way to parallel your c code by using openmp

if your code has low dependencies between each others, you can just use openmp to parallelize your code.
In ubuntu, openmp is installed defaultly, so we don't need to install anything.
In your code, add
#include <omp.h>
in headers.
And add
#pragma omp parallel for
before your for loop.
like

And compile with flag -fopenmp.
gcc ctest.c -fopenmp .....
Then it would run like this.