C 为什么可以';我不能得到结构指针的正确值吗?
我想在函数中使用ptr[1]->ReadLength,但它总是显示0 解决这个问题的方法是什么 多谢各位C 为什么可以';我不能得到结构指针的正确值吗?,c,arrays,pointers,implicit-conversion,dereference,C,Arrays,Pointers,Implicit Conversion,Dereference,我想在函数中使用ptr[1]->ReadLength,但它总是显示0 解决这个问题的方法是什么 多谢各位 struct cache_read_block { unsigned short ReadLength; // How many words }; typedef struct cache_read_block CACHE_READ_BLOCK; void getValue(CACHE_READ_BLOCK (*ptr)[100]) { printf("index %
struct cache_read_block
{
unsigned short ReadLength; // How many words
};
typedef struct cache_read_block CACHE_READ_BLOCK;
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", ptr[0]->ReadLength);
printf("index %d\n", ptr[1]->ReadLength);
}
int main(void) {
CACHE_READ_BLOCK arr[100] = {0};
arr[0].ReadLength = 10;
arr[1].ReadLength = 5;
getValue(&arr);
system("pause");
return 0;
}
在这个函数中
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", ptr[0]->ReadLength);
printf("index %d\n", ptr[1]->ReadLength);
}
参数是一个指针,指向类型为CACHE\u READ\u BLOCK
的100个元素的数组。你必须先解除指针的指向
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", ( *ptr )[0].ReadLength);
printf("index %d\n", ( *ptr )[1].ReadLength);
}
用以下方法声明和定义函数会更简单
void getValue( CACHE_READ_BLOCK *ptr )
{
printf("index %d\n", ptr[0].ReadLength);
printf("index %d\n", ptr[1].ReadLength);
}
就这样说吧
getValue( arr );
用作函数参数的数组隐式转换为指向其第一个元素的指针
或者,由于数组的元素没有更改,因此参数应该具有限定符const
void getValue( const vCACHE_READ_BLOCK *ptr )
{
printf("index %d\n", ptr[0].ReadLength);
printf("index %d\n", ptr[1].ReadLength);
}
在这个函数中
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", ptr[0]->ReadLength);
printf("index %d\n", ptr[1]->ReadLength);
}
参数是一个指针,指向类型为CACHE\u READ\u BLOCK
的100个元素的数组。你必须先解除指针的指向
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", ( *ptr )[0].ReadLength);
printf("index %d\n", ( *ptr )[1].ReadLength);
}
用以下方法声明和定义函数会更简单
void getValue( CACHE_READ_BLOCK *ptr )
{
printf("index %d\n", ptr[0].ReadLength);
printf("index %d\n", ptr[1].ReadLength);
}
就这样说吧
getValue( arr );
用作函数参数的数组隐式转换为指向其第一个元素的指针
或者,由于数组的元素没有更改,因此参数应该具有限定符const
void getValue( const vCACHE_READ_BLOCK *ptr )
{
printf("index %d\n", ptr[0].ReadLength);
printf("index %d\n", ptr[1].ReadLength);
}
试试这个:
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", (*ptr)[0].ReadLength);
printf("index %d\n", (*ptr)[1].ReadLength);
}
试试这个:
void getValue(CACHE_READ_BLOCK (*ptr)[100])
{
printf("index %d\n", (*ptr)[0].ReadLength);
printf("index %d\n", (*ptr)[1].ReadLength);
}
你的答案应该解释你改变了什么以及为什么——只使用代码的答案不被鼓励——你的答案应该解释你改变了什么以及为什么——只使用代码的答案不被鼓励