1

## 【急】有沒有大神可以幫小弟解出這題【C】

ant1017 iT邦新手 3 級 ‧ 2019-04-25 14:11:51 檢舉

Astair iT邦新手 5 級 ‧ 2019-04-25 14:13:09 檢舉

akira6285 iT邦新手 5 級 ‧ 2019-04-25 14:27:54 檢舉

froce iT邦大師 1 級 ‧ 2019-04-25 14:31:08 檢舉

ant1017 iT邦新手 3 級 ‧ 2019-04-25 14:42:21 檢舉

"武吉郎"直接買新的...(誤

newkevin iT邦高手 1 級 ‧ 2019-04-25 18:04:03 檢舉

fillano iT邦超人 1 級 ‧ 2019-04-26 11:03:31 檢舉

### 3 個回答

1

iT邦大師 1 級 ‧ 2019-04-25 14:58:14

Compiler之後就知道了...

4

iT邦大師 1 級 ‧ 2019-04-25 15:37:25
``````#include <stdio.h>

int main() {
float ary[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
float *ptr = ary;
float *pEnd = ary + sizeof(ary)/sizeof(float) - 1;

printf("--- 基礎資料 ---\n");
while (ptr <= pEnd) {
printf("pointer = %p \t value = %f\n", ptr, *ptr);
ptr++;
}
printf("--- 基礎資料 ---\n");

float *ptr2 = ary;
printf("--- 乘以10 ---\n");
while (ptr2 <= pEnd) {
*ptr2 *= 10.0;
ptr2++;
}

float *ptr3 = ary;
printf("--- 新資料 ---\n");
while (ptr3 <= pEnd) {
printf("pointer = %p \t value = %f\n", ptr3, *ptr3);
ptr3++;
}
printf("--- 新資料 ---\n");

return 0;
}
``````

``````--- 基礎資料 ---
pointer = 0x7ffee939da90 	 value = 1.000000
pointer = 0x7ffee939da94 	 value = 2.000000
pointer = 0x7ffee939da98 	 value = 3.000000
pointer = 0x7ffee939da9c 	 value = 4.000000
pointer = 0x7ffee939daa0 	 value = 5.000000
pointer = 0x7ffee939daa4 	 value = 6.000000
pointer = 0x7ffee939daa8 	 value = 7.000000
pointer = 0x7ffee939daac 	 value = 8.000000
pointer = 0x7ffee939dab0 	 value = 9.000000
--- 基礎資料 ---
--- 乘以10 ---
--- 新資料 ---
pointer = 0x7ffee939da90 	 value = 10.000000
pointer = 0x7ffee939da94 	 value = 20.000000
pointer = 0x7ffee939da98 	 value = 30.000000
pointer = 0x7ffee939da9c 	 value = 40.000000
pointer = 0x7ffee939daa0 	 value = 50.000000
pointer = 0x7ffee939daa4 	 value = 60.000000
pointer = 0x7ffee939daa8 	 value = 70.000000
pointer = 0x7ffee939daac 	 value = 80.000000
pointer = 0x7ffee939dab0 	 value = 90.000000
--- 新資料 ---
``````

froce iT邦大師 1 級 ‧ 2019-04-25 20:12:22 檢舉

(雖然計概都不會但還是過了 XD

froce iT邦大師 1 級 ‧ 2019-04-25 20:44:18 檢舉

weiclin iT邦高手 4 級 ‧ 2019-04-25 23:38:34 檢舉

1

iT邦研究生 2 級 ‧ 2019-04-26 10:46:18

``````#include <stdio.h>

void forEach(void *ary, int size, int count, void (*func)(void *data));
void mulTen(void *data);
void printData(void *data);

int main()
{
float ary[] = {1, 2, 3, 4, 5, 6, 7, 8, 9 };
int size=sizeof(*ary);
int count=sizeof(ary)/sizeof(*ary);
printf("before:\n");
forEach(ary, size, count, printData);
forEach(ary, size, count, mulTen);
printf("\nafter:\n");
forEach(ary, size, count, printData);
return 0;
}

void forEach(void *ary, int size, int count, void (*func)(void *data))
{
int i;
for(i=0; i<count; ++i) {
func((char*)ary+i*size);
}
}

void mulTen(void *data)
{
*(float*)data=*(float*)data*10;
}

void printData(void *data)
{
printf("%.2f ",*(float*)data);
}
``````