![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: Feb 2006
Posts: 5
Rep Power: 0
![]() |
how to use pointers to display a part of a string
hi guys,
thanks for posting on my last thread. i was wondering if pointers could be used to display part of a string instead of arrays. so i tried the following program. but it is not working. #include<iostream.h>
int main()
{
char tu[]={"hello world"};
char *p;
char se[20];
int i;
for(i=0;i=5;i++)
se[i]=*(p+i)
cout<<se;
return 0;
} |
|
|
|
|
|
#2 |
|
The Supreme Ruler
![]() Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6
![]() |
*Edited to correct code tags*
__________________
"Every gun that is made, every warship launched, every rocket signifies, in the final sense, a theft from those who hunger and are not fed, from those who are cold and are not clothed. The world in arms is not spending money alone. It is spending the sweat of its laborers, the genius of its scientists, the hopes of its children." - Dwight D. Eisenhower |
|
|
|
|
|
#3 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
(The trailing code tag requires a slash, [/code]. Thanks for using them, though.) A character string (ala C) IS an array. Exactly and precisely. It has one additional requirement -- a 'null' character to mark the end of useful characters.
I think maybe you'd like to examine what this means: for (i = 0; i = 5; i++);
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#4 |
|
Programming Guru
![]() Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5
![]() |
Additionally, you should include <iostream>, <iostream.h> is deprecated.
@David: Does the compiler set i to 5 in the for loop, or does it only compare even though one types the = operator instead of == operator?
__________________
"Employ your time in improving yourself by other men's writings, so that you shall gain easily what others have labored hard for." -- Socrates Last edited by nnxion; Feb 13th, 2006 at 9:25 AM. |
|
|
|
|
|
#5 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
<me>Dang me, take a new rope and hang me, didn't even pick up on the single '='</me> In that case, the loop will run (i = 5 is true), i will be incremented, set back to 5, ad nauseum. se [i] will get a workout and p+i will definitely have the jitters like a wino on joy juice.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#6 |
|
Programmer
Join Date: Apr 2005
Posts: 96
Rep Power: 4
![]() |
I dont use pointers much but it seems to me it wouldnt work because he didnt set p to point to anything
__________________
I had my portable CD player, and took it in the bathroom with me while I went to pee. And the second I whipped my penis out, the theme song to 'Rocky' started playing. I've never felt more manly than in that moment. |
|
|
|
|
|
#7 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
That's true, too. It's hell to be old and blind. I can't even read my own tutorial without a magnifying glass.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
|
|
#8 |
|
Hobbyist Programmer
Join Date: Aug 2005
Posts: 137
Rep Power: 4
![]() |
Another method that will work is to use pointer notation to pass the starting address of the string to cout. For example, tu itself stores the address of the beginning of the string so cout will start printing from that memory address until it reaches the NULL byte. However, you can do pointer arithmetic by adding a specific number to tu and then cout should start printing from that memory address.
For example to print only the word "world" you can use cout << tu + 6 << endl; Here we start printing 6 characters inward starting from the beginning of the string. |
|
|
|
|
|
#9 |
|
Troll
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4
![]() |
@aznluvsmc: Don't forget a little bounds checking when arbitrarily advancing a pointer. In this case, if the string you are working with isn't long enough, you could end up with a pointer past the end of your useful data. Any string manipulation/display function would keep processing garbage until, by chance, it hits a zero.
__________________
MD5(sig) = bcef75433db02e9ad9bf81d6f7c5c270 |
|
|
|
|
|
#10 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
...or references protected memory...
. We need a smiley that shows the system running off into the woods and getting eaten by a b'ar.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code. Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|