I wanted to access the array elements using the struct attributes. I am able to print 1st and the 2nd element using the struct pointer (nameptr) while the remaining 3 elements has to be accessed using the (uint8_t ptr) which itself is a attribute of the 'struct name'.
#include <iostream>
struct name{
uint8_t b0;
uint8_t b1;
uint8_t* ptr;
}*nameptr;
int main()
{
uint8_t arr[]= {1,2,3,4,5};
nameptr = (name *)arr;
nameptr->ptr = arr+2;
printf("%d ", nameptr->b0); //prints 1
printf("%d ", nameptr->b1); //prints 2
for (int i=2; i<5; i++){
printf("%d ",*(nameptr->ptr+i)); //expecting to print 3 4 5
}
return 0;
}
When compiled I get the below error, Please help me getting this error solved.
*** stack smashing detected ***: ./a.out terminated
1 2 5 0 56 Aborted (core dumped)