Note that C and C++ don't have strings as a native data type. Note that
C++ also has a class named string, but that is not a native data type but
a standardized class.
If you write:
char name1[] = "Hello";
You will get a character array containing the characters 'H', 'e', 'l',
'l', 'o', '\0', where the last character is a terminator. All strings in C
are zero-terminated.
If you write:
char name2[] = {'H', 'e', 'l', 'l', 'o'};
You do not have this terminating zero. If you do:
printf("This is a string: %s\n",name1);
you will get an expected printout.
If you do:
printf("This is a string: %s\n",name2);
You will get zero or more random characters after the 'o', since printf()
will continue to print characters until a zero is found in the memory. The
same thing happens if you use cout << name1 contra cout << name2;
If you do want to create the string in character-by-character form, you
should instead write:
char name3[] = {'H', 'e', 'l', 'l', 'o', '\0'};
This is identical to the name1 alternative, and will print correctly.
Another thing: Since C and C++ don't have a native string datatype, a
string is an array of characters. And an array and a pointer is just about
identical. This means that the cout function will assume that any pointer
to a character (either an array name, or the address of a single character
in the array or a character pointer or a string "Hello" will all match the
same output rule, i.e. to print out a zero-terminated array of characters.
Note that many compilers will warn if you take the address of an array,
since that normally means that the developer have make an error. The array
name is always a pointer, and &name may look like a pointer to a pointer
to a character but since name is an array, you can't "move" it by
assigning to this dual-indirection pointer. Hence the warning about
conceptual error.
/pwm
On Mon, 23 Mar 2009, JYL wrote:
> Hi, everyone
>
> I have a question about pointer in C++, below is my source code and
> question, if anyone can answer my question, I will very appreciate, thanks a
> lot
>
>
>
> //source code and question start here
>
>
>
> #include <iostream>
>
> #include <cstdlib>
>
> using namespace std;
>
> int main(void)
>
> {
>
> int num[]={11,22,33,44,55};
>
> char name[6]={'j','i','a','n','y','i'};
>
> char *ptr="How are you?";
>
>
>
>
>
> cout << "int num[]={11,22,33,44,55}" << endl;
>
> cout << "num=" << num << endl;
>
> cout << "&num=" << &num << endl;
>
> cout << "*num=" << *num << endl;
>
>
>
> cout<< endl << "char name[6]={'j','i','a','n','y','i'};" << endl;
>
> cout << "name=" << name << endl;
>
> /*here,
>
> question 1: why output name is all of the array content,
>
> not the address as same as output of &name??
>
> the array name is a pointer, doesn't the content of array name is the
> same as the address of array name
>
>
>
> question 2: why there is a random character after
>
> output of i??
>
>
>
> */
>
> cout << "&name=" << &name << endl;
>
> cout << "*name=" << *name << endl;
>
>
>
> cout << endl << "char *ptr=\"How are you?\";" << endl;
>
> cout << "ptr=" << ptr << endl;
>
> /* here, the same of question 1*/
>
> cout << "&ptr=" << &ptr << endl;
>
> cout << "*ptr=" << *ptr << endl;
>
>
>
> ptr=ptr+1;
>
> cout << endl << "ptr=ptr+1;" << endl;
>
> cout << "ptr=" << ptr << endl;
>
> cout << "&ptr=" << &ptr << endl;
>
> /* here, why the address of ptr doesn't change??*/
>
> cout << "*ptr=" << *ptr << endl;
>
>
>
> system("pause");
>
> return 0;
>
> }
>
>
|