Thread: char pointer working in scanf but not in cin.

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    61

    char pointer working in scanf but not in cin.

    Hi
    This seems very easy and beginner question.But really i can't figure out it.
    Code:
       char *ch
        cout << "Enter name";
       scanf("%s",&ch);
        cin >> ch;
    Why can not cin allocate ch pointer but scanf can allocate it.What is happening?It gives run-time error.
    I am looking for your answers.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    Scanf doesn't allocate pointers either. You maybe getting lucky. Always allocate/init pointers!
    Woop?

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    As far as I can tell, both dont work (syntax errors aside). You havent allocated any memory for the null terminated string ch.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    hmm yes you are right.Also i see that scanf didn't work if i write:
    cout >> ch;

    What are the differneces between string and char pointer.I mean this code is working :
    Code:
        
    string s;
        cout << "Enter name";
       cin >> s;
       cout  << s;
    but if declare s char pointer, it doesn't.We know that s is a pointer isn't it?Also ch is a pointer.But still i don't understand the differences..AlsoIs there a way that i can assign variable size string to char pointer.How can string variable achieve this?
    Please explain.
    Thanks for answers.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    We know that s is a pointer isn't it?
    No, s is a std::string object.

    Is there a way that i can assign variable size string to char pointer
    Yes, by using the c_str() member function of the std::string object. If you want this pointer to a null terminated string of characters to take ownership of the string contents, then you should copy the contents over, since the std::string retains ownership of what is returned by c_str().
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Aug 2005
    Posts
    113
    If you want to do this by char pointer then allocate the required memory through new operator.

    Code:
    char *a=new char[20];
    cin>>a;
    cout<<a;
    But be sure you don't enter more than 19 word string (1 reserved for NULL character)

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Quote Originally Posted by vaibhav
    If you want to do this by char pointer then allocate the required memory through new operator.

    Code:
    char *a=new char[20];
    cin>>a;
    cout<<a;
    But be sure you don't enter more than 19 word string (1 reserved for NULL character)
    Interesting. You're not supposed to use cin.get() for this anymore?
    And what is wrong with std::string? Even if you really wanted a C-style version stored somewhere that's easy enough to do.
    Last edited by whiteflags; 06-14-2006 at 11:57 AM.

  8. #8
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    Code:
    text[] = s.c_str()
    What does that do? I get a compilation error, maybe strcpy is needed?

  9. #9
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> char *text = new char[ s.size() ];
    >> text = s.c_str()

    That code is wrong citizen, you are assigning the pointer returned by c_str and leaking the memory allocated for text. Use strcpy. Also, you need to allocate space for the null terminator, so you should use new char[ s.size() + 1];.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    It stores the c-style string in text. Yeah, I didn't compile anything before I posted that. And .c_str() returns a const char, and I missed a semi-colon. I fixed it though.

  11. #11
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    Quote Originally Posted by vaibhav
    If you want to do this by char pointer then allocate the required memory through new operator.

    Code:
    char *a=new char[20];
    cin>>a;
    cout<<a;
    But be sure you don't enter more than 19 word string (1 reserved for NULL character)
    No.
    I want to do this without giving size.
    I mean if we use string variable.We didn't give size before use.And string can allocate memory whatever its size at runtime.For example you said we must enter 19 character.But if a was a string, its size is not important.

    String is not a variable type.It uses char pointers.I want to understand how string can achieve this. I hope i can explain what my problem is.

    Thanks for answers.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    string is a class that defines a type. There are different ways for it to be implemented, but it generally has a dynamic array of characters internally that it uses to store the string. If you have a variable of type string (like your s above), then it has overloaded the operator>> to read in characters from the input stream like cin.

  13. #13
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by sawer
    No.
    I want to do this without giving size.
    I mean if we use string variable.We didn't give size before use.And string can allocate memory whatever its size at runtime.For example you said we must enter 19 character.But if a was a string, its size is not important.

    String is not a variable type.It uses char pointers.I want to understand how string can achieve this. I hope i can explain what my problem is.

    Thanks for answers.
    std::string is a variable type. The implementation is unimportant.

    If you don't want to worry about giving the size of the string before stroring it, that's what std::string is for.

    If you really want to make things dificult for yourself by using C strings, you can create a function or an object that will store put to memmory undefined length string to a character array. After all that's what std::string does internally, and it's written in C++. I don't actually know how std::string manages it, but vectors allow simmilar functionality by allocating a huge amount of space, and realocating more if your data does not fit. I suspect std::string works the same way.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  14. #14
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    A common solution is to double the amount of data allocated whenever necessary. This will not use up too much memory, but is stll very fast.

    std::string will automatically resize to fit any string you pass it, at least until you run out of RAM.
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #15
    Registered User
    Join Date
    Jan 2006
    Posts
    61
    ok.
    Thanks friends.
    I see that string doesn't use only one pointer to reallocate memory while user enter characters.

    Thanks.
    Good works...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. The Interactive Animation - my first released C program
    By ulillillia in forum A Brief History of Cprogramming.com
    Replies: 48
    Last Post: 05-10-2007, 02:25 AM
  3. Direct3D problem
    By cboard_member in forum Game Programming
    Replies: 10
    Last Post: 04-09-2006, 03:36 AM
  4. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 05:59 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM