0

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?

5
  • 1
    Why are you using printf in C++ Commented Aug 17, 2014 at 13:56
  • @EdHeal what should I be using Commented Aug 17, 2014 at 13:58
  • 1
    << from iostream, and you'll get rid of your crash, since I believe you have an answer. Commented Aug 17, 2014 at 14:00
  • 1
    iostream- std::cout Commented Aug 17, 2014 at 14:07
  • 1
    "I'm trying to learn how pointers work in C++, except for the last hour I've only managed to make it crash." - and it will stay like this for the rest of your career. Because even experienced senior software developers create pointer bugs. Commented Aug 17, 2014 at 14:45

2 Answers 2

4

Your program exhibits undefined behavior when it passes an std::string to printf. %s format specifier expects a const char* as a parameter. Everywhere you have something.str or something->str in your series of printf statements, replace it with something.str.c_str()

Similarly, %d expects int as a parameter, but you are passing double. To print something.y and something.z, use %f specifier instead.

Sign up to request clarification or add additional context in comments.

Comments

1

You can move to the c++ stream output, it's more simple way to do such things:

// now let's have some fun;
std::cout<<t2.x<<t2.y<<t2.z<<t2.str<<t2.str2<<t2.str3<<std::endl;
std::cout<<t2.next->x<<t2.next->y<<t2.next->z<<t2.next->str<<t2.next->str2<<t2.next->str3<<std::endl;
std::cout<<t1->x<<t1->y<<t1->z<<t1->str<<t1->str2<<t1->str3<<std::endl;
std::cout<<t1->next->x<<t1->next->y<<t1->next->z<<t1->next->str<<t1->next->str2<<t1->next->str3<<std::endl;

1 Comment

Just what happens if "%d %d %d %s %s %s" in the OP's answer actually comes from a user-defined language-dependent setting? std::cout and friends cannot always make up for the flexibility of format strings. Boost.Format tries to combine the flexibility of format strings with the type safety of C++.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.