By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,819 Members | 1,185 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,819 IT Pros & Developers. It's quick & easy.

Stroustrup, chapter 5, navigating arrays

P: n/a
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]$

Mar 7 '07 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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]$

Mar 7 '07 #2

P: n/a
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

Mar 7 '07 #3

P: n/a
lokki wrote:
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
Mar 7 '07 #4

P: n/a
On Mar 7, 4:17 pm, benben <benhonghatgmaildotcom@nospamwrote:
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]$

Mar 7 '07 #5

P: n/a
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

Mar 7 '07 #6

P: n/a
On Mar 7, 5:12 pm, "lokki" <jkusn...@gmail.comwrote:

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.

Mar 7 '07 #7

P: n/a
"arnuld" <ge*********@gmail.comwrote in message
news:11**********************@p10g2000cwp.googlegr oups.com...
On Mar 7, 4:17 pm, benben <benhonghatgmaildotcom@nospamwrote:
<snip>
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.

<snip>
>
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.

--
Dennis
Mar 7 '07 #8

P: n/a
On Mar 7, 5:29 pm, "Dennis \(Icarus\)" <nojunkm...@ever.invalidwrote:

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

:-)

Mar 7 '07 #9

P: n/a
On 7 Mar, 12:21, "arnuld" <geek.arn...@gmail.comwrote:
On Mar 7, 5:12 pm, "lokki" <jkusn...@gmail.comwrote:
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.
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

Mar 7 '07 #10

P: n/a
"arnuld" <ge*********@gmail.comwrote in message
news:11*********************@j27g2000cwj.googlegro ups.com...
<snip>
>
YES, it works

thanks Dennis

:-)
You're welcome.

Dennis
Mar 8 '07 #11

This discussion thread is closed

Replies have been disabled for this discussion.