Thread: Pointers and array of char[]

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    224

    Cool Pointers and array of char[]

    Code:
    	char name1[50];
    	char *pname;
    
    	cout << endl << i << ". Please enter your full name: ";
    	cin.getline(name1, 50);
    
    	pname = name1;	// The address of the pointer is now the address of the first element of the array
    
    	system("pause");
    	cout << "\n*pname is " << *pname << " and " << "pname is " << pname << endl;
    	system("pause");
    Hi,

    I cant understand for the life of me why pname becomes the full length of the string.

    Can someone explain this code for me please?

    Thanks

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    pname holds the address of the first member of your array, so the memory represented by pname is the same as name1. The length of name1 is defined by the 50 you give, and if you enter a string into that memory, strlen(pname) and strlen(name1) will give the same result (actually, if you where to stop inside strlen() and look at the string argument, it would be the same for both of those cases).

    A pointer is really just a variable that holds the memory location of something - in this case name1.

    Note also that the comment is slightly misleading. It should say "the address in the pointer is now the address of the first element ...", not "the address of the pointer". The address OF the pointer is still the same as when the variable is first introduced a few lines up - 52 more than the start of name, most likely (52 because it's rounded to an even 4 - it could also be 56 if the machine has 64-bit (8 byte) pointers).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Another thing is that cin and cout simply have a special behaviour for arguments of char* type. If you tried with an array of a different type, getline would not compile, and cout would print the address of the pointer, not the contents pointed to.

    So if you want to see the address of the char*, you can cast it to a void*, so that cout would treat it like any other pointer:
    Code:
    cout << "\n*pname is " << *pname << " and " << "pname is " << (void*)pname << endl;
    Last edited by anon; 11-13-2008 at 05:03 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    So your question is why does *pname output a single char while pname outputs the entire string? pname is a pointer to char, so *pname is a char, thus you get a char. Being a pointer to char, pname is considered by cout to be a C-style string (the char array must be zero-terminated), so it outputs the whole string. As anon mentioned, casting to another pointer type will output the address.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  3. Quick question about SIGSEGV
    By Cikotic in forum C Programming
    Replies: 30
    Last Post: 07-01-2004, 07:48 PM
  4. Passing pointers between functions
    By heygirls_uk in forum C Programming
    Replies: 5
    Last Post: 01-09-2004, 06:58 PM
  5. Struct *** initialization
    By Saravanan in forum C Programming
    Replies: 20
    Last Post: 10-09-2003, 12:04 PM