need to push pop strings on a stack

M

merrittr

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

Martin Ambuhl

merrittr said:
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?
 
M

merrittr

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

Richard Heathfield

Martin Ambuhl said:
^^^^^
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.
 
R

Richard Heathfield

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

Keith Thompson

Martin Ambuhl said:
^^^^^
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.
 
K

Keith Thompson

merrittr said:
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>.
 
A

Army1987

merrittr said:
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 said:
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.
 
M

merrittr

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?
 
C

Chad

merrittr said:
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.h> to 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."
 
C

Chad

merrittr said:
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.h> to 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."
 
C

CBFalconer

merrittr wrote: *** and top-posted - fixed ***
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)
 
K

Keith Thompson

CBFalconer said:
merrittr wrote: *** and top-posted - fixed ***

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

Army1987

Chad said:
merrittr said:
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.h> to 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).
 
A

Army1987

CBFalconer said:
merrittr wrote: *** and top-posted - fixed ***

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
 
M

merrittr

Thanks guys

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top