C Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesC Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 19th, 2003, 11:45 AM
ahdai ahdai is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Posts: 1 ahdai User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Pointer

#define MAXLEN 50
char *pMsgString = {"HDLC defines three different data.......................................... "};
void split(char *pMessage)
{
int slen, leng;

char *pE, *pF;

slen = strlen(pMessage) ;
pE = pMessage;
do
{
pF = pE;
leng = slen > MAXLEN ? MAXLEN : slen ;

pE = pF + leng;

while (*pE != ' ')
{
if (*pE == 0)
break ;
pE--;
leng-- ;
}

leng==pE-pF ;

printf("%*.*s\n", leng, leng, pF) ;
getch();

display(pF);
slen -= leng;
} while(slen>0);

}
this function is suppose to split my msg into 50 characters by 50 chars and display it. But i wish to store the segmented message into another pointer rather than directly displaying it. is there a way to do it?
thanx for helping

Reply With Quote
  #2  
Old March 20th, 2003, 03:06 AM
7stud 7stud is offline
Contributing User
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Feb 2001
Posts: 1,365 7stud User rank is Private First Class (20 - 50 Reputation Level)7stud User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 18 h 9 m 25 sec
Reputation Power: 14
You can create a pointer with new and for the char array size use the length of the char pointer you want to copy + 1. Then, you can use strcpy() to copy one char pointer to the other:

pnew_string = new char[strlen(pstring) + 1];
strcpy(pnew_string, pstring);

pstring must contain a c-style string, i.e. one that's terminated with \0. (include <cstring>)

If your pointer doesn't contain a c-style string, you can use a for-loop to copy one character at a time using pointer notation:

pnew_string= new char[pstring_length];
for(int i=0;i<pstring_length; i++)
{
*(pnew_string + i) = *(pstring + i);
}

Last edited by 7stud : March 20th, 2003 at 05:22 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesC Programming > Pointer

Developer Shed Advertisers and Affiliates



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 | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap