I'm trying to learn how pointers work in C++, except for the last hour I've only managed to make it crash.
#include<string>
// a sample structure
typedef struct test_obj_type_s{
// with some numbers
int x;
float y;
double z;
std::string str; // a standard string...
char * str2; // a variable-length string...
char str3[2048]; // a fixed-length string...
struct test_obj_type_s *next; // and a link to another similar structure
} test_obj_type_t;
// a sample pointer
test_obj_type_t * last_test_obj;
test_obj_type_t obj;
// let's go
int main(){
// let's assign some demo values
obj.x = 12;
obj.y = 15.15;
obj.z = 25.1;
obj.str = "test str is working";
obj.str2 = "test str2 is working";
strcpy_s(obj.str3, "test str3 is working");
// let's also assign some circular references
obj.next = &obj;
// now...
last_test_obj = &obj;
test_obj_type_t * t1 = last_test_obj;
test_obj_type_t t2 = obj;
// now let's have some fun;
printf("%d %d %d %s %s %s", t2.x, t2.y, t2.z, t2.str, t2.str2, t2.str3);
printf("%d %d %d %s %s %s", t2.next->x, t2.next->y, t2.next->z, t2.next->str, t2.next->str2, t2.next->str3);
printf("%d %d %d %s %s %s", t1->x, t1->y, t1->z, t1->str, t1->str2, t1->str3);
printf("%d %d %d %s %s %s", t1->next->x, t1->next->y, t1->next->z, t1->next->str, t1->next->str2, t1->next->str3);
printf("I survived!");
}
What am I missing? Or what am I doing wrong?
printfin C++std::cout