Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Pointer to Pointer to character

Reply
Thread Tools

Pointer to Pointer to character

 
 
howachen@gmail.com
Guest
Posts: n/a
 
      10-07-2006
a simple program...

#include <iostream>

using namespace std;

int main(int argc, char** argv) {

cout<<argv<<endl;
cout<<**argv<<endl;
cout<<argv[0][0]<<endl;
cout<<&argv[0]<<endl;
cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]?

}

why &argv[0] & &(argv[0][0]) difference?

 
Reply With Quote
 
 
 
 
Frederick Gotham
Guest
Posts: n/a
 
      10-07-2006
Howachen posted:

> #include <iostream>



You need to include "ostream" if you want to use "endl".


> using namespace std;
>
> int main(int argc, char** argv) {
>
> cout<<argv<<endl;



I don't think ostream:perator<< has an overload which takes a char**.


> cout<<**argv<<endl;



This will print a single char.


> cout<<argv[0][0]<<endl;



As will this. (Because it's equivalent to:

*(*(argv + 0) + 0)

which is equal to:

**argv


> cout<<&argv[0]<<endl;



This is equal to:

&*(argv+0)

which is equal to:

argv+0

which is equal to:

argv


> cout<<&(argv[0][0])<<endl; // why not the same as &argv[0]?



This is equal to:

&*(*(argv+0)+0)

which is equal to:

*(argv+0)

Which is equal to:

*argv

--

Frederick Gotham
 
Reply With Quote
 
 
 
 
Phlip
Guest
Posts: n/a
 
      10-07-2006
howachen wrote:

> why &argv[0] & &(argv[0][0]) difference?


Because argv[0] is type char * and argv[0][0] is type char. So their
addresses are char** and char* respectively.

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
Phlip
Guest
Posts: n/a
 
      10-07-2006
> howachen wrote:

>> why &argv[0] & &(argv[0][0]) difference?


> Because argv[0] is type char * and argv[0][0] is type char. So their
> addresses are char** and char* respectively.


Also because argv[0] is a pointer, so the address of the pointer itself is
here, while argv[0][0] is a character, so its address is there.

assert (argv[0] == &argv[0][0]);

--
Phlip
http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


 
Reply With Quote
 
howachen@gmail.com
Guest
Posts: n/a
 
      10-07-2006

Phlip 寫道:

> > howachen wrote:

>
> >> why &argv[0] & &(argv[0][0]) difference?

>
> > Because argv[0] is type char * and argv[0][0] is type char. So their
> > addresses are char** and char* respectively.

>
> Also because argv[0] is a pointer, so the address of the pointer itself is
> here, while argv[0][0] is a character, so its address is there.
>
> assert (argv[0] == &argv[0][0]);
>
> --
> Phlip
> http://www.greencheese.us/ZeekLand <-- NOT a blog!!!


thanks first...

so &argv[0][0] is the pointer to the first element in the first row

but how to print out this address as integer?

i can do this with &argv[0], but how to do it with argv[0][0]?

thanks.

 
Reply With Quote
 
Renee Klawitter
Guest
Posts: n/a
 
      10-07-2006
Frederick Gotham <(E-Mail Removed)> writes:

> Howachen posted:
>
>> #include <iostream>

>
> You need to include "ostream" if you want to use "endl".
>


I don't think so, as <iostream> includes <ostream> - at least
on my system. But judging from its name, it should be similar
on other platforms, too.


/Renee

--
For contacting me: klawitter(at)email(dot)de
--
"The major difference between a thing that might go wrong and a thing
that cannot possibly go wrong is that when a thing that cannot
possibly go wrong goes wrong it usually turns out to be impossible
to get at or repair." Douglas Adams, Mostly Harmless
 
Reply With Quote
 
BobR
Guest
Posts: n/a
 
      10-08-2006

Renee Klawitter wrote in message <(E-Mail Removed)>...
>Frederick Gotham <(E-Mail Removed)> writes:
>
>> Howachen posted:
>>
>>> #include <iostream>

>>
>> You need to include "ostream" if you want to use "endl".
>>

>I don't think so, as <iostream> includes <ostream>


BUT, it's not written/required in the standards.

> - at least on my system.


BUT, not on system xyz. What about the future when your implementation
removes <ostream> from <iostream>?

> But judging from its name, it should be similar on other platforms, too.


Usually is, BUT, you can't be sure!

>/Renee


If <iostream> includes <ostream> and you include <ostream> again, it won't
hurt much because of include guards. So, it's best to be safe and include
both.

--
Bob R
POVrookie


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      10-08-2006
BobR wrote:
> Renee Klawitter wrote in message <(E-Mail Removed)>...
>
>>Frederick Gotham <(E-Mail Removed)> writes:
>>
>>
>>>Howachen posted:
>>>
>>>
>>>>#include <iostream>
>>>
>>>You need to include "ostream" if you want to use "endl".
>>>

>>
>>I don't think so, as <iostream> includes <ostream>

>
>
> BUT, it's not written/required in the standards.
>

Considering std::iostream has std:stream as a base class, it would be
rather hard to avoid <iostream> including <ostream>.

--
Ian Collins.
 
Reply With Quote
 
red floyd
Guest
Posts: n/a
 
      10-08-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> Phlip 寫道:
>
>>> howachen wrote:
>>>> why &argv[0] & &(argv[0][0]) difference?
>>> Because argv[0] is type char * and argv[0][0] is type char. So their
>>> addresses are char** and char* respectively.

>> Also because argv[0] is a pointer, so the address of the pointer itself is
>> here, while argv[0][0] is a character, so its address is there.
>>
>> assert (argv[0] == &argv[0][0]);
>>
>> --
>> Phlip
>> http://www.greencheese.us/ZeekLand <-- NOT a blog!!!

>
> thanks first...
>
> so &argv[0][0] is the pointer to the first element in the first row
>
> but how to print out this address as integer?
>
> i can do this with &argv[0], but how to do it with argv[0][0]?
>


std::cout << static_cast<void *>(&argv[0][0]) << std::endl;

operator<<(ostream&, const char *) is overloaded so as to do the "right
thing" for C-style strings.
 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-10-2006
Frederick Gotham wrote:
> Howachen posted:
>
>> #include <iostream>

>
>
> You need to include "ostream" if you want to use "endl".
>
>
>> using namespace std;
>>
>> int main(int argc, char** argv) {
>>
>> cout<<argv<<endl;

>
>
> I don't think ostream:perator<< has an overload which takes a char**.
>
>


There is one that takes a const void* that this will convert to.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
character encoding +missing character sequence raavi Java 2 03-02-2006 05:01 AM
getting the character code of a character in a string Velvet ASP .Net 9 01-19-2006 09:27 PM
warning: multi-character character constant...help me! mimmo C Programming 4 04-10-2004 08:15 PM
Character reference "&#c" is an invalid XML character cgbusch XML 6 09-02-2003 07:04 PM
question: reading character for character from stdin KwikRick Python 1 08-22-2003 05:56 PM



Advertisments