Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > How to literate a const char*

Reply
Thread Tools

How to literate a const char*

 
 
silverburgh.meryl@gmail.com
Guest
Posts: n/a
 
      07-14-2007
Hi,

how can I literator a const char*?

I have this code, but it results in an infinite loop. It never exits
the while (p1 != '\0') loop

void teststr( const char *str1) {
char* p1 = (char*)str1;


while (p1 != '\0') {
//....
p1 ++;
}

}

and i call teststr like this:

teststr("http://www.cnn.com");

Can you please tell me why i never exit the while loop?

Thank you.

 
Reply With Quote
 
 
 
 
Ben Pfaff
Guest
Posts: n/a
 
      07-14-2007
"(E-Mail Removed)" <(E-Mail Removed)> writes:

> I have this code, but it results in an infinite loop. It never exits
> the while (p1 != '\0') loop
>
> void teststr( const char *str1) {
> char* p1 = (char*)str1;


This is questionable. If you don't need to modify str1, then
declare p1 as const and drop the cast. If you do need to modify
str1, then don't declare str1 as const and drop the cast.

> while (p1 != '\0') {


Should be
while (*p1 != '\0')
Note the *.

> //....
> p1 ++;
> }
>
> }


--
"Am I missing something?"
--Dan Pop
 
Reply With Quote
 
 
 
 
Christopher Benson-Manica
Guest
Posts: n/a
 
      07-15-2007
Ben Pfaff <(E-Mail Removed)> wrote:

> "(E-Mail Removed)" <(E-Mail Removed)> writes:


> > void teststr( const char *str1) {
> > char* p1 = (char*)str1;


> This is questionable. If you don't need to modify str1, then
> declare p1 as const and drop the cast. If you do need to modify
> str1, then don't declare str1 as const and drop the cast.


Note that OP passed a string literal to this function, so it seems
that the former is the right advice.

--
C. Benson Manica | I *should* know what I'm talking about - if I
cbmanica(at)gmail.com | don't, I need to know. Flames welcome.
 
Reply With Quote
 
 
 
Reply

Thread Tools

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
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Literate Programming Hans Georg Schaathun Python 10 04-11-2011 04:39 PM
Literate programs in Python Paul Miller Python 6 05-14-2008 06:04 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
XMLMind and Literate Programming Mike Maxwell XML 2 11-03-2004 02:13 PM



Advertisments