[問題] 請問如何print此陣列

看板C_and_CPP (C/C++)作者 (..)時間12年前 (2014/04/18 06:07), 編輯推噓5(505)
留言10則, 3人參與, 最新討論串1/1
想請問的完整程式碼如下: #include <stdio.h> #include <stdlib.h> #include <string.h> #define ARRAYSIZE 10 int main() { int i; unsigned long buff; unsigned char *ReadBuf = NULL; ReadBuf = (unsigned char *)malloc(sizeof(unsigned char)*ARRAYSIZE); memset(ReadBuf, '1', ARRAYSIZE); for(i=0; i<ARRAYSIZE; i++) { printf("%c ", ReadBuf[i]); } printf("\n"); unsigned char *temp= (unsigned char *)malloc(sizeof(unsigned char)*ARRAYSIZE); memcpy((void *)temp, (void *)buff, ARRAYSIZE); buff = (unsigned long)temp; memcpy((void *)buff, (void *)ReadBuf, ARRAYSIZE); //how to print the buff?? for(i=0; i<ARRAYSIZE; i++) { printf("%lu ", *(&buff+i)); } printf("\n"); return 0; } 問題: 因為buff是unsigned long,所以我用%lu去印, 但因出的都不是正確的內容: 1 1 1 1 1 1 1 1 1 1 想請問哪裡有問題,謝謝! -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 61.230.174.105 ※ 文章網址: http://www.ptt.cc/bbs/C_and_CPP/M.1397772431.A.668.html

04/18 08:25, , 1F
呃, buff 根本就不是陣列啊...
04/18 08:25, 1F

04/18 08:25, , 2F
還有你中間的 temp 是來做什麼的 @@
04/18 08:25, 2F

04/18 08:26, , 3F
或者該問你你真的知道你在寫些什麼嗎?
04/18 08:26, 3F

04/18 13:59, , 4F
請問是要做動態陣列嗎
04/18 13:59, 4F

04/18 14:08, , 5F
這邊好像說pointer的大小不一樣 http://goo.gl/wC4V8i
04/18 14:08, 5F

04/18 14:13, , 6F
把temp 存到buff有點怪
04/18 14:13, 6F

04/18 14:14, , 7F
對不起我推文推得好亂@@;; SORRY
04/18 14:14, 7F

04/19 07:34, , 8F
buff不是陣列 可是為何可以用memcpy 複製到temp?
04/19 07:34, 8F

04/19 07:35, , 9F
還有 這不是我的 code 我也是看不懂才想問一下 @@
04/19 07:35, 9F

04/21 11:33, , 10F
都用(void *)轉型 code已經只認兩個位址 跟大小
04/21 11:33, 10F
文章代碼(AID): #1JK52FPe (C_and_CPP)
文章代碼(AID): #1JK52FPe (C_and_CPP)