[英]C - passing elements from within structures to functions
I have a structure comprised of an array of pointers to other structures of a different type. 我有一个结构,该结构由指向不同类型其他结构的指针数组组成。
typedef struct{
NodeT* nodes[2];
int size;
}stackT;
with: 与:
typedef struct{
char info;
}NodeT;
And I have a pointer to the above (first) structure: 我有一个指向上面(第一个)结构的指针:
stackT* stackPtr;
Assuming the memory is allocated for the stackT structure as well as both NodeT structures with associated assignments for the members of both NodeT structures, how would I pass to a function one of the pointers in stackT? 假设已为stackT结构以及两个NodeT结构分配了内存,并且为两个NodeT结构的成员分配了关联的分配,我如何将函数传递给stackT中的指针之一?
For example: 例如:
void setChar(NodeT* nodePtr, char setTo){
nodePtr->info = setTo;
}
called with line: 用行调用:
setChar(stackPtr->nodes[0], 'A');
Does not work. 不起作用。 I figured it had something to do with the -> syntax dereferencing the pointer whereby I am actually passing in a structure.
我认为这与->语法解引用指针有关,实际上我在结构中传递了指针。 I do not get any compilation errors, but when I check for the assignment by printing whatever is stored in
char info
I do not get anything. 我没有得到任何编译错误,但是当我通过打印存储在
char info
检查赋值时,我什么也没有。
Is the notation incorrect or do I have issues elsewhere in the program perhaps? 该表示法是否正确,或者程序中其他地方可能存在问题? I just wanted to rule this out first (proper passing syntax).
我只是想先排除掉它(正确的传递语法)。
If someone is looking for a solution to this problem, the above code was actually correct. 如果有人在寻找解决此问题的方法,那么上面的代码实际上是正确的。 Assuming you have an array of pointer in some structure and you wish to pass such a pointer, the correct syntax would be:
假设您在某种结构中具有一个指针数组,并且希望传递此类指针,则正确的语法应为:
someFunc( structPtr->ptrArray[0] )
The line: 该行:
structPtr->ptrArray[0]
Actually returns a pointer, and not whatever the pointer is pointer to. 实际上返回一个指针,而不返回任何指针。
(*structPtr).ptrArray[0]
Is also equivalent. 也等效。
That being said, I either mistakenly interpreted the information before me, or I had underlying errors elsewhere in the my code. 话虽如此,我要么错误地解释了我面前的信息,要么我的代码中的其他地方有潜在的错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.