Thread: [Dev-C++] array + pointers???
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Noorez K. <coo...@ho...> - 2004-05-28 13:45:01
|
I have asked questions about pointers and arrays but all the answers are not
that great. Maybe I was unclear so I will explain my questions better...
first...
int main()
{
int nArray[5]={1,2,3,4,5};
cout << nArray << endl;/*this gives the address of the array*/
char sArray[6]={'h','e','l','l','o',\0};
cout << sArray << endl;/*this give the string hello, but i used the name
of the array without subscripts? Don't i get the address of it like in the
int array?*/
char* pszString = "hello";/*strangest of all*/
/*this looks as if you were trying to put "hello" where the address of a
char would go.*/
char String[] = "hello";
char *pszString2 = String;/*looks less confusing because the address of
the array would get assigned to 'pszString2' because of using the name of
the array without a subscript, but still is confusing because cout the name
of the char array without subscripts gives you the string*/
_________________________________________________________________
MSN Premium includes powerful parental controls and get 2 months FREE*
http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
|
|
From: <dan...@ya...> - 2004-05-28 14:19:41
|
--- Noorez Kassam <coo...@ho...>
escreveu: > I have asked questions about pointers and
arrays but
> int nArray[5]={1,2,3,4,5};
> cout << nArray << endl;/*this gives the address
> of the array*/
Yes, this is the default for pointers; if you use just
the name of the array, you get the address of the
begining of the array; so nArray acts just like a
pointer.
> char sArray[6]={'h','e','l','l','o',\0};
> cout << sArray << endl;/*this give the string
> hello, but i used the name
> of the array without subscripts? Don't i get the
> address of it like in the
> int array?*/
No. The << operator for ostream's is overloaded to
handle pointers to char (char*) in a special way. If
you cast your sArray (when you're printing it) to
another type, like void*, you'll get the address. i
suggest you to start first with C, not with C++. Or
get yourself a good book (D&D, TC++PL and so on).
> char* pszString = "hello";/*strangest of all*/
Isn't strange at all. You are declaring a C string
constant, and pointing pszString to it.
Daniel K. O.
______________________________________________________________________
Participe da pesquisa global sobre o Yahoo! Mail:
http://br.surveys.yahoo.com/global_mail_survey_br
|
|
From: Nicolas R. <ni...@bo...> - 2004-05-28 14:37:40
|
Hello Noorez,
> I have asked questions about pointers and arrays but all the answers=20=
> are not that great. Maybe I was unclear so I will explain my questions=20=
> better...
Thank you for the compliments. For great answers, I'd suggest (for the=20=
second time) that you google for some tutorials and reference. For even=20=
better answers, you can try reading the K&R ("The C Programming=20
Language) and/or Stroustroup's book ("The C++ Programming Language").
> int nArray[5]=3D{1,2,3,4,5};
> cout << nArray << endl;/*this gives the address of the array*/
Correct, because the name of an array of T is always a T* (a pointer to=20=
T). All arrays are pointers (implicitly).
> char sArray[6]=3D{'h','e','l','l','o',\0};
> cout << sArray << endl;/*this give the string hello, but i used the=20=
> name of the array without subscripts? Don't i get the address of it=20=
> like in the int array?*/
No, you get the string instead of the address because, as I told you=20
before, arrays of char get treated in a special way (due to the fact=20
that, in plain old C, they were the only way to represent strings of=20
characters). If you really want the address, you could cast the pointer=20=
to some other type first. If you don't know what 'to cast' means,=20
please look it up in TFM.
Again, as Per already said,
> cout always assumes that a pointer to a char is not just a pointer to=20=
> a single character but actually a pointer to the first character of a=20=
> zero-terminated string
Next question:
> char* pszString =3D "hello";/*strangest of all*/
> /*this looks as if you were trying to put "hello" where the address=20=
> of a char would go.*/
The easy way to think about this is the one I already said: C/C++=20
provide special syntax to assign constant strings into arrays of char.=20=
(It would be rather unpractical if they didn't.)
What's going on behind the scenes there is that the string "hello" gets=20=
created "on the fly" by the compiler as a constant array of chars that=20=
contains {'h', 'e', 'l', 'l', 'o', \0}. Then you create a=20
pointer-to-char called pszString. What does the assignment do? It just=20=
copies the address. So pszString now points to the same place where the=20=
compiler stored the array containing the constant string.
Good luck with your C/C++ learning. You will encounter many, many such=20=
quirks along the way. Please try to use the excellent and free FAQs and=20=
tutorials around. They are your friends.
Regards,
Nicol=E1s
|
|
From: Rafael C. <cha...@un...> - 2004-05-28 14:53:54
|
Hi,
I think the answers to your question were great.
Maybe it's not clear to you that we can have
int, float, double, char and other data types
arrays, **but when it comes to char arrays,
things are different**.
The point to consider is: there are things you
can do only with char arrays and pointers and
I/O functions (e.g. cout) can demonstrate that.
Suggestion: read more about C strings (not C++ "string" type).
Google: c array pointers (returns a lot of sites)
Hope this helps you.
Rafael
Noorez Kassam wrote:
> I have asked questions about pointers and arrays but all the answers are
> not that great. Maybe I was unclear so I will explain my questions
> better...
> first...
>
> int main()
> {
> int nArray[5]={1,2,3,4,5};
> cout << nArray << endl;/*this gives the address of the array*/
> char sArray[6]={'h','e','l','l','o',\0};
> cout << sArray << endl;/*this give the string hello, but i used the
> name of the array without subscripts? Don't i get the address of it
> like in the int array?*/
>
> char* pszString = "hello";/*strangest of all*/
> /*this looks as if you were trying to put "hello" where the address of
> a char would go.*/
>
> char String[] = "hello";
> char *pszString2 = String;/*looks less confusing because the address
> of the array would get assigned to 'pszString2' because of using the
> name of the array without a subscript, but still is confusing because
> cout the name of the char array without subscripts gives you the string*/
>
> _________________________________________________________________
> MSN Premium includes powerful parental controls and get 2 months FREE*
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU=http://hotmail.com/enca&HL=Market_MSNIS_Taglines
>
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: Oracle 10g
> Get certified on the hottest thing ever to hit the market... Oracle 10g.
> Take an Oracle 10g class now, and we'll give you the exam FREE.
> http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: <de...@ci...> - 2004-05-28 20:26:51
|
Let's go through your examples one by one.
int nArray[5]={1,2,3,4,5};
This declares an array of 5 ints, and initialises them to the values 1-5.
In terms of storage, the array and all the contents are on the stack.
nArray has type int[5] , i.e. an array of five ints.
cout << nArray << endl;
This passes nArray to the stream functions. The type int[5] easily
converts to int*.
Now the act of moving nArray to cout causes C++ to look for an overloaded
function operator<<(ostream, int*). Now there is no such overloading, so
the pointer is converted to a long, leading the the value of the pointer
being printed.
char sArray[6]={'h','e','l','l','o',\0};
Now the type of sArray is char[6] , i.e. an array of 6 chars.
As before the whole array is on the stack.
cout << sArray << endl;
Now in this case sArray easily converts from char[6] to char*.
There *is* an overloaded function operator<<(ostream, char*) as that's
what's used to print a string ; hence the string is printed.
So that's why the two arrays are handled differently.
char* pszString = "hello";
In this case the string "hello" is put into the static data segment during
compilation, and the initialiser sets the value of the pointer to point to
that stored string.
So there isn't an attempt to assign a long string into a short pointer at
all. In fact you can do this with assignments as well as initialisors
because the expression "hello" returns a char*. So all you're ever doing
is assigning one pointer to another.
char String[] = "hello";
char *pszString2 = String;
Because you've defined an array of characters, it is initialised to the
contents of the string, not a pointer to it. The size of the array is
set to the size of the string assigned, i.e. 6 including the null.
So String is an array of characters exactly as sArray was, and so resolves
to a char* and hence cout << String yields the contents.
And pszString2 is initialised to point to the same address and you get the
same result.
if you were to add the following lines:
cout << static_cast<void*>(sArray) << endl;
cout << static_cast<void*>(pszString) << endl;
cout << static_cast<void*>(String) << endl;
cout << static_cast<void*>(pszString2) << endl;
Then the pointer contents will be printed.
You will see that pszString is a very different value than the other
three, whereas String and pszString2 should be identical.
In article <BAY...@ho...>,
coo...@ho... (Noorez Kassam) wrote:
> I have asked questions about pointers and arrays but all the answers
> are not that great. Maybe I was unclear so I will explain my questions
> better...
> first...
>
> int main()
> {
> int nArray[5]={1,2,3,4,5};
> cout << nArray << endl;/*this gives the address of the array*/
> char sArray[6]={'h','e','l','l','o',\0};
> cout << sArray << endl;/*this give the string hello, but i used the
> name of the array without subscripts? Don't i get the address of it
> like in the int array?*/
>
> char* pszString = "hello";/*strangest of all*/
> /*this looks as if you were trying to put "hello" where the address
> of a char would go.*/
>
> char String[] = "hello";
> char *pszString2 = String;/*looks less confusing because the address
> of the array would get assigned to 'pszString2' because of using the
> name of the array without a subscript, but still is confusing because
> cout the name of the char array without subscripts gives you the
> string*/
>
> _________________________________________________________________
> MSN Premium includes powerful parental controls and get 2 months FREE*
>
> http://join.msn.com/?pgmarket=en-ca&page=byoa/prem&xAPID=1994&DI=1034&SU
> =http://hotmail.com/enca&HL=Market_MSNIS_Taglines
>
>
>
> -------------------------------------------------------
> This SF.Net email is sponsored by: Oracle 10g
> Get certified on the hottest thing ever to hit the market... Oracle
> 10g. Take an Oracle 10g class now, and we'll give you the exam FREE.
> http://ads.osdn.com/?ad_id=3149&alloc_id=8166&op=click
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|