?
Solved

Pointer to a character problem

Posted on 2003-03-03
4
Medium Priority
?
204 Views
Last Modified: 2010-04-01
Given the program below:

//**** begin
#include <iostream>
using namespace std;

void main()
{
     char data[] = {'a','b','c','d','\0'};
     char * b = data;
     cout << &data << endl << b << endl;
     cout << &b << endl << *b << endl;
}
//**** end

Why does it output the following?
//****
0012FF78
abcd
0012FF74
a
//****

I had expected "cout << b" to print out the memory address contained in b, but rather it prints out the characters in the array.  I think that "cout << &data" will print the address of the first character, but "cout << &data[1]" will print out "bcd" instead of a memory address.  

Does it have to do with the fact that the pointer is passed to cout, which is taking the array by reference or something? (Not sure if this question makes any sense, sorry.)

I'd really appreciate any help someone can give me in understanding the matter. Thanks in advance.

0
Comment
Question by:hamsterboy143
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 8

Accepted Solution

by:
Exceter earned 200 total points
ID: 8061601
>> I had expected "cout << b" to print out the memory address contained in b, but rather it prints out the characters in the array.

No, b is a pointer, as is data, cout either will print out the data contained at the memory address they point to. To print out the address you would have to say,

cout << *b;

>> I think that "cout << &data" will print the address of the first character

That is correct. All other array locations are reached via an offset or index.

>> cout << &data[1]" will print out "bcd" instead of a memory address.  

That is because cout start reading data at the address of data plus the index. So it skips the 'a' at the beginning of the array.

Exceter
0
 
LVL 11

Expert Comment

by:bcladd
ID: 8061722
Pointers to char are special. That is how C stores strings (as you stored the null-terminated batch of characters in data). Thus when passing a char * to cout, it assumes you are trying to print a C-style string.

You can prove this to your self by changing the content type to int.

-bcl
0
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8062018
>>To print out the address you would have to say,

>>cout << *b;
*b would print out 'a' (in the case that 'a' is first char)
&b prints the address of b
>>>> I think that "cout << &data" will print the address of the first character

>>That is correct. All other array locations are reached via an offset or index.

cout<<&data prints the address of the array or the address of where 'a' is.
the offset can be reached by
cout<<*(b+1)  or 'b' in this case
0
 

Expert Comment

by:vinay_krishna
ID: 8063650
Bsladd is right.

there is one advantage of using pointer to char as it acn be reassigned to differnt chars.
 
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question