Reputation: 55
This is a header file in my current project where I define two structs:
typedef struct{
unsigned char length;
unsigned char buffer[8];
unsigned char index;
} buffer;
typedef struct {
volatile unsigned char * SFR;
unsigned char risingEdges;
unsigned char fallingEdges;
unsigned char buffer[2];
buffer * b;
}port;
Now when i call this method, the current value of the special function register (by using the SFR pointer in the struct) be inserted in the buffer * b.
void portUpdate(port p){
bufferInsertItem(*p.SFR,*p.b);
**** More C code omitted
}
This is the implementation of bufferInsertItem
void bufferInsertItem(char i,buffer b){
b.buffer[b.index]=i;
b.index = b.index++%b.length;
}
I'm not really sure and I can't test this embedded project at this moment, but should i really dereference the buffer when i call. I'm a bit confused, also because the name of an array is already a pointer to the first element. So what's really the value of *p.b
?
bufferInsertItem(*p.SFR,*p.b);
I'm quite sure that *p.SFR is the actual value of the special function register, but is *p.b the value of the first element in the buffer or the buffer?
Upvotes: 0
Views: 52
Reputation: 222457
With the code shown in the question, the second parameter to bufferInsertItem
is buffer b
, meaning it is a buffer
.
In the call bufferInsertItem(*p.SFR, *p.b)
, p
is a port
, and its b
member is buffer *
, that is, a pointer to a buffer
. Thus *p.b
is a buffer
, and that is what should be passed to bufferInsertItem
given its current definition.
However, that is not likely the desired solution. With its current definition, bufferInsertItem
receives the value of a buffer
. That is, it receives a copy. Its name suggests it is intended to change the buffer, but it will only be able to change the copy, which will not create any change in the original. To fix this, the parameter type should be changed to buffer *b
, so that a pointer is passed. Corresponding changes inside bufferInsertItem
will be needed to use b
as a pointer rather than as a structure, and the call should then pass p.b
, not *p.b
.
In comments, it was stated that the declaration void portUpdate(port p)
should be changed to void portUpdate(port *p)
, but this is not necessary to make bufferInsertItem
work. When portUpdate
is passed a copy of p
, that copy includes the member buffer *b
, which is a pointer to the original buffer
, and so that original buffer would be modified by the call bufferInsertItem(*p.SFR, p.b)
. However, structures are often passed by address even when pass by value would work, as a matter of efficiency (less space is used to pass a single address than to pass an entire structure). So void portUpdate(port p)
could be modified to void portUpdate(port *p)
, with corresponding changes in other code.
There is no array involved in the use of buffer
, so it is unclear why the question asks about arrays. It is possible memory for an array of buffer
could have been allocated, and p.b
was set to point to the first buffer
of that array. Nonetheless, the parameter buffer b
must be given an argument that is a buffer
, not a pointer to a buffer.
Upvotes: 1