By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,665 Members | 1,900 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,665 IT Pros & Developers. It's quick & easy.

need to push pop strings on a stack

P: n/a
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;
}

May 12 '07 #1
Share this Question
Share on Google+
20 Replies


P: n/a
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?
May 12 '07 #2

P: n/a
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.netwrote:
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?

May 12 '07 #3

P: n/a
Martin Ambuhl said:
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?
It has, TTBOMKAB, *never* been a part of C.
Has Schildt just published a new book of errors?
That depends. Has he just published a new book? If the answer to my
question is "yes", then it is probably the answer to yours, too.

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

P: n/a
merrittr said:
Hi Martin ,

What was
is
the correct form?
The correct forms are:

int main(int argc, char **argv)

int main(void)

and any semantic equivalent.
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)
There's no "maybe" about it.

Get a better book. I recommend "The C Programming Language", 2nd
edition, by Kernighan and Ritchie. (Since Dennis Ritchie devised the
language, you can be sure that he has a fair idea of how it goes.)

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

P: n/a
Martin Ambuhl <ma*****@earthlink.netwrites:
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?
void main() has *never* been part of C for hosted implementations.
The C89 standard both introduced the void keyword and required main to
return int.

--
Keith Thompson (The_Other_Keith) ks***@mib.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"
May 12 '07 #6

P: n/a
merrittr <me******@gmail.comwrites:
On May 12, 2:29 am, Martin Ambuhl <mamb...@earthlink.netwrote:
>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?

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)
Please don't top-post. Your response should go after, or interspersed
with, any quoted text. See almost all the followups in this newsgroup
for examples.

A C reference from 1984 is obsolete. The version of the C standard
that's most widely supported was introduced in 1989; the official
current standard was introduced in 1999.

The comp.lang.c FAQ is at <http://www.c-faq.com/>. Section 11 covers
the correct declaration of main().

The best book on C is Kernighan & Ritchie's _The C Programming
Language_, 2nd Edition, commonly referred to as K&R2. Harbison &
Steele's _C: A Reference Manual_, 5th Edition, is a very good
reference. The latest draft of the standard is
<http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf>.

--
Keith Thompson (The_Other_Keith) ks***@mib.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"
May 12 '07 #7

P: n/a

"merrittr" <me******@gmail.comha scritto nel messaggio
news:11**********************@k79g2000hse.googlegr oups.com...
>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()
main returns an int.
{

int iCur,iCharCount=0,i1,i2,bExit=0;
Know what? You are free to put whitespaces after these commas, the
compiler will ignore them anyway.
char *cPos,*cTok1=NULL,*cTok2=NULL,*cTok3=NULL;
char cVal,cSwitch;
char cString[CMDBUFFER]=" ";
You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER.
E.g.
char cString[CMDBUFFER];
memset(cString, ' ', CMDBUFFER);
(You need to #include <string.hto do that.)
enum eTokens {Exit=0,Add=1,Subtract=2,Multiply=3,Divide=4};

while(bExit==0)
{
cString[CMDBUFFER]=" ";
What you are doing here is converting the address of the string
literal " " itself to a char and writing it immediately
past the end of cString. If your compiler didn't complain, raise
the diagnostics level. Assignment doesn't work like initialization.
printf(":");
Use fflush(stdout), or else that character won't show until the
next newline. BTW, to print single characters you can also use
putchar(':');
cVal=getchar();
while(cVal != '\n')
If it hits EOF, the loop is infinite.
{
cString[iCharCount++] = tolower(cVal);
What if I write more than CMDBUFFER characters on the same line?
You'll write past the end of cString, causing undefined behaviour.
cVal=getchar();
}
cString[iCharCount++] ='\0';
Be sure not to write past the end of cString.
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;
}
I suggest you to use better indentation.
May 12 '07 #8

P: n/a
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?

May 12 '07 #9

P: n/a
On May 12, 3:23 am, "Army1987" <please....@for.itwrote:
"merrittr" <merri...@gmail.comha scritto nel messaggionews:11**********************@k79g2000hse .googlegroups.com...
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()

main returns an int.{
int iCur,iCharCount=0,i1,i2,bExit=0;

Know what? You are free to put whitespaces after these commas, the
compiler will ignore them anyway. char *cPos,*cTok1=NULL,*cTok2=NULL,*cTok3=NULL;
char cVal,cSwitch;
char cString[CMDBUFFER]=" ";

You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER.
E.g.
char cString[CMDBUFFER];
memset(cString, ' ', CMDBUFFER);
(You need to #include <string.hto do that.)
Can you elaborate a bit more on
"You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER."

May 12 '07 #10

P: n/a
On May 12, 3:23 am, "Army1987" <please....@for.itwrote:
"merrittr" <merri...@gmail.comha scritto nel messaggionews:11**********************@k79g2000hse .googlegroups.com...
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()

main returns an int.{
int iCur,iCharCount=0,i1,i2,bExit=0;

Know what? You are free to put whitespaces after these commas, the
compiler will ignore them anyway. char *cPos,*cTok1=NULL,*cTok2=NULL,*cTok3=NULL;
char cVal,cSwitch;
char cString[CMDBUFFER]=" ";

You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER.
E.g.
char cString[CMDBUFFER];
memset(cString, ' ', CMDBUFFER);
(You need to #include <string.hto do that.)
Can you elaborate on

"You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER."
May 12 '07 #11

P: n/a
merrittr wrote: *** and top-posted - fixed ***
Martin Ambuhl <mamb...@earthlink.netwrote:
>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?

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)
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>.

Please do not top-post. Your answer belongs after (or intermixed
with) the quoted material to which you reply, after snipping all
irrelevant material. See the following links:

--
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/ (taming google)
<http://members.fortunecity.com/nnqweb/ (newusers)

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

May 12 '07 #12

P: n/a
CBFalconer <cb********@yahoo.comwrites:
merrittr wrote: *** and top-posted - fixed ***
>Martin Ambuhl <mamb...@earthlink.netwrote:
>>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?

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)

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.

Other values may be valid, but are implementation-specific and not
portable.

--
Keith Thompson (The_Other_Keith) ks***@mib.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"
May 12 '07 #13

P: n/a
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>merrittr wrote: *** and top-posted - fixed ***
>>Martin Ambuhl <mamb...@earthlink.netwrote:
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?
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)
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.

--
Ian Collins.
May 12 '07 #14

P: n/a

"Chad" <cd*****@gmail.comha scritto nel messaggio
news:11**********************@k79g2000hse.googlegr oups.com...
On May 12, 3:23 am, "Army1987" <please....@for.itwrote:
>"merrittr" <merri...@gmail.comha scritto nel
messaggionews:11**********************@k79g2000hs e.googlegroups.com...
char cString[CMDBUFFER]=" ";

You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER.
E.g.
char cString[CMDBUFFER];
memset(cString, ' ', CMDBUFFER);
(You need to #include <string.hto do that.)

Can you elaborate a bit more on
"You could use memset() to write to all these, so you won't need to
change that initialization when you change CMDBUFFER."
By using a macro rather than directly writing cString[10], it
sounds like you might want to increase (or more generally, change)
the value of CMDBUFFER in another version of the program, or at
least that you want to avoid "magic numbers" showing in the source.
memset(cString, ' ', CMDBUFFER) sets the first CMDBUFFER bytes of
cString to ' '. This way, it will be automatically updated when the
value of CMDBUFFER is changed, and it's clearer. One doesn't have
to actually count the characters in " " to know wheter
that array is a string or not (which is an especially bad thing
when the name is misleading; if I haven't forgotten how to count
to ten, cString isn't a C string because there's no room for the
terminating null).
May 12 '07 #15

P: n/a

"CBFalconer" <cb********@yahoo.comha scritto nel messaggio
news:46***************@yahoo.com...
merrittr wrote: *** and top-posted - fixed ***
>Martin Ambuhl <mamb...@earthlink.netwrote:
>>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?

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)

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
May 12 '07 #16

P: n/a
Ian Collins <ia******@hotmail.comwrites:
Keith Thompson wrote:
>CBFalconer <cb********@yahoo.comwrites:
[...]
>>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) ks***@mib.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"
May 13 '07 #17

P: n/a
merrittr wrote:
>
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

May 13 '07 #18

P: n/a
Keith Thompson wrote:
CBFalconer <cb********@yahoo.comwrites:
>merrittr wrote: *** and top-posted - fixed ***
>>Martin Ambuhl <mamb...@earthlink.netwrote:
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?

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)

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.

Other values may be valid, but are implementation-specific and not
portable.
Thanks for the correction. I vaguely thought something was wrong
when I wrote it.

--
<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 13 '07 #19

P: n/a
"Army1987" <please....@for.itwrote:
"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.hat
some point.

--
Peter

May 14 '07 #20

P: n/a

Thanks guys

Got it working, and got the K&R 2nd ed. at a used book store so I am
on my way

May 14 '07 #21

This discussion thread is closed

Replies have been disabled for this discussion.