473,480 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

more string input confusion

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);
}

May 16 '07 #1
8 1729
In article <11**********************@w5g2000hsg.googlegroups. com>,
merrittr <me******@gmail.comwrote:
>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
May 16 '07 #2
merrittr said:
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...
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).
I have avoided scanf and gets`after reading the c programming FAQ.
Very wise.
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.
May 16 '07 #3
Ah I see , thats great you 2 have been very helpfull here is the push
function for the next person with the problem
void push(char *p)
{
char *ptr;
ptr = (char *)malloc( strlen(p) );
strcpy(ptr, p);
iItem[iSp++]=ptr;
print(*iItem);
}

May 16 '07 #4
merrittr said:
Ah I see , thats great you 2 have been very helpfull here is the push
function for the next person with the problem

Add:

#include <stdlib.h/* prototype for malloc */

and

#include <string.h/* prototype for strcpy */
void push(char *p)
{
char *ptr;
ptr = (char *)malloc( strlen(p) );
Lose the cast. Add 1 to strlen(p) to make room for the null terminator.
And add this check:

if(ptr != NULL)
{
strcpy(ptr, p);
iItem[iSp++]=ptr;
print(*iItem);
}
}
Change the return type from void to int, and document which value you
will return in the case of success, and which in the case of failure.
One common convention is that 0 means success, and non-0 means
something went wrong.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 16 '07 #5
In article <11**********************@y80g2000hsf.googlegroups .com>,
merrittr <me******@gmail.comwrote:
>Ah I see , thats great you 2 have been very helpfull here is the push
function for the next person with the problem
>void push(char *p)
{
char *ptr;
ptr = (char *)malloc( strlen(p) );
strcpy(ptr, p);
iItem[iSp++]=ptr;
print(*iItem);
}
1) You don't need to cast the result of the malloc(), and doing so
can sometimes hide errors

2) strlen() does not include the terminating null character but
strcpy() copies until (and including) null, so you are writing one
more character than you have allocated for

3) you do not check that the return value of the malloc is not NULL
before you write to it.

4) Do you really wish to be always printing the first iItem rather
than the iItem you just pushed?

5) what is print() anyhow? If that's a typo for printf() then be
careful because your stored string might happen to include % format
codes that printf() would try to interpret if the string is passed
as the first parameter.
--
"law -- it's a commodity"
-- Andrew Ryan (The Globe and Mail, 2005/11/26)
May 16 '07 #6
merrittr <me******@gmail.comwrites:
void push(char *p)
p could be made type "const char *".
{
char *ptr;
ptr = (char *)malloc( strlen(p) );
You forgot to add 1 to the return value of strlen here.

I don't recommend casting the return value of malloc():

* The cast is not required in ANSI C.

* Casting its return value can mask a failure to #include
<stdlib.h>, which leads to undefined behavior.

* If you cast to the wrong type by accident, odd failures can
result.

In unusual circumstances it may make sense to cast the return value of
malloc(). P. J. Plauger, for example, has good reasons to want his
code to compile as both C and C++, and C++ requires the cast, as he
explained in article <9s*****************@nwrddc01.gnilink.net>.
However, Plauger's case is rare indeed. Most programmers should write
their code as either C or C++, not in the intersection of the two.
strcpy(ptr, p);
iItem[iSp++]=ptr;
print(*iItem);
It seems odd for a push function to print the item at the bottom
of the stack.
}
--
Comp-sci PhD expected before end of 2007
Seeking industrial or academic position *outside California* in 2008
May 16 '07 #7
Richard Heathfield wrote:
merrittr said:
.... snip ...
>
>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.
Huh? iSp is undefined. As is iItem and print.

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

May 17 '07 #8
CBFalconer said:
Richard Heathfield wrote:
>merrittr said:
... snip ...
>>
>>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.

Huh? iSp is undefined. As is iItem and print.
It is not unreasonable to say so. Nor, however, is it unreasonable to
assume that they are defined in unposted code.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
May 17 '07 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
22991
by: flyaflya | last post by:
a = "(1,2,3)" I want convert a to tuple:(1,2,3),but tuple(a) return ('(', '1', ',', '2', ',', '3', ')') not (1,2,3)
4
2505
by: Vincent Berkeley | last post by:
Is there a way to allocate memory for a character array without knowing in advance what size you need? For instance, is there a way to allocate memory based on the length of an input string after...
8
22100
by: alternativa | last post by:
Hi, how to write a function that would return a string? I've tried something like this: char enterstring(void) { char somestring; printf("\tstring: "); fgets(somestring, sizeof somestring,...
1
8311
by: willie | last post by:
>willie wrote: wrote:
7
19927
by: wenmang | last post by:
Hi all, I have a confusion for representation of hex number: For an unsigned long long number, its max value is: unsigned long long maxNum = 18446744073709551615; if I do: printf("max uint64_t...
232
13060
by: robert maas, see http://tinyurl.com/uh3t | last post by:
I'm working on examples of programming in several languages, all (except PHP) running under CGI so that I can show both the source files and the actually running of the examples online. The first...
3
1477
agsrinivasan
by: agsrinivasan | last post by:
hi everyone... I had done the program to search a string in a text file...Now i want to put tags,,like $$ into infornt of as well as after end of searched string ...
1
3034
by: kellysgirl | last post by:
Now what you are going to see posted here is both the set of instructions I was given..and the code I have written. The instructions I was given are as follows In this case, you will create...
151
7900
by: istillshine | last post by:
There are many languages around: C++, JAVA, PASCAL, and so on. I tried to learn C++ and JAVA, but ended up criticizing them. Is it because C was my first programming language? I like C...
0
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
6944
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.