pointer assignment

This is a discussion on pointer assignment within the C++ Programming forums, part of the General Programming Boards category; i want to know whether this direct assignment to pointer variable is valid or not. i compiled the program in ...

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    25

    pointer assignment

    i want to know whether this direct assignment to pointer variable is valid or not.
    i compiled the program in dev c++ and it works also but i am little bit confused as a pointer variable definition says that it is a variable which holds the address of another variable so is it safe to initialise pointer variables with some values given from cin directly.
    Code:
    #include<iostream>
    #include<conio.h>
    
    using namespace std;
    int main()
    {
     struct stu
     {
         int size;
         char name[10];
         
     }*ptr=NULL;
     ptr=new stu;
     cin>>ptr->size;
     cin>>ptr->name;
     
     cout<<ptr->size<<ptr->name;
     getch();
     return 0;
    }

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    cin>>ptr->size;
    cin>>ptr->name;

    These are not pointers, they are an int and a char* actually. And cin operator >> works by reference, so as long as the object exists, it is valid.

  3. #3
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    to whiteflags:-
    can you please explain me in detail. i am new to c and c++

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Quote Originally Posted by shikhardeep View Post
    to whiteflags:-
    can you please explain me in detail. i am new to c and c++
    Code:
    #include<iostream>
    #include<conio.h>
    
    using namespace std;
    int main()
    {
     struct stu
     {
         int size;
         char name[10];
         
     }*ptr=NULL;
     1. ptr=new stu;
     2. cin>>ptr->size;
     3. cin>>ptr->name;
     
     4. cout<<ptr->size<<ptr->name;
     getch();
     return 0;
    }
    1. This assigns a new stu object to ptr. If it does not succeed, new will throw an exception.
    2. This changes size if the user enters a valid integer. If the user fails to provide a good integer, then cin will enter a bad state, and size remains unchanged.
    3. This line works similarly to 2. The only difference is that the input should be a string that fits in name.
    4. You echo the int and name back, but it could be garbage.

    Steps two and three and four also depend on the success of step 1. The fact that you're using pointers doesn't matter. As I mentioned, cin operator >> works by reference, and as long as the pointer actually points to a stu object, the code will work.

  5. #5
    Registered User
    Join Date
    Aug 2011
    Posts
    25
    thank u. could u please reccommend me some website,books or weblinks so that i can clear my concepts especially on pointers,self referencial structures and data structures like linked list

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    23,021
    I'd advise you to use std::string instead of char. What you are doing right now is prone to buffer overruns.
    Also, you use new without a corresponding delete, hence you will get a memory leak. To avoid that, avoid new or use smart pointers.
    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. Pointer assignment using array name
    By stavos77 in forum C Programming
    Replies: 7
    Last Post: 03-12-2010, 03:12 PM
  2. Pointer Assignment
    By cardinals03 in forum C++ Programming
    Replies: 2
    Last Post: 10-22-2009, 01:16 PM
  3. assignment of pointer...
    By roaan in forum C Programming
    Replies: 14
    Last Post: 07-08-2009, 12:07 PM
  4. Pointer assignment
    By MAx12345 in forum C Programming
    Replies: 16
    Last Post: 07-10-2008, 11:42 PM
  5. Assignment [pointer]
    By aotomato in forum C++ Programming
    Replies: 4
    Last Post: 11-22-2004, 04:31 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21