473,513 Members | 2,263 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Stroustrup 5.9, exercise 1

------ 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 ?

Mar 30 '07 #1
12 1748
* 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: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 30 '07 #2
On Mar 30, 7:42 pm, "Alf P. Steinbach" <a...@start.nowrote:
* arnuld:
// a reference to an array of 10 integers
char& r_arr_int = arr_int;
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]$

Mar 30 '07 #3
* 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.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Mar 30 '07 #4
On Mar 30, 9:19 pm, "Alf P. Steinbach" <a...@start.nowrote:

Well, what does that tell you? 'int' on one side, 'char' on the other
side, they're not the same.
really very SORRY for the stupidity i have done

Mar 30 '07 #5
On 30 Mar, 16:00, "arnuld" <geek.arn...@gmail.comwrote:
// 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

Mar 30 '07 #6
On Mar 30, 10:21 pm, "Gavin Deane" <deane_ga...@hotmail.comwrote:
On 30 Mar, 16:00, "arnuld" <geek.arn...@gmail.comwrote:
// 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

Mar 30 '07 #7
On 30 Mar, 18:32, "arnuld" <geek.arn...@gmail.comwrote:
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

Mar 30 '07 #8
arnuld <ge*********@gmail.comwrote:
// 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[i] << ": " << (*pacs)[i] << '\n';
}
}

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 3 '07 #9
On Apr 4, 2:39 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
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[i] << ": " << (*pacs)[i] << '\n';
}
}
Did you mean to cause undefined behaviour?

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

Apr 4 '07 #10
Old Wolf <ol*****@inspire.net.nzwrote:
On Apr 4, 2:39 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>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[i] << ": " << (*pacs)[i] << '\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.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 4 '07 #11
Marcus Kwok <ri******@gehennom.invalidwrote:
Old Wolf <ol*****@inspire.net.nzwrote:
>Did you mean to cause undefined behaviour?

No, I did not. What did I do wrong?
OK, after looking at the code more closely, I think the reference to
pacs[i] is what is causing the UB.
>(Also, you should use 'const char*' rather than 'char*').

True.
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)[i] << '\n';
}
}
This one look OK?

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Apr 4 '07 #12
On Apr 5, 9:25 am, ricec...@gehennom.invalid (Marcus Kwok) wrote:
>
OK, after looking at the code more closely, I think the reference to
pacs[i] is what is causing the UB. Here is a revised program:

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

This one look OK?
Much better :)

Apr 4 '07 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

26
3117
by: Oplec | last post by:
Hi, I am learning standard C++ as a hobby. The C++ Programming Language : Special Edition has been the principal source for my information. I read the entirety of the book and concluded that I...
7
2330
by: arnuld | last post by:
problem: define functions F(char), g(char&) & h(const char&). call them with arguments 'a', 49, 3300, c, uc & sc where c is a char, uc is unsigned char & sc is signed char. whihc calls are legal?...
0
1743
by: arnuld | last post by:
Stroustrup has a table in section 4.9 of declarations and definitions. he asks to write a similar table but in opposite sense: char ch; // declaration with definition he asks to do the...
0
1743
by: arnuld | last post by:
this programme runs without any trouble. it took 45 minutes of typing. i posted it here so that people can save their precious time: // Stroustrup special edition // chapter 4 exercise 2 //...
2
5327
by: arnuld | last post by:
MAX and MIN values of CHAR could not be displayed. Why ? BTW, any advice on improvement ? (please remember i have covered chapter 4 only) ------------- PROGRAMME -------------- /*...
16
2348
by: arnuld | last post by:
i did what i could do at Best to solve this exercise and this i what i have come up with: ----------- PROGRAMME -------------- /* Stroustrup 5.9, exercise 3 STATEMENT: Use typedef to define...
11
1816
by: arnuld | last post by:
/* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a table of the name sof months o fyear and the number of days in each month. write out that table. Do this twice: 1.) using ar array of char...
6
3032
by: arnuld | last post by:
this one was much easier and works fine. as usual, i put code here for any further comments/views/advice: --------- PROGRAMME ------------ /* Stroustrup: 5.9 exercise 7 STATEMENTS: Define a...
14
2454
by: arnuld | last post by:
there is no "compile-time error". after i enter input and hit ENTER i get a run-time error. here is the code: ---------- PROGRAMME -------------- /* Stroustrup, 5.9, exercise 11 STATEMENT:...
27
2417
by: arnuld | last post by:
it works fine without any trouble. i want to have advice on improving the code from any angle like readability, maintenance etc: ---------- PROGRAMME ------------ /* Stroustrup, 5.9, exercise 11...
0
7265
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7171
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7547
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7114
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7541
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5693
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5098
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3240
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
461
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.