I thought #1
uint8_t* start_ptr, end_ptr;
and #2
uint8_t* start_ptr;
uint8_t* end_ptr;
are generally the same. It seems to me they are not.
Could somebody specify what the first one does other than the second?
What happened:
if(strncmp(mseq,mseq_a,8) == 0){
start_ptr = MY_UART_RingBuffer_getReadPointer();
start_found = 1;
}
if(strncmp(mseq+3,mseq_z,5) == 0){
end_ptr = MY_UART_RingBuffer_getReadPointer();
if (start_found == 1){
if(!MY_UART_RingBuffer_getOverlap()){
end_ptr = end_ptr - 6;
}
else{
start_found = 0;
continue;
}
ptrdiff_t length = end_ptr - start_ptr;
start_found = 0;
}
}
On using #1 the compiler will give me this for the length calculation:
../Core/Src/main.c:143:32: error: invalid operands to binary - (have 'int' and 'uint8_t * {aka unsigned char *}')
ptrdiff_t length = end_ptr - start_ptr;
On using #2 everything works out fine.
I am somehow confused here. I see the solution but I don’t really get the issue.
Thanks a lot 😉
>Solution :
Grammatically, the type uint8_t is a declaration-specifier; whereas the pointer * is a declarator. According to C’s grammar declarators bind to the declared name rather than the declaration-specifier. Consequently your first line is parsed like:
uint8_t (*start_ptr), end_ptr;
i.e. only the first name becomes a pointer.
This is one reason some tend to put the space before the pointer * rather than after. The correct way to declare those in one line would be:
uint8_t *start_ptr, *end_ptr; // both are pointers to uint8_t.