Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Feb 13th, 2006, 6:42 AM   #1
rahulsr123
Newbie
 
rahulsr123's Avatar
 
Join Date: Feb 2006
Posts: 5
Rep Power: 0 rahulsr123 is on a distinguished road
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;
}
rahulsr123 is offline   Reply With Quote
Old Feb 13th, 2006, 6:50 AM   #2
Mjordan2nd
The Supreme Ruler
 
Join Date: May 2004
Location: Houston
Posts: 1,476
Rep Power: 6 Mjordan2nd is on a distinguished road
*Edited to correct code tags*
__________________
&quot;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.&quot; - Dwight D. Eisenhower
Mjordan2nd is offline   Reply With Quote
Old Feb 13th, 2006, 6:51 AM   #3
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
(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++);
You are setting i to zero. You are specifying that the loop will execute only when i is 5. Hard to see that working. I would suggest you put braces around the body of your loops and such, even though, as a single line, they're not required. A novice will get into the bad habit of leaving them out where they're needed.
__________________
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
DaWei is offline   Reply With Quote
Old Feb 13th, 2006, 9:08 AM   #4
nnxion
Programming Guru
 
nnxion's Avatar
 
Join Date: Jun 2005
Location: elemental plane
Posts: 1,429
Rep Power: 5 nnxion is on a distinguished road
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.
nnxion is offline   Reply With Quote
Old Feb 13th, 2006, 9:27 AM   #5
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
<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
DaWei is offline   Reply With Quote
Old Feb 13th, 2006, 9:30 AM   #6
crazykid48x
Programmer
 
crazykid48x's Avatar
 
Join Date: Apr 2005
Posts: 96
Rep Power: 4 crazykid48x is on a distinguished road
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.
crazykid48x is offline   Reply With Quote
Old Feb 13th, 2006, 9:33 AM   #7
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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
DaWei is offline   Reply With Quote
Old Feb 14th, 2006, 9:34 PM   #8
aznluvsmc
Hobbyist Programmer
 
Join Date: Aug 2005
Posts: 137
Rep Power: 4 aznluvsmc is on a distinguished road
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.
aznluvsmc is offline   Reply With Quote
Old Feb 14th, 2006, 9:54 PM   #9
Dameon
Troll
 
Dameon's Avatar
 
Join Date: Apr 2005
Location: Texas
Posts: 732
Rep Power: 4 Dameon is on a distinguished road
@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
Dameon is offline   Reply With Quote
Old Feb 15th, 2006, 6:39 AM   #10
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
...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
DaWei is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 11:47 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC