我在main函数中生成了一个随机数组,如何使用单独的函数正确打印出来,并在函数内部使用指针打印出该数组的每个元素的值和内存位置。到目前为止,这是我的代码:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printArray(int *pointertoArray, int *Size);
int main (void)
{
srand(time(NULL));
int array[10];
int *pointer = NULL;
for(int i = 0; i < size; i++)
{
array[i] = rand();
*pointer = array[i];
printArray(*pointer,size);
}
}
void printArray(int *pointerToArray, int *size)
{
int i = 0;
do
{
printf("\nValue %d = %p ",i,*pointerToArray);
i++;
}
while(i < size);
}
这是我想要实现的目标:
value 1 = 0x7fff0815c0e0
.....
value 10 = 0x7fff0815c0ec
答案 0 :(得分:3)
Traceback (most recent call last): File "<pyshell#0>", line 1,
in <module> import pygame File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/__init__.py", line 95,
in <module> from pygame.base import * ImportError: dlopen(/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so, 2): no suitable image found.
Did find: /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pygame/base.so: no matching architecture in universal wrapper.
应为int *size
。你没有通过指针,你也不需要指针。int size
会更合适。size_t size
的调用应位于循环之后。您只想打印一次数组。printArray
应为printArray(*pointer, size);
。printArray(array, size);
应命名为pointerToArray
或array
。pointerToInts
,而不是pointerToArray[i]
。i
,而非pointerToArray+i
。*pointerToArray
中的循环应该经过顶级测试。 (没有理由对它进行底部测试,所以要安全起来。)printArray
被声明为返回main
,但没有。我们得到了,
int
替代:
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
void printArray(int *array, size_t size);
int main() {
srand(time(NULL));
int array[10];
for (int i = 0; i < size; ++i) {
array[i] = rand() % 1000;
}
printArray(array, sizeof(array)/sizeof(array[0]));
return 0;
}
void printArray(int *array, size_t size) {
for (int i = 0; i < size; ++i) {
printf("Value @ %p = %d\n", array+i, array[i]);
}
}