Thread: struct of pointers being read by cin

  1. #1
    Registered User
    Join Date
    Nov 2015
    Posts
    4

    struct of pointers being read by cin

    I have this code that compiles just fine, however it creates a segmentation fault (core dumped) error. The error occurs as soon as the first name is entered. I have a struct with pointers of type string. Then I try to read in the dereferenced value. Any pointers(oh god) on this ?

    I am using the g++ compiler with ubuntu 14.04

    Code:
    #include <iostream>
    #include <cstring>
    using namespace std;
    
    struct Name {
      string* first;
      string* last;
    };
    
    
    int main()
    {
      Name name;
      cout << "Enter your first name: ";
      cin >> *name.first;
      cout << "Enter your last name: ";
      cin >> *name.last;
    }

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Where have you allocated memory for those pointers?

    Where is your #include for the use of those strings?

    Better yet why are you using pointers in your class in the first place? This is C++ you should be trying to avoid pointers whenever possible.

    Jim

  3. #3
    Registered User
    Join Date
    Nov 2015
    Posts
    4
    Quote Originally Posted by jimblumberg View Post
    Where have you allocated memory for those pointers?
    It is my understanding that if you declare a pointer it will automatically take up memory ?

    Where is your #include for the use of those strings?
    string is part of the std namespace

    Better yet why are you using pointers in your class in the first place? This is C++ you should be trying to avoid pointers whenever possible.
    I am learning c++ through Jumping into C++. One of the assignments is to create a function that takes in 2 parameters(first and last name), prompts the user for first and last, and returns this updated data. I was getting compile errors and took the function out of the equation and decided to start with small steps. The only way I know how to return 2 different parameters is through either a struct/array/class(we have not covered these yet).
    Thanks for your reply!

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    It is my understanding that if you declare a pointer it will automatically take up memory ?
    Then your understanding is incorrect. A pointer must be allocated or assigned memory.

    string is part of the std namespace
    Yes, but you still need to include the proper header file to use this standard C++ class.

    I am learning c++ through Jumping into C++. One of the assignments is to create a function that takes in 2 parameters(first and last name), prompts the user for first and last, and returns this updated data.
    But this doesn't answer the question about why you're using pointers.

    I was getting compile errors and took the function out of the equation and decided to start with small steps.
    Taking small steps is good. But why did you take the function out of the equation, using a function is one of the objective of this assignment.

    he only way I know how to return 2 different parameters is through either a struct/array/class(we have not covered these yet).
    Have you heard about passing values to functions using reference parameters?

    Jim

  5. #5
    Registered User
    Join Date
    Nov 2015
    Posts
    4
    It is my understanding that if you declare a pointer it will automatically take up memory ?
    Then your understanding is incorrect. A pointer must be allocated or assigned memory.

    Pointers in C and C++ - Tutorial - Cprogramming.com, explains "For example, you could declare a pointer that stores the address of an integer with the following syntax: int *points_to_integer;"
    In my case, first is the pointer variable that stores the address of a string(so first now has an address which means memory). I think you are describing use of the "new" keyword when declaring a pointer, which I am not doing for this assignment since I do not need dynamically allocated memory.

    string is part of the std namespace
    Yes, but you still need to include the proper header file to use this standard C++ class.
    Surprised it compiled then(each compiler is its own beast I suppose)

    I am learning c++ through Jumping into C++. One of the assignments is to create a function that takes in 2 parameters(first and last name), prompts the user for first and last, and returns this updated data
    But this doesn't answer the question about why you're using pointers.
    The assignment wants you to do the problem with both pointers and references.

    Thx for reply

  6. #6
    Registered User
    Join Date
    Nov 2015
    Posts
    4
    I got it working and yes I am wearing my dunce hat while I am typing this.
    The problem was, as you suggested not assigning memory. I used cout statements to see the memory of my pointer declaration and confused this with assignment.
    I also brought in the function. Without further ado, my code(pointers only, no references)

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    
    void firstAndLast(string* firstname, string* lastname);
    
    
    int main()
    {
      string first;
      string last;
      string* p_first = &first;
      string* p_last = &last;
      for (int i = 0; i < 3; i++)
      {
        firstAndLast(p_first, p_last);
        cout << "first name is: " << *p_first << '\n';
        cout << "last name is: " << *p_last << '\n';
      }
    }
    
    
    void firstAndLast(string* firstname, string* lastname)
    {
      cout << "Enter your first name: ";
      cin >> *firstname;
      cout << "Enter your last name: ";
      cin >> *lastname;
    }

  7. #7
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    You could simplify this by getting rid of the pointer variables in main.

    Code:
     
    ...
    void firstAndLast(string* firstname, string* lastname);
    
    int main()
    {
      string first;
      string last;
    
      for (int i = 0; i < 3; i++)
      {
        firstAndLast(&first, &last);
        cout << "first name is: " << first << '\n';
        cout << "last name is: " << last << '\n';
      }
    }
    
    ...
    And really you should be using reference parameters not pointers.

    Jim

  8. #8
    Guest
    Guest
    That being said, it's still good to get familiar with all the pointer shenanigans. So long the book introduces references later on, I don't see that as a problem.

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Pointers should be introduced a good while after references.
    Pointers are complicated and makes for a lot of explaining.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. K&R 6-4 : won't read Struct *
    By allan01123 in forum C Programming
    Replies: 2
    Last Post: 04-06-2011, 11:18 AM
  2. size of struct with pointers and function pointers
    By sdsjohnny in forum C Programming
    Replies: 3
    Last Post: 07-02-2010, 05:19 AM
  3. multiway read\write struct
    By quantt in forum C Programming
    Replies: 2
    Last Post: 02-02-2009, 10:13 AM
  4. How to read files into a struct
    By verbity in forum C Programming
    Replies: 16
    Last Post: 11-24-2006, 03:39 PM
  5. Assigning pointers from pointers in struct.
    By Brian in forum C Programming
    Replies: 2
    Last Post: 10-18-2003, 04:30 AM