C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

more string input confusion

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language
View previous topic :: View next topic  
Author Message
merrittr
Guest





PostPosted: Wed May 16, 2007 9:11 am    Post subject: more string input confusion Reply with quote



I have been having troubles getting "string" input pushed on to the
stack in the assn1. The funny thing is
I can push a literal on no problem so

push("push i 0");
push("push i 1");
push("add");

works fine however

while(cVal != '\n') {
cString[iCharCount++] = tolower(cVal);
cVal=getchar();
}
cString[iCharCount++]='\0';
iCharCount=0;
push(cString);

doesn't work is there a diference between the literal text string and
the built string (char array null terminated?).
I have avoided scanf and gets`after reading the c programming FAQ.



here is the push code:
void push(char *p)
{
int ptr=iSp;
iItem[iSp++]=p;
print(*iItem);
}
Back to top
Walter Roberson
Guest





PostPosted: Wed May 16, 2007 9:11 am    Post subject: Re: more string input confusion Reply with quote



In article <1179301777.851351.141960 (AT) w5g2000hsg (DOT) googlegroups.com>,
merrittr <merrittr (AT) gmail (DOT) com> wrote:
Quote:
I have been having troubles getting "string" input pushed on to the
stack in the assn1. The funny thing is
I can push a literal on no problem so

push("push i 0");
push("push i 1");
push("add");

works fine however

while(cVal != '\n') {
cString[iCharCount++] = tolower(cVal);
cVal=getchar();
}
cString[iCharCount++]='\0';
iCharCount=0;
push(cString);

doesn't work is there a diference between the literal text string and
the built string (char array null terminated?).

here is the push code:
void push(char *p)
{
int ptr=iSp;
iItem[iSp++]=p;
print(*iItem);
}

Your iItem[iSp++]=p code in push() copies the pointer, but probably
on your next loop around you overwrite the same memory area cString
again and push a copy of that same pointer. String literals, though,
have persistant and unique addresses, so the recorded pointer for
those is going to continue to point to the literal content, not overwritten
by the next trip through the loop. If you were to push the -same-
string literal again, and were to examine iItem[], you would find that
you had two copies of the same literal pointer.

What you probably need to do in your push() routine is
find the strlen() of the string passed in, malloc() enough space
to hold the string *and it's terminating null*, copy the
string into that malloc'd memory, and record the pointer to the
malloc'd memory; when you went to pop() the value, you would then need
to free() the pointer after you had finished using it.
--
"No one has the right to destroy another person's belief by
demanding empirical evidence." -- Ann Landers
Back to top
Richard Heathfield
Guest





PostPosted: Wed May 16, 2007 9:12 am    Post subject: Re: more string input confusion Reply with quote



merrittr said:

Quote:
I have been having troubles getting "string" input pushed on to the
stack in the assn1. The funny thing is
I can push a literal on no problem so

push("push i 0");
push("push i 1");
push("add");

works fine however

while(cVal != '\n') {
cString[iCharCount++] = tolower(cVal);
cVal=getchar();
}
cString[iCharCount++]='\0';
iCharCount=0;
push(cString);

doesn't work

You haven't shown enough code for us to tell precisely what is going
wrong, but...

Quote:
is there a diference between the literal text string and
the built string (char array null terminated?).

....string literals are indeed strings - i.e. null terminated arrays of
char, just like what you call "built" strings (provided you build them
properly, of course).

Quote:
I have avoided scanf and gets`after reading the c programming FAQ.

Very wise.

Quote:
here is the push code:
void push(char *p)
{
int ptr=iSp;
iItem[iSp++]=p;
print(*iItem);
}

Aha! Tell me... is the symptom that you get the same thing printed over
and over again? I can see two aspects of your code that might both,
separately or together, cause that problem.

Note that iItem[iSp++]=p; does not create a fresh copy of the string. It
merely points a pointer in the direction of the string. So if you give
this function a bunch of string literals, it'll work fine, but every
time you pass in your own "built" string as you call it, what you're
really doing is telling your stack where that array is. The fact that
you change the array's contents from time to time is of no concern to
iItem[]. It will continue to point at that array, quite happily, and
several times over if you tell it too.

Note also that iItem[iSp++]=p suggests that iItem[iSp++] is a char *,
and therefore *iItem is also a char *. This suggests that print() will
only ever print the first item in the array!

The solution to the first problem is to arrange some storage, and copy
the data into it rather than just tweak pointers. The solution to the
second is probably to populate iItem[iSp], then print(iItem[iSp]), and
then increment iSp.

Incidentally, one day you *will* get tired of type prefixes. Why not
drop them now? That way, you won't have such a big clean-up job later
on.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C Language All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.