 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
merrittr Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: need to push pop strings on a stack |
|
|
I need some C advice I want to read in string commands from a user
when the user enters a \n
I want to push it on the stac. Then at some point , if the user enters
the word print pop off and print each word (or using another stack
pointer scan the stack printing each string). here is a stub of what i
want to do. (how do I implement this currently my code doesn't work
due to my lack of strings and pointers)
#include <stdio.h>
#include <ctype.h>
#define CMDBUFFER 10
char iItem[10],*x;
int iSp;
void push(char*);
char pop(void) ;
void main()
{
int iCur,iCharCount=0,i1,i2,bExit=0;
char *cPos,*cTok1=NULL,*cTok2=NULL,*cTok3=NULL;
char cVal,cSwitch;
char cString[CMDBUFFER]=" ";
enum eTokens {Exit=0,Add=1,Subtract=2,Multiply=3,Divide=4};
while(bExit==0)
{
cString[CMDBUFFER]=" ";
printf(":");
cVal=getchar();
while(cVal != '\n')
{
cString[iCharCount++] = tolower(cVal);
cVal=getchar();
}
cString[iCharCount++] ='\0';
iCharCount=0; //reset cString
push(&cString);
*x=pop();
printf ("main %s\n",x);
}
}
void push(char *p)
{
iItem [iSp++]=p;
}
char pop(void)
{
printf("%i\n",iSp);
char *x=iItem [--iSp];
printf ("pop %s\n",x);
return *x;
} |
|
| Back to top |
|
 |
merrittr Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: need to push pop strings on a stack |
|
|
Hi Martin ,
What was the correct form? as I mentioned I am unfamiliar with C and
currently reading a book
(The C refrence from tartan labs printed in 1984 , maybe a bad idea)
On May 12, 2:29 am, Martin Ambuhl <mamb...@earthlink.net> wrote:
| Quote: | merrittr wrote:
void main()
^^^^^
I have lost count of how many of these I have seen in the last couple of
days. Why the sudden deluge of a form which has not been part of C for
hosted implementations for now some 18 years? Has Schildt just
published a new book of errors? |
|
|
| Back to top |
|
 |
Martin Ambuhl Guest
|
Posted: Sat May 12, 2007 9:11 am Post subject: Re: need to push pop strings on a stack |
|
|
merrittr wrote:
I have lost count of how many of these I have seen in the last couple of
days. Why the sudden deluge of a form which has not been part of C for
hosted implementations for now some 18 years? Has Schildt just
published a new book of errors? |
|
| Back to top |
|
 |
Keith Thompson Guest
|
Posted: Sun May 13, 2007 7:12 am Post subject: Re: need to push pop strings on a stack |
|
|
Ian Collins <ian-news (AT) hotmail (DOT) com> writes:
| Quote: | Keith Thompson wrote:
CBFalconer <cbfalconer (AT) yahoo (DOT) com> writes:
[...]
I suggest using K&R II. main returns an int. The valid values are
0, EXIT_OK, and EXIT_FAILURE. The macros are found in <stdlib.h>.
EXIT_SUCCESS, not EXIT_FAILURE.
EXIT_SUCCESS not EXIT_OK.
|
Yes, of course. D'oh!
The (portably) valid values are 0, EXIT_SUCCESS, and EXIT_FAILURE.
--
Keith Thompson (The_Other_Keith) kst-u (AT) mib (DOT) org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister" |
|
| Back to top |
|
 |
CBFalconer Guest
|
Posted: Sun May 13, 2007 8:15 am Post subject: Re: need to push pop strings on a stack |
|
|
merrittr wrote:
| Quote: |
Sounds good I will update my code,
however I am still a little confused on how to push strings onto a
stach and pop them off somewhere in there
I should see a ** to reference a array of pointers to char arrays
shouldn't I?
|
Try this (untested):
typedef struct st_node {
char *s;
struct st_node *next;
} *st_nodep;
static st_nodep root; /* initializes to NULL */
int st_push(const char *s1) {
size_t len;
st_nodep item;
if (!(item = malloc(sizeof item)}) return 0;
else {
if (!s1) len = 0;
else len = 1 + strlen(s1);
if (!(item->s = malloc(len))) {
free(item)
return 0;
}
else {
if (s1) strcpy(s1, item->s)
else {s = NULL;
item->next = root;
root = item;
return 1;
}
char *st_pop(void) {
/* take the top-item at root, return the char*, free the node,
and set the root to be root->next. NULL in root is empty */
}
Notice that the only link to the stack is root. It can grow until
malloc gives up. The system also makes copies of any strings it is
passed. When popped, the popper is responsible for freeing those
strings.
--
<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 |
|
| Back to top |
|
 |
Peter Nilsson Guest
|
Posted: Mon May 14, 2007 5:21 am Post subject: Re: need to push pop strings on a stack |
|
|
"Army1987" <please....@for.it> wrote:
| Quote: | "CBFalconer" <cbfalco...@yahoo.com>...
I suggest using K&R II. main returns an int. The valid
values are 0, EXIT_OK, and EXIT_FAILURE. The macros are
found in <stdlib.h>.
#define EXIT_OK EXIT_SUCCESS
|
Don't do this if you might one day include <errno.h> at
some point.
--
Peter |
|
| Back to top |
|
 |
merrittr Guest
|
Posted: Mon May 14, 2007 6:06 am Post subject: Re: need to push pop strings on a stack |
|
|
Thanks guys
Got it working, and got the K&R 2nd ed. at a used book store so I am
on my way |
|
| Back to top |
|
 |
|
|
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
|
|