Stroustrup, chapter 5, navigating arrays

A

arnuld

i have created a programme that prints the elements of an array.
programme is compiled without ant trouble but i am getting some
strange outputs:

------------------- PROGRAMME --------------------------
#include <iostream>

void print_array(char ar[])
{
std::cout << "printing elements:";
for(char* p=ar; *p !=0; p++)
std::cout << *p << "\t";

std::cout << "\n";
}


int main() {
char v1[] = { '1', '2', '3' };
char v2[] = { 'a', 'b', 'c'};

print_array(v1);
print_array(v2);
}



------------------- OUTPUT -----------------------------------
[arch@voodo cpp]$ ./a.out
printing elements:1 2 3 ? ? ?
? H ? ? ? ? G ? ??
? ? ? @ ?  H ?
? ? ? G ? ? 
printing elements:a b c 1 2 3
? ? ? ? H ? ? ? ?G
? ? ? ? ? ? @ ?
 H ? ? ? ? G ?? 
[arch@voodo cpp]$
 
B

boguslaw.filipowicz

The thing is that when you initialise an array that way, then it is
not automatically Null terminated. I mean it has exacly 3 elements.
You need add a 4th element to ensure only 3 elements will be printed
out. See below.
------------------- PROGRAMME --------------------------
#include <iostream>

void print_array(char ar[])
{
std::cout << "printing elements:";
for(char* p=ar; *p !=0; p++)
std::cout << *p << "\t";

std::cout << "\n";

}

int main() {
char v1[] = { '1', '2', '3', '\0' };
char v2[] = { 'a', 'b', 'c', '\0' };
print_array(v1);
print_array(v2);

}

------------------- OUTPUT -----------------------------------
[arch@voodo cpp]$ ./a.out
printing elements:1 2 3 ? ? ?
? H ? ? ? ? G ? ??
? ? ? @ ? H ?
? ? ? G ? ?
printing elements:a b c 1 2 3
? ? ? ? H ? ? ? ?G
? ? ? ? ? ? @ ?
H ? ? ? ? G ??
[arch@voodo cpp]$
 
L

lokki

Hello,

you didn't put 0 character at the end of array. You assume that after
the last element of your array will be 0 in memory. But isn't. The
loop continues until it finds one. Better would be:

char *v1 = "123";
char *v2 = "abc";

when filling character arrays at compile time. This will put 0
character at the end automatically.


-jk
 
B

benben

lokki said:
Hello,

you didn't put 0 character at the end of array. You assume that after
the last element of your array will be 0 in memory. But isn't. The
loop continues until it finds one. Better would be:

char *v1 = "123";
char *v2 = "abc";

when filling character arrays at compile time. This will put 0
character at the end automatically.


-jk

Just to add to lokki's comment for the sake of const-correctness,
C-style strings are better declared as:

const char* v1 = "123";
const char* v2 = "abc";

And so the print_array function should take const char* instead of char[].

Ben
 
A

arnuld

Just to add to lokki's comment for the sake of const-correctness,
C-style strings are better declared as:

const char* v1 = "123";
const char* v2 = "abc";

And so the print_array function should take const char* instead of char[].

Ben, i tried that and got loads of errors i do not understand:

---------------- PROGRAMME ------------------
#include <iostream>

void print_array(const char* ar[])
{
std::cout << "printing elements:";
for(char* p=ar; *p !=0; p++)
std::cout << *p << "\t";

std::cout << "\n";
}



int main() {
const char* v1[] = { '1', '2', '3', '4', '5', '\0' };
const char* v2 = "abcde";

print_array(v1);
print_array(v2);
}

----------------- OUTPUT ---------------
[arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra 531.cpp
531.cpp: In function 'void print_array(const char**)':
531.cpp:9: error: cannot convert 'const char**' to 'char*' in
initialization
531.cpp: In function 'int main()':
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
531.cpp:22: error: cannot convert 'const char*' to 'const char**' for
argument '1' to 'void print_array(const char**)'
[arch@voodo cpp]$
 
L

lokki

In function main() you defined array of char pointers, but you
initialised it as array of chars

const char* v1[] = { '1', '2', '3', '4', '5', '\0' }; // incorrect
const char* v2 = "abcde"; // correct

correct would be
const char v1[] = { '1', '2', '3', '4', '5', '\0' };

remember:

const char* p = "123"; is a pointer to char, that is pointing to the
first element in memory that is initialised to {'1', '2', '3', '\0'}

const char q[] = {'1','2','3','\0'}; is an array of characters,
initialised to the same values as previous example.

your function treat p and q equally because of pointer/array
relationship

Try to find out more about pointer and array relationship in books/
google

-jk
 
A

arnuld

const char* v1[] = { '1', '2', '3', '4', '5', '\0' }; // incorrect
const char* v2 = "abcde"; // correct

correct would be
const char v1[] = { '1', '2', '3', '4', '5', '\0' };

still it does not work:

#include <iostream>

void print_array(const char* ar[])
{
std::cout << "printing elements:";
for(char* p=ar; *p !=0; p++)
std::cout << *p << "\t";

std::cout << "\n";
}


int main() {
const char v1[] = { '1', '2', '3', '4', '5', '\0' };
const char* v2 = "abcde";

print_array(v1);
print_array(v2);
}

------------- OUTPUT -------------
[arch@voodo cpp]$ g++ -ansi -pedantic -Wall -Wextra 531.cpp
531.cpp: In function 'void print_array(const char**)':
531.cpp:9: error: cannot convert 'const char**' to 'char*' in
initialization
531.cpp: In function 'int main()':
531.cpp:21: error: cannot convert 'const char*' to 'const char**' for
argument '1' to 'void print_array(const char**)'
531.cpp:22: error: cannot convert 'const char*' to 'const char**' for
argument '1' to 'void print_array(const char**)'
[arch@voodo cpp]$


remember:

const char* p = "123"; is a pointer to char, that is pointing to the
first element in memory that is initialised to {'1', '2', '3', '\0'}

const char q[] = {'1','2','3','\0'}; is an array of characters,
initialised to the same values as previous example.

same values but no "pointing to 1st element". ok ?

your function treat p and q equally because of pointer/array
relationship

thanks, that was a new thing you told

Try to find out more about pointer and array relationship in books/
google

i follow Stroustrup actually. i *fear* that online material will
provide me with WRONG information.
 
A

arnuld

drop the []
void print_array(const char* ar)

531.cpp:9: error: cannot convert 'const char**' to 'char*' in
initialization

The parameter "char *ar[]" is a synonym for "char ** ar".
char ** is different from char *.
ok

Also change

for(const char* p=ar; *p !=0; p++)

The variable p needs to be const char * as well.

hmm....

int main() {
const char* v1[] = { '1', '2', '3', '4', '5', '\0' };

Drop the * from v1. Currently you're trying to declare an array of const
pointers to char,
This is what the message
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
is telling you - that the char values cannot be converted to a const char *

Making that change will also correct:
531.cpp:22: error: cannot convert 'const char*' to 'const char**' for
argument '1' to 'void print_array(const char**)'
since v1 will no loger be an a pointer to const char pointers.

YES, it works

thanks Dennis

:)
 
D

Dennis \(Icarus\)

arnuld said:
On Mar 7, 4:17 pm, benben <benhonghatgmaildotcom@nospam> wrote:
And so the print_array function should take const char* instead of
char[].

Ben, i tried that and got loads of errors i do not understand:

---------------- PROGRAMME ------------------
#include <iostream>

void print_array(const char* ar[])
{
std::cout << "printing elements:";
for(char* p=ar; *p !=0; p++)

drop the []
void print_array(const char* ar)

531.cpp:9: error: cannot convert 'const char**' to 'char*' in
initialization

The parameter "char *ar[]" is a synonym for "char ** ar".
char ** is different from char *.

Also change

for(const char* p=ar; *p !=0; p++)

The variable p needs to be const char * as well.

int main() {
const char* v1[] = { '1', '2', '3', '4', '5', '\0' };

Drop the * from v1. Currently you're trying to declare an array of const
pointers to char,
This is what the message
531.cpp:18: error: invalid conversion from 'char' to 'const char*'
is telling you - that the char values cannot be converted to a const char *

Making that change will also correct:
531.cpp:22: error: cannot convert 'const char*' to 'const char**' for
argument '1' to 'void print_array(const char**)'
since v1 will no loger be an a pointer to const char pointers.
 
G

Gavin Deane

i follow Stroustrup actually. i *fear* that online material will
provide me with WRONG information.

That's a very sensible fear. Some information available online is high
quality. Rather more of it is of questionable quality. And by
definition, if you are trying to learn something new, you have no way
of telling the difference.

Gavin Deane
 

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

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top