Array iteration

J

jeff_hann

Hi everyone. I'm a newbie in c++ and I have a question related to the
an exercise in Stroustrup's book, namely
5.9/8. It's about iterations in a given array using pointers vs using
indexes. The indexes one I managed to write, but when I try to execute
the pointer variant (it compiles ok on BSD and Linux also) , I get the
string I wanted to see plus lots of gibberish and a segfault. What's
wrong? Here's the code :

1 #include <iostream>
2
3 using namespace std;
4
5
6
7 int main()
8 {
9 int i;
10 char *p;
11 char v[] = "Testing...";
12
13 for(p = v; p != 0; p++)
14 cout << *p << endl;
15
16 return 0;
17 }
 
V

Victor Bazarov

jeff_hann said:
Hi everyone. I'm a newbie in c++ and I have a question related to the
an exercise in Stroustrup's book, namely
5.9/8. It's about iterations in a given array using pointers vs using
indexes. The indexes one I managed to write, but when I try to execute
the pointer variant (it compiles ok on BSD and Linux also) , I get the
string I wanted to see plus lots of gibberish and a segfault. What's
wrong? Here's the code :

1 #include <iostream>
2
3 using namespace std;
4
5
6
7 int main()
8 {
9 int i;

You don't need 'i' here, do you?
10 char *p;
11 char v[] = "Testing...";
12
13 for(p = v; p != 0; p++)

How would 'p' ever become 0 (null pointer)? Perhaps you meant to
compare the *contents* of the memory to which 'p' points with 0?

Also, consider declaring 'p' where you need it, not up ahead, waaaay
before ever using it.
14 cout << *p << endl;
15
16 return 0;
17 }

Please avoid posting line numbers in the future, they are in the way.
When your code is more complicated, somebody (like I) will want to
copy-and-paste your code and compile it and run it, but then they'd need
to clean out all those extraneous digits...

V
 
M

mzdude

Hi everyone. I'm a newbie in c++ and I have a question related to the
an exercise in Stroustrup's book, namely
5.9/8. It's about iterations in a given array using pointers vs using
indexes. The indexes one I managed to write, but when I try to execute
the pointer variant (it compiles ok on BSD and Linux also) , I get the
string I wanted to see plus lots of gibberish and a segfault. What's
wrong? Here's the code :

  1 #include <iostream>
  2
  3 using namespace std;
  4
  5
  6
  7 int main()
  8 {
  9         int i;
 10         char *p;
 11         char v[] = "Testing...";
 12
 13         for(p = v; p != 0; p++)

Your second statement in the for loop p != 0 is testing
if the pointer is 0. What you want to test is if the
contents of what p points to is 0 (end of string).

try *p != 0
 
J

jeff_hann

Hi everyone. I'm a newbie in c++ and I have a question related to the
an exercise in Stroustrup's book, namely
5.9/8. It's about iterations in a given array using pointers vs using
indexes. The indexes one I managed to write, but when I try to execute
the pointer variant (it compiles ok on BSD and Linux also) , I get the
string I wanted to see plus lots of gibberish and a segfault. What's
wrong? Here's the code :
  1 #include <iostream>
  2
  3 using namespace std;
  4
  5
  6
  7 int main()
  8 {
  9         int i;
 10         char *p;
 11         char v[] = "Testing...";
 12
 13         for(p = v; p != 0; p++)

Your second statement in the for loop p != 0 is testing
if the pointer is 0. What you want to test is if the
contents of what p points to is 0 (end of string).

try  *p != 0

Thank you so much everybody. Changing that part in the for loop made
it work. :)
Also sorry for the line numbers.
 

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,768
Messages
2,569,574
Members
45,050
Latest member
AngelS122

Latest Threads

Top