hae i need some help

i made a program in which i used


char *name;
cin>>name;
also i used
cin>>*name;
but both of these syntax are not giving any error but at runtime debug window opens and tell me whats unresolved external errors
i think it will be stupid to ask these question frm ya but i am a new programmer what so ever so please reply me as soon as possible or tell me if i have any concept mistake




Answer this question

hae i need some help

  • Satch78

    In this situation it would be better to use a string.  You can't do:

    char *name="raheel";
    cin>>*name;

    Because what you're saying here is "I have a pointer to some characters.  The pointer to these characters is pointing to a string constant with the letters 'raheel' in it"

     

    You could do
    char name[30];

    cin>>name;

    because here the [30] is shorthand for allocating 30 bytes of space and having name point to the first byte.  This space will be destroyed once you leave the current scope (hit the tailing }).  Or you could do

    char* name = new char[30];

    Which would do the same thing, but you would have to take care of deleting the allocated memory yourself. 

     

    However, using cin>>name in this case is a very BAD idea because what if they write in more than 30 characters   Then they will be over writing memory you did not allocate and could do all sorts of nasty things like overwriting the return address for your function and letting them execute arbitrary code.  If you want to try this, just do

    char name[2];

    cin >> name;

     

    Then enter a long name.  You'll overwrite the return address and break the program.  A hacker could take advantage of this by sending in their own code and having the overwriting the return address with the location of this code. 

     

    What you probably should do in this case is either:

    #include <string>

    #include <iostream>

    using namespace std;

    int main()

    {

    string name;

    cin >> name;

    }

    Since the string class will grow itself automatically to hold the input, or use the secure CRT functions in Visual Studio 2005 to guard against overflows:

    #include <stdio.h>

    #define BUF_SIZE 128

    int main()

    {

        char buf[BUF_SIZE];

        scanf_s("%127s",buf,BUF_SIZE);

    }

     

    By using scanf_s, you are telling it not to scan in any input longer than BUF_SIZE (128) characters.  The %127s says to stop scanning at the 127th character (the 128th is the terminating null). 



  • Drudkh

    The reason you need to use the [] is because raheel is an 'array' of characters i.e 6 sequential characters that will reside sequentially in memory. You can't put 6 characters into the space of one character only. That's why he suggested the use of [] which indicates an array of the type you had specified, in this case an array of characters of 8 bits each.

  • kenjioba

    Now even I'm confused. :)
  • sachinsharma

    Keep on trying mate. It's the only way to learn. You'll run into the same problems if you read a book or not.

  • xjimx

    The ASCII characterset has 256 characters.
    Each character is represented by a binary number.
    8 bits provide 8 to the power of 2 (=256) combinations.
    Therefore each character is represented by an 8 bit binary number.
    Ok so far

    when you do this
    char myChar = 'y';

    you are reserving 8 bits of memory which will be populated by the 8 bit binary representation of you character 'y'.
    If you want to put your name 'raheel' in memory you need more than 8bits. Because each character in your name takes 8 bits to represent.
    Are you following

    So 'raheel' which has 6 characters needs 6 * 8bit  blocks of memory reserved.
    It would make logical sense to have these memory blocks one after the other and so we use an array. An array means that we are saying from memory location  x every 8 bits thereafter is a character. Arrays are declared by using the []. The number 50 (which was perhaps a little too big) in  [50] was saying to the compiler 'reserve me 50 times 8bits one after the other of memory because I want to store my name in it'.

  • RobbieJones

    But it would make much more sense to just use a string.

  • AdrianWalls

    This want work too. Because you initialized it with a constant string.

    char *name = new char[50]; // would be correct


    But take my advise: Don't try. Read a book first!


  • The Goat

    *name is a pointer to a character array. The pointer itself is not initialized, so it pointes to a memory address that is not valid.
    You cannot read a stream into a pointer that is not initialized. The effect is that the program crashes.

    Please read a beginners book about C++
    http://forums.microsoft.com/msdn/ShowPost.aspx PostID=107484



  • KAK

    hae dude thanks for being nice and so helpful but the thing is that u said i havent initialized it with any thing i initialized it with my name raheel
    char *name="raheel";
    cin>>*name;
    also
    cin>>name;
    cout<<name; or cout<<name;
    but they both didnt work Tongue TiedSad

  • Kyle J.

    In this situation it would be better to use a string.  You can't do:

    char *name="raheel";
    cin>>*name;

    Because what you're saying here is "I have a pointer to some characters.  The pointer to these characters is pointing to a string constant with the letters 'raheel' in it"

     

    You could do
    char name[30];

    cin>>name;

    because here the [30] is shorthand for allocating 30 bytes of space and having name point to the first byte.  This space will be destroyed once you leave the current scope (hit the tailing }).  Or you could do

    char* name = new char[30];

    Which would do the same thing, but you would have to take care of deleting the allocated memory yourself. 

     

    However, using cin>>name in this case is a very BAD idea because what if they write in more than 30 characters   Then they will be over writing memory you did not allocate and could do all sorts of nasty things like overwriting the return address for your function and letting them execute arbitrary code.  If you want to try this, just do

    char name[2];

    cin >> name;

     

    Then enter a long name.  You'll overwrite the return address and break the program.  A hacker could take advantage of this by sending in their own code and having the overwriting the return address with the location of this code. 

     

    What you probably should do in this case is either:

    #include <string>

    #include <iostream>

    using namespace std;

    int main()

    {

    string name;

    cin >> name;

    }

    Since the string class will grow itself automatically to hold the input, or use the secure CRT functions in Visual Studio 2005 to guard against overflows:

    #include <stdio.h>

    #define BUF_SIZE 128

    int main()

    {

        char buf[BUF_SIZE];

        scanf_s("%127s",buf,BUF_SIZE);

    }

     

    By using scanf_s, you are telling it not to scan in any input longer than BUF_SIZE (128) characters.  The %127s says to stop scanning at the 127th character (the 128th is the terminating null). 



  • hae i need some help