简体   繁体   English

C - function 内的指针指向显示无效字符的数组

[英]C - Pointer inside a function pointing to an array displaying invalid character

I have an array product[6] and a pointer (*productPtr) pointing to this array.我有一个数组 product[6] 和一个指向该数组的指针 (*productPtr)。 When I pass the pointer as an argument to a function called productCheck and try to print one of the characters, I get an invalid reading of the characters from the product array.当我将指针作为参数传递给名为 productCheck 的 function 并尝试打印其中一个字符时,我从产品数组中读取的字符无效。 Any help as to why this is happening is greatly appreciated.非常感谢任何有关为什么会发生这种情况的帮助。 For example if i = 3, instead of reading 'u', the output is '{'.例如,如果 i = 3,而不是读取 'u',output 是 '{'。

int i = 3;    
char product[6] = "xddua";
char * productPtr;
productPtr = product;


bool productCheck(int i, char * productPtr);


productOk = productCheck(i, &product);


bool productCheck(int i, char * productPtr)
{    
    printf("product is %c\n", *productPtr + i);

The function has the second parameter of the type char * . function 具有char *类型的第二个参数。

bool productCheck(int i, char * productPtr);

You are calling the function passing expression the expression &product as the second argument您正在调用 function 传递表达式表达式&product作为第二个参数

productOk = productCheck(i, &product);

As the array product is declared like由于数组产品被声明为

char product[6] = "xddua";

then the type of the expression ^product is char ( * )[6] .那么表达式 ^product 的类型是char ( * )[6] That is the type of the argument is not compatible with the type of the function parameter.即参数的类型与 function 参数的类型不兼容。

You need to call the function either like您需要像这样调用 function

productOk = productCheck(i, product);

or like或喜欢

productOk = productCheck(i, productPtr);

Within the function dereferencing the passed value of the expression &product in this statement在 function 内取消引用此语句中表达式&product的传递值

printf("product is %c\n", *productPtr + i);

And then to the result expression you are adding the value i instead of at first to add the value i to the pointer and then to dereference the pointer expression..然后在结果表达式中添加值i而不是首先将值i添加到指针然后取消引用指针表达式。

Thus you need to call the function like因此,您需要像这样调用 function

productOk = productCheck(i, product);

or like或喜欢

productOk = productCheck(i, productPtr);

and within the function you need to write either在 function 中,您需要编写

printf("product is %c\n", *( productPtr + i ) );

or或者

printf("product is %c\n", productPtr[i] );

The unary * operator has higher precedence than + operator.一元*运算符的优先级高于+运算符。

*productPtr + i first reads what is pointed at by productPtr , then adds i to the value read. *productPtr + i首先读取productPtr指向的内容,然后将i添加到读取的值。

To access another element, You should write *(producePtr + i) or productPtr[i] .要访问另一个元素,您应该编写*(producePtr + i)productPtr[i]

Also the argument &product is not good.论点&product也不好。 Most arrays in expressions are automatically converted to pointers to the first element (one of the exceptions is the operand of unary & ), so it should be product to pass char* value.表达式中的大多数 arrays 会自动转换为指向第一个元素的指针(一个例外是一元&的操作数),因此传递char*值应该是product

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM