2012-03-07 66 views
1

我想寫整數1到第一個字節和0x35到文件描述符的第二個字節使用寫(http://linux.about.com/library/cmd/blcmdl2_write.htm),但我得到以下警告當我嘗試以下操作:使用寫入將整數寫入文件描述符?

write(fd, 1, 1); 
write(fd, 0x35, 1); 


source.c:29: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast 
source.c:30: warning: passing argument 2 of ‘write’ makes pointer from integer without a cast 

回答

8

您需要傳遞一個地址,因此您需要某種形式或其他形式的變量。

如果你需要的是一個單個字符:

char c = 1; 
write(fd, &c, 1); 
c = 0x35; 
write(fd, &c, 1); 

或者使用一個陣列(這通常是更常見的):

char data[2] = { 0x01, 0x35 }; 
write(fd, data, 2); 
2

第二個參數應該是一個指向緩衝區的指針,你可以這樣做:

char a = 1; 
write(fd, &a, 1); 

或更簡單:

char buff[] = {1, 0x35}; 
write(fd, buff, sizeof(buff));