2009年8月28日 星期五

strstr

strstr(const char *s1, const char *s2);
find s2 from s1, search until 0
ex:
char a[100] = "bbbbaaaa";
a[2]= 0;
printf("%d\n", strstr(a, "a"));
---> can not find


2009年2月25日 星期三

friend class

ex:
if A class wants B class can access A's protected method,
A must define B as its friend class

2009年2月1日 星期日

void parameter

in c,
int a()  &  int a(void) are different

a() can accept an infinite number of parameters,
a(void) can accept zero parameter

2009年1月19日 星期一

function pointer in c++

testFunPtr is not a static function 
testFunPtr2 is a static function

correct:
int (com_scsi_command_driver::*pt2Function)()=       &com_scsi_command_driver::testFunPtr;
int (*pt2Function)()= 
&com_scsi_command_driver::testFunPtr2;

wrong:
int (*pt2Function)()= &com_scsi_command_driver::testFunPtr;




2009年1月14日 星期三

file IO

in fcntl.h
O_RDONLY

2008年12月28日 星期日

typeof

get type:

ex:
typeof(int) intVar1=1;
typeof(intVar1) intVar2=intVar1;
typeof(int*) intPtrVar1=&intVar1;
typeof(intPtrVar1) intPtrVar2=intPtrVar1;
printf("%d %d %d %d\n", intVar1, intVar2, *intPtrVar1, *intPtrVar2);


result: 1 1 1 1

2008年10月31日 星期五

pragma pack

pragma pack(1):

#pragma pack()
回到預設值,在32-bit的系統上預設為pack(4)

2008年10月30日 星期四

little endian & big endian

how to know big endian or little endian?


#include

#ifdef __BYTE_ORDER
# if __BYTE_ORDER == __LITTLE_ENDIAN
# define I_AM_LITTLE_ENDIAN
# else
# if __BYTE_ORDER == __BIG_ENDIAN
# define I_AM_BIG_ENDIAN
# else
Error: unknown byte order!
# endif
# endif
#endif /* __BYTE_ORDER */

uint64_t

include stdint.h

typedef signed char int8_t
typedef unsigned char uint8_t
typedef signed int int16_t
typedef unsigned int uint16_t
typedef signed long int int32_t
typedef unsigned long int uint32_t
typedef signed long long int int64_t
typedef unsigned long long int uint64_t

2008年9月11日 星期四

the website about c

http://www.cprogramming.com

coding book

Writing Secure Code

code complete 

2008年9月4日 星期四

static function

in c:
the scope of function is limited to the file

2008年7月29日 星期二

size for array & pointer

char str[100];
char *str2=str;
sizeof(str)  
--->   100
sizeof(str2)
---->  4

2008年7月20日 星期日

%u

%u:  for unsigned int

2008年7月17日 星期四

fopen , fwrite

fopen
r or rb
Open file for reading.
w or wb
Truncate to zero length or create file for writing.
a or ab
Append; open or create file for writing at end-of-file.
r+ or rb+ or r+b
Open file for update (reading and writing).
w+ or wb+ or w+b
Truncate to zero length or create file for update.
a+ or ab+ or a+b
Append; open or create file for update, writing at end-of-file.

fwrite:
size_t fwrite ( const void * ptr, size_t size, size_t count, FILE * stream );

int fseek(FILE *stream, long offset, int whence)
sets the file position indicator for the stream pointed to by stream
If whence is set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the start of the file, the current position indicator, or end-of-file, respectively

long ftell(FILE *stream)
obtains the current value of the file position indicator for the stream pointed to by stream.

2008年7月16日 星期三

struct, array, pointer

ex:
struct people{
        int age;
        int height;
};

struct people a[10];
&a: bffff988  a:bffff988  &a[0]: bffff988  &a[1]:bffff990  (a+1):bffff990
because the size of struct people is 8,  (a+1)= &a[1]- &a[0]= bffff988+ 8= bffff990

struct people *b=a;
b+1 equals a+1
b[1] equals a[1]

2008年6月29日 星期日

fseek

reposition a file-position indicator in a stream

#include <stdio.h>


int fseek(FILE *stream, long offset, int whence);

whence:
describe the location in the file to which offset is relative
ex:
SEEK_SET:
relative to the beginning of the file

2008年5月5日 星期一

fscanf

nt fscanf ( FILE * stream, const char * format, ... );
the function returns the number of items succesfully read

2008年5月4日 星期日

fgets

char* fgets( char *str,  int num,  File  *stream )
read one line