Stroustrup 5.9, exercise 1

A

arnuld

------ PROGRAMME -----------
/* Stroustrup, 5.9, exercises

to write some declarations (with initializers)
comments explain what we intend to do
*/


#include<iostream>


int main()
{

// a pointer to a character
char c1 = 'a';
char* pc1 = &c1;

// an array of 10 integers
const int arr_size = 10;
char arr_int[arr_size];

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

// a pointer to an array of character strings
/* SORRY but i do not know how to present an array
of character strings */

// a pointer to a pointer to a char
char c;
char* pc = &c;
char* ppc = pc;

// a constant integer
const int ci = 7;

// a pointer to a constant integer
const int* pci = &ci;

// a constant pointer to an integer
int j = 8;
int *const cpi = &j;


return 0;
}

--------- OUTPUT ---------------
[arch@voodo tc++pl]$ g++ 5.9_ex-01.cpp
5.9_ex-01.cpp: In function 'int main()':
5.9_ex-01.cpp:23: error: invalid initialization of non-const reference
of type 'char&' from a temporary of type 'char*'
[arch@voodo tc++pl]$
-----------------------------------

i did not use "-ansi -W" flags as they were emitting warnings i did
not need to correct at this moment. i know that this is an error:

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

i dont find any way to create a reference to an integer. BTW, are all
declarations fine ?
 
A

Alf P. Steinbach

* arnuld:
i know that this is an error:

// a reference to an array of 10 integers
char& r_arr_int = arr_int;

i dont find any way to create a reference to an integer. BTW, are all
declarations fine ?

What you have above is a reference to char, initialized with an array of
integers.

Apples on one side, oranges on the other side.

This is a reference to an array:

int (&r_arr_int)[10] = arr_int;
 
A

arnuld

* arnuld:
What you have above is a reference to char, initialized with an array of
integers.

Apples on one side, oranges on the other side.

This is a reference to an array:

int (&r_arr_int)[10] = arr_int;

it does not compile:

---------- PROGRAMME -------------
/* Stroustrup, 5.9, exercises

to write declarations with initializers.
comments explain what we intend to do.

*/


#include<iostream>


int main()
{

// a pointer to a character
char c1 = 'a';
char* pc1 = &c1;

// an array of 10 integers
const int arr_size = 10;
char arr_int[arr_size];

// a reference to an array of 10 integers
int (&r_arr_int)[arr_size] = arr_int;

// a pointer to an array of character strings
/* SORRY but i do not know how to present an array
of character strings */

// a pointer to a pointer to a char
char c;
char* pc = &c;
char* ppc = pc;

// a constant integer
const int ci = 7;

// a pointer to a constant integer
const int* pci = &ci;

// a constant pointer to an integer
int j = 8;
int *const cpi = &j;


return 0;
}

-------- OUTPUT ----------
[arch@voodo tc++pl]$ g++ 5.9_ex-01.cpp
5.9_ex-01.cpp: In function 'int main()':
5.9_ex-01.cpp:24: error: invalid initialization of reference of type
'int (&)[10]' from expression of type 'char [10]'
[arch@voodo tc++pl]$
 
A

Alf P. Steinbach

* arnuld:
5.9_ex-01.cpp: In function 'int main()':
5.9_ex-01.cpp:24: error: invalid initialization of reference of type
'int (&)[10]' from expression of type 'char [10]'
[arch@voodo tc++pl]$

Well, what does that tell you? 'int' on one side, 'char' on the other
side, they're not the same.
 
G

Gavin Deane

// a pointer to a pointer to a char
char c;
char* pc = &c;
char* ppc = pc;

No, that's not right. Reading the declaration right to left again:

char* ppc
^ ^ ^
3 2 1

ppc [1] is a pointer to [2] char [3]. You wanted a pointer to a
pointer to a char, which is different.

You initialised it as a copy of pc. So now you have two different
pointers-to-char (pc and ppc) that currently both happen to point to
the same char (c).

* in the type declaration means "pointer to", and the type you want to
declare includes the phrase "pointer to" twice.

Gavin Deane
 
A

arnuld

// a pointer to a pointer to a char
char c;
char* pc = &c;
char* ppc = pc;

No, that's not right. Reading the declaration right to left again:

char* ppc
^ ^ ^
3 2 1

ppc [1] is a pointer to [2] char [3]. You wanted a pointer to a
pointer to a char, which is different.

You initialised it as a copy of pc. So now you have two different
pointers-to-char (pc and ppc) that currently both happen to point to
the same char (c).

* in the type declaration means "pointer to", and the type you want to
declare includes the phrase "pointer to" twice.

Gavin Deane

i must call it a BUG (arose of typing) what i actually meant was:

char c;
char* pc = &c;
char** ppc = &pc;

and it compiles without any trouble
 
G

Gavin Deane

i must call it a BUG (arose of typing) what i actually meant was:

char c;
char* pc = &c;
char** ppc = &pc;

and it compiles without any trouble

And does what you want it to. Looks like I was explaining something
you already knew.

Gavin Deane
 
M

Marcus Kwok

arnuld said:
// a pointer to an array of character strings
/* SORRY but i do not know how to present an array
of character strings */

Let me know if you understand this:


#include <iostream>

int main()
{
const int array_size = 4;

char* char_string_array[array_size] = {"one", "two", "three", "four"};

// a pointer to an array of character strings
char* (*pacs)[array_size] = &char_string_array;

for (int i = 0; i != array_size; ++i) {
std::cout << pacs << ": " << (*pacs) << '\n';
}
}
 
O

Old Wolf

Let me know if you understand this:

#include <iostream>

int main()
{
const int array_size = 4;

char* char_string_array[array_size] = {"one", "two", "three", "four"};

// a pointer to an array of character strings
char* (*pacs)[array_size] = &char_string_array;

for (int i = 0; i != array_size; ++i) {
std::cout << pacs << ": " << (*pacs) << '\n';
}
}


Did you mean to cause undefined behaviour?

(Also, you should use 'const char*' rather than 'char*').
 
M

Marcus Kwok

Old Wolf said:
Let me know if you understand this:

#include <iostream>

int main()
{
const int array_size = 4;

char* char_string_array[array_size] = {"one", "two", "three", "four"};

// a pointer to an array of character strings
char* (*pacs)[array_size] = &char_string_array;

for (int i = 0; i != array_size; ++i) {
std::cout << pacs << ": " << (*pacs) << '\n';
}
}


Did you mean to cause undefined behaviour?


No, I did not. What did I do wrong?
(Also, you should use 'const char*' rather than 'char*').

True.
 
M

Marcus Kwok

Marcus Kwok said:
No, I did not. What did I do wrong?

OK, after looking at the code more closely, I think the reference to
pacs is what is causing the UB.

Here is a revised program:


#include <iostream>

int main()
{
const int array_size = 4;

const char* char_string_array[array_size] = {"one", "two", "three", "four"};

// a pointer to an array of character strings
const char* (*pacs)[array_size] = &char_string_array;

for (int i = 0; i != array_size; ++i) {
std::cout << (*pacs) << '\n';
}
}


This one look OK?
 
O

Old Wolf

OK, after looking at the code more closely, I think the reference to
pacs is what is causing the UB. Here is a revised program:

for (int i = 0; i != array_size; ++i) {
std::cout << (*pacs) << '\n';
}

This one look OK?


Much better :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top