C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 28th, 2004, 09:23 AM
whitedragon007 whitedragon007 is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2004
Posts: 1 whitedragon007 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Exclamation Help with C++ Pointer

Hi, I'm trying to print "Spiderman is amazing" on both lines, but on the second line, it is printing out some weird symbols before the phrase. Could someone please help correct my mistake .
Thanks.

Code:
#include <iostream.h>
main()
{
char mynameis[]="Spiderman is amazing";
char string[25];
char *ptr=mynameis;
char *p=string;
for(int i=0; mynameis[i] !='\0'; i++)
*p = *ptr;
cout<<ptr<<'\n';
cout<<p<<'\n';

return 0;
}

Reply With Quote
  #2  
Old June 28th, 2004, 09:34 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Ok first off you need to null terminate your string -

char mynameis[] = "Spiderman is amazing\0";

This lets the program know where the string ends.

If you change the for loop execution line to string [i] = mynameis[i]; then you should get the desired result.

Having made these observations though this code is fairly bug ridden (no offence intended but getting your head around c/c++ strings is a nightmare and the sooner someone gets you to read LOTS of tutorials the better) so I suggest you find some decent tutorials around the net and read up on how strings, pointers and arrays work in c/c++ and hopefully you'll have more luck with code like this.

-KM-

Reply With Quote
  #3  
Old June 29th, 2004, 10:24 AM
_julien_ _julien_ is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: May 2004
Posts: 11 _julien_ User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Quote:
Originally Posted by kode_monkey
Ok first off you need to null terminate your string -
char mynameis[] = "Spiderman is amazing\0";
This lets the program know where the string ends.

It is done allready. You do not need to add another 0.

Quote:
Originally Posted by kode_monkey
If you change the for loop execution line to string [i] = mynameis[i]; then you should get the desired result.

you also have to copy the 0.

--
julien barbier

Reply With Quote
  #4  
Old July 1st, 2004, 09:11 PM
Monkey Junk Monkey Junk is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 5 Monkey Junk User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
When you initialize a char array with anything in double quotes. "Spider man";

A null terminator is automatically placed at the end. That is why you must make your array one element larger to allow room for the null terminator. If you chose to you place your initialization characters in single quotes, which you can do, you must add the null terminator. '\0'.

Why don't you use the new ANSI standard header file <string>. Instead of char as your data type you can replace it with string and then enter you string.

If I am wrong, someone punch me in the throat.... No just kidding...[img]images/icons/icon10.gif[/img]

Reply With Quote
  #5  
Old July 2nd, 2004, 05:22 AM
kode_monkey kode_monkey is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Posts: 367 kode_monkey User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 7 m 21 sec
Reputation Power: 6
Thats right but I presume the purpose of the program is to learn how that method works rather than anything else, since spiderman is indeed amazing but a program that prints this twice isn't exactly rocket science, so using string instead wouldn't help here. (Like it does everywhere else, please use it )

-KM-

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with C++ Pointer


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
Stay green...Green IT