Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c++ > #80660 > unrolled thread
| Started by | rob <rob@drrob.com> |
|---|---|
| First post | 2021-07-03 16:12 -0400 |
| Last post | 2021-07-08 08:17 +0000 |
| Articles | 6 — 5 participants |
Back to article view | Back to comp.lang.c++
Trying to understand pointers. Why does this give unexpected results? rob <rob@drrob.com> - 2021-07-03 16:12 -0400
Re: Trying to understand pointers. Why does this give unexpected results? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-07-03 21:27 +0100
Re: Trying to understand pointers. Why does this give unexpected results? rob <rob@drrob.com> - 2021-07-03 17:54 -0400
Re: Trying to understand pointers. Why does this give unexpected results? Real Troll <real.troll@trolls.com> - 2021-07-03 22:04 +0000
Re: Trying to understand pointers. Why does this give unexpected results? "Fred. Zwarts" <F.Zwarts@KVI.nl> - 2021-07-15 09:45 +0200
Re: Trying to understand pointers. Why does this give unexpected results? Juha Nieminen <nospam@thanks.invalid> - 2021-07-08 08:17 +0000
| From | rob <rob@drrob.com> |
|---|---|
| Date | 2021-07-03 16:12 -0400 |
| Subject | Trying to understand pointers. Why does this give unexpected results? |
| Message-ID | <5og1egt5kc9m17k8ndg5qiv9djdtk20844@4ax.com> |
This is my test program
#include everything relevant here
int main {
int a = 0;
float b = 1.0;
char c = 'c';
int *ptrA;
float *ptrB;
char *ptrC;
ptrA = &a;
ptrB = &b;
ptrC = &c;
cout << "value of a: " << a << "; address of a: " << ptrA << endl;
cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
cout << "value of c: " << c << "; address of c: " << ptrC << endl;
return 0;
}
This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an
address for c. It just prints "c"; this does show addresses for a and
b.
Why can't I show the address for the char variable c?
Thx,
Rob
[toc] | [next] | [standalone]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Date | 2021-07-03 21:27 +0100 |
| Message-ID | <87bl7jdty0.fsf@bsb.me.uk> |
| In reply to | #80660 |
rob <rob@drrob.com> writes:
> This is my test program
>
> #include everything relevant here
> int main {
And you need ()s there and at least a "using namespace std;" unless you
are using a museum version of C++.
> int a = 0;
> float b = 1.0;
> char c = 'c';
>
> int *ptrA;
> float *ptrB;
> char *ptrC;
>
> ptrA = &a;
> ptrB = &b;
> ptrC = &c;
>
> cout << "value of a: " << a << "; address of a: " << ptrA << endl;
> cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
> cout << "value of c: " << c << "; address of c: " << ptrC << endl;
What would you like this code to do:
char *string = "Hello world\n";
cout << string;
? Can you see the problem now?
> return 0;
> }
>
>
> This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an
> address for c. It just prints "c"; this does show addresses for a and
> b.
>
> Why can't I show the address for the char variable c?
You can if you do this:
cout << "value of c: " << c << "; address of c: " << (void *)ptrC << endl;
--
Ben.
[toc] | [prev] | [next] | [standalone]
| From | rob <rob@drrob.com> |
|---|---|
| Date | 2021-07-03 17:54 -0400 |
| Message-ID | <93n1eg56unidh0g7fine23uhtdtmos2lvc@4ax.com> |
| In reply to | #80661 |
Thanks, that helps me. >> This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an >> address for c. It just prints "c"; this does show addresses for a and >> b. >> >> Why can't I show the address for the char variable c? > >You can if you do this: > > cout << "value of c: " << c << "; address of c: " << (void *)ptrC << endl;
[toc] | [prev] | [next] | [standalone]
| From | Real Troll <real.troll@trolls.com> |
|---|---|
| Date | 2021-07-03 22:04 +0000 |
| Subject | Re: Trying to understand pointers. Why does this give unexpected results? |
| Message-ID | <sbqn1q$1t20$1@gioia.aioe.org> |
| In reply to | #80663 |
On 03/07/2021 22:54, rob wrote:
> Thanks, that helps me.
>
>
>
>
>>> This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an
>>> address for c. It just prints "c"; this does show addresses for a and
>>> b.
>>>
>>> Why can't I show the address for the char variable c?
>> You can if you do this:
>>
>> cout << "value of c: " << c << "; address of c: " << (void *)ptrC << endl;
You could also do something like this:
> #include <iostream>
>
> using namespace std;
>
> int main()
> {
> int a = 0;
> float b = 1.0;
> char c[] = "c";
>
> int *ptrA;
> float *ptrB;
> char *ptrC;
>
> ptrA = &a;
> ptrB = &b;
> ptrC = c;
>
> cout << "value of a: " << a << "; address of a: " << ptrA << endl;
> cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
> cout << "value of c: " << c << "; address of c: " << &ptrC << endl;
>
> return 0;
> }
[toc] | [prev] | [next] | [standalone]
| From | "Fred. Zwarts" <F.Zwarts@KVI.nl> |
|---|---|
| Date | 2021-07-15 09:45 +0200 |
| Subject | Re: Trying to understand pointers. Why does this give unexpected results? |
| Message-ID | <scop36$1k4k$1@gioia.aioe.org> |
| In reply to | #80664 |
Op 04.jul..2021 om 00:04 schreef Real Troll:
> On 03/07/2021 22:54, rob wrote:
>> Thanks, that helps me.
>>
>>
>>
>>
>>>> This program, compiled w/ gcc 9.3 on Pop_OS 20.04, does not print an
>>>> address for c. It just prints "c"; this does show addresses for a and
>>>> b.
>>>>
>>>> Why can't I show the address for the char variable c?
>>> You can if you do this:
>>>
>>> cout << "value of c: " << c << "; address of c: " << (void *)ptrC << endl;
>
> You could also do something like this:
>
>> #include <iostream>
>>
>> using namespace std;
>>
>> int main()
>> {
>> int a = 0;
>> float b = 1.0;
>> char c[] = "c";
>>
>> int *ptrA;
>> float *ptrB;
>> char *ptrC;
>>
>> ptrA = &a;
>> ptrB = &b;
>> ptrC = c;
>>
>> cout << "value of a: " << a << "; address of a: " << ptrA << endl;
>> cout << "vaue of b: " << b << "; address of b: " << ptrB << endl;
>> cout << "value of c: " << c << "; address of c: " << &ptrC << endl;
Doesn't that print the address of ptrC, instead of the address of c?
>>
>> return 0;
>> }
>
>
>
[toc] | [prev] | [next] | [standalone]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Date | 2021-07-08 08:17 +0000 |
| Message-ID | <sc6cab$1bks$3@gioia.aioe.org> |
| In reply to | #80660 |
rob <rob@drrob.com> wrote: > int *ptrA; > float *ptrB; > char *ptrC; > > ptrA = &a; > ptrB = &b; > ptrC = &c; I'm genuinely wondering why you are writing it like that, instead of the simpler: int *ptrA = &a; float *ptrB = &b; char *ptrC = &c; > cout << "value of c: " << c << "; address of c: " << ptrC << endl; A char* pointer is overloaded to print the string pointed to by that pointer, so that will severely malfunction. You can cast it to void* instead: std::cout << static_cast<void*>(ptrC) << std::endl;
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.c++
csiph-web