Pointer clarification.

Discussion in 'C++' started by Makwana, Aug 26, 2008.

  1. Makwana

    Makwana Guest

    I am a grad student trying to get my hands on c++. I am trying to code
    simple programs using pointers and it would be great if some gurus
    here point me to the correct method of referencing.

    My code complies but crash's .. can someone point me the right way to
    do it. Thanks in advance!

    *******************************************************************************************************************************
    // pointers to structures
    #include <iostream>
    #include <string>
    #include <sstream>
    using namespace std;

    struct movies_t {
    string title;
    int year;
    };

    int main ()
    {
    string mystr;
    int mov_num,i;
    cout << " Enter the number of movies you want to enter " << endl;
    cin >> mov_num;

    movies_t amovie[mov_num];
    movies_t * pmovie[mov_num];
    // strcpy(&amovie, pmovie);
    // pmovie = &amovie;

    for (i=0;i<mov_num;i++);
    {
    cout << "Enter title: " << endl;
    getline (cin, pmovie->title);
    cout << "Enter year: " << endl;
    getline (cin, mystr);
    (stringstream) mystr >> pmovie->year;
    }
    for (i=0;i<mov_num;i++);
    cout << "\nYou have entered:\n";
    cout << pmovie->title;
    cout << " (" << pmovie->year << ")\n";
    system ("pause");
    return 0;
    }
    Makwana, Aug 26, 2008
    #1
    1. Advertising

  2. On 2008-08-26 21:42, Makwana wrote:
    > I am a grad student trying to get my hands on c++. I am trying to code
    > simple programs using pointers and it would be great if some gurus
    > here point me to the correct method of referencing.
    >
    > My code complies but crash's .. can someone point me the right way to
    > do it. Thanks in advance!
    >
    > *******************************************************************************************************************************
    > // pointers to structures
    > #include <iostream>
    > #include <string>
    > #include <sstream>
    > using namespace std;
    >
    > struct movies_t {
    > string title;
    > int year;
    > };
    >
    > int main ()
    > {
    > string mystr;
    > int mov_num,i;


    This is C++, you declare the variables right before you use them for the
    first time, in particular 'i' should be declared in the for-loop.

    > cout << " Enter the number of movies you want to enter " << endl;
    > cin >> mov_num;
    >
    > movies_t amovie[mov_num];


    This is not allowed in C++, the size of the array have to be a constant.
    If your compiler allows you to do that you need to tell it to disallow
    extensions to the language. Instead of an array you should be using
    std::vector in this situation, considering that you are using string-
    streams I think you should know about them by now.

    > movies_t * pmovie[mov_num];


    There is really no need for pointers in this program, they will only
    make things more complicated than necessary, you can use the array (or
    vector) directly.

    > // strcpy(&amovie, pmovie);
    > // pmovie = &amovie;
    >
    > for (i=0;i<mov_num;i++);


    for (int i = 0; i < mov_num; ++i)

    Notice the lack of the semicolon at the end, it is very important. The
    way you have written it only the semicolon is inside the loop, the code
    below is not. The effect is that when the code below is executed 'i'
    will be equal to mov_num, which is one past the end of the array, which
    is probably why your program crashes.

    > {
    > cout << "Enter title: " << endl;
    > getline (cin, pmovie->title);
    > cout << "Enter year: " << endl;
    > getline (cin, mystr);
    > (stringstream) mystr >> pmovie->year;
    > }
    > for (i=0;i<mov_num;i++);


    Remove the semicolon, and add the brackets, if your text-editor supports
    automatically indenting the code you should use this feature since it
    will tell you which code belongs to which code-block.

    > cout << "\nYou have entered:\n";
    > cout << pmovie->title;
    > cout << " (" << pmovie->year << ")\n";
    > system ("pause");
    > return 0;
    > }


    --
    Erik Wikström
    Erik Wikström, Aug 26, 2008
    #2
    1. Advertising

  3. Makwana

    peter koch Guest

    On 26 Aug., 21:42, Makwana <> wrote:
    > I am a grad student trying to get my hands on c++. I am trying to code
    > simple programs using pointers


    There's your first problem. It is not so easy to write (nontrivial)
    programs with pointers. Even experienced programmers might put in a
    bug or two in this case.

    > and it would be great if some gurus
    > here point me to the correct method of referencing.
    >
    > My code complies but crash's .. can someone point me the right way to
    > do it. Thanks in advance!
    >
    > ***************************************************************************­****************************************************
    > // pointers to structures
    > #include <iostream>
    > #include <string>
    > #include <sstream>
    > using namespace std;
    >
    > struct movies_t {
    >   string title;
    >   int year;
    >
    > };
    >
    > int main ()
    > {
    >   string mystr;
    >   int mov_num,i;


    As Erik mentioned, declare variables when you use them - not before.
    This habit might improve the performance of your program, but more
    important it reduces the number of bugs.

    >   cout << " Enter the number of movies you want to enter " << endl;
    >   cin >> mov_num;
    >
    >   movies_t amovie[mov_num];

    This is not valid C++. mov_num is not a compile-time constant - your
    program should not compile.

    Why amovie? This is a bad name for an array.
    >   movies_t * pmovie[mov_num];


    This is a pointer to an array of movies - probably not what you want.

    > //  strcpy(&amovie, pmovie);
    > //  pmovie = &amovie;


    Why these commented out statements - they make no sense.

    >
    >   for (i=0;i<mov_num;i++);
    >   {
    >   cout << "Enter title: " << endl;
    >   getline (cin, pmovie->title);
    >   cout << "Enter year: " << endl;
    >   getline (cin, mystr);
    >   (stringstream) mystr >> pmovie->year;


    This is a C-cast. Never do that. What are you trying to do here? If
    you want to read an integer simply do e.g. std::cin << pmovie-
    >year;


    >   }
    >   for (i=0;i<mov_num;i++);

    This line is a complete for loop, that doesn't do anything.

    >   cout << "\nYou have entered:\n";
    >   cout << pmovie->title;
    >   cout << " (" << pmovie->year << ")\n";
    >   system ("pause");
    >   return 0;
    >
    >
    >
    > }


    Also your intendation is bad. Use copy-paste from your source-file.

    /Peter
    peter koch, Aug 26, 2008
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. ma740988
    Replies:
    5
    Views:
    376
    ma740988
    Aug 23, 2004
  2. Replies:
    3
    Views:
    366
    Victor Bazarov
    Feb 2, 2005
  3. Peter

    pointer/linked List clarification

    Peter, Dec 23, 2008, in forum: C Programming
    Replies:
    5
    Views:
    649
    Peter
    Dec 26, 2008
  4. Tricky

    Pointer clarification needed

    Tricky, Sep 9, 2009, in forum: VHDL
    Replies:
    3
    Views:
    521
    Tricky
    Sep 9, 2009
  5. Ruby Stevenson

    clarification on pointer use

    Ruby Stevenson, Feb 22, 2012, in forum: C++
    Replies:
    4
    Views:
    242
    Jorgen Grahn
    Feb 22, 2012
Loading...

Share This Page