Forums

Skip to content

Advanced search
  • Quick links
    • Unanswered topics
    • Active topics
    • Search
  • FAQ
  • Login
  • Register
  • Board index Assistance Portage & Programming
  • Search

Simple stack in C

Problems with emerge or ebuilds? Have a basic programming question about C, PHP, Perl, BASH or something else?
Post Reply
Advanced search
6 posts • Page 1 of 1
Author
Message
paul555
l33t
l33t
User avatar
Posts: 612
Joined: Mon Nov 22, 2004 11:35 pm
Location: Greece
Contact:
Contact paul555
Website

Simple stack in C

  • Quote

Post by paul555 » Tue Nov 28, 2006 2:10 pm

Hi all i want to make a program that for a simple stack (an integer array) i will insert elements (integers) in the stack until it will be full or remove them until it will be empty.Here is my code:

Code: Select all

#define N 2
//int stack[N];
//int head=0;

#include<stdio.h>
void push(int [],int,int,int);
int pop(int *,int,int);


int main()
{
	int s,k,stack[N],head=0;

	do
	{
		printf("Give your selection\n");
		printf("1.Push\n");
		printf("2.Pop\n");
		printf("3.Finish\n");
		scanf("%d",&s);
		switch(s)
		{
			case 1:
			{
				printf("give an element to push in the stack:\n");
				scanf("%d",&k);
				push(stack,N,head,k);
				break;
			}
			case 2:
			{
				pop(stack,N,head);
				//printf("%d\n",pop());
				break;
			}
		}
	}
	while(s!=3);

}

void push(int mstack,int N,int head,int k)
{
	if (head<N)
		mstack[head++]=e;
	else
		printf("STACK IS FULL\n");
}


int pop(int mstack,int N,int head)
{
	if(head>0)
		return mstack[--head];
	else
		printf("STACK IS EMPTY\n");
}
Until now i can't get it to work.I get an error that is expected ';' , ',' or ')' before numeric constant in lines 63 and 72.Any ideas?
"LINUX, MS-DOS, Windows : known as the Good, the Bad and the Ugly." :)
http://www.gnome.gr
Top
anthrax_
Apprentice
Apprentice
User avatar
Posts: 168
Joined: Tue Oct 17, 2006 3:43 pm
Location: Poland

  • Quote

Post by anthrax_ » Tue Nov 28, 2006 2:15 pm

#define N 2
//int stack[N];
//int head=0;

#include<stdio.h>
void push(int [],int,int,int);
int pop(int *,int,int);


int main()
{
int s,k,stack[N],head=0;

do
{
printf("Give your selection\n");
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Finish\n");
scanf("%d",&s);
switch(s)
{
case 1:
{
printf("give an element to push in the stack:\n");
scanf("%d",&k);
push(stack,N,head,k);
break;
}
case 2:
{
pop(stack,N,head);
//printf("%d\n",pop());
break;
}
}
}
while(s!=3);

}

void push(int mstack,int N,int head,int k)
{
if (head<N)
mstack[head++]=e;
else
printf("STACK IS FULL\n");
}


int pop(int mstack,int N,int head)
{
if(head>0)
return mstack[--head];
else
printf("STACK IS EMPTY\n");
}
There are only 56 lines :|
Top
IvanYosifov
l33t
l33t
User avatar
Posts: 778
Joined: Fri Oct 15, 2004 6:33 pm
Location: Bulgaria

  • Quote

Post by IvanYosifov » Tue Nov 28, 2006 2:18 pm

Code: Select all

 #define N 2 
....
 void push(int mstack,int N,int head,int k) 
....
The N in the function definition gets substituted with 2, so you end up with

Code: Select all

 void push(int mstack,int 2,int head,int k) 
which is nonsense.
Top
batistuta
Veteran
Veteran
User avatar
Posts: 1384
Joined: Fri Jul 29, 2005 3:54 pm
Location: Aachen

  • Quote

Post by batistuta » Tue Nov 28, 2006 2:21 pm

your function headers have a "int N". In here, the N will be replaced by your macro. Pick for your macro name something more meaningfull
Top
ttuttle
Tux's lil' helper
Tux's lil' helper
Posts: 131
Joined: Sun Oct 03, 2004 8:44 pm
Contact:
Contact ttuttle
Website

  • Quote

Post by ttuttle » Tue Nov 28, 2006 8:03 pm

Another problem you are going to run in to... the prototypes for push and pop both say they take an array of integers or a pointer to an integer as a first argument, but the actual definitions of the functions don't. You should settle on whether they will take arrays or pointers as parameters, and then fix the definitions so they match the prototypes.
Visit my website.
Top
bluepass
n00b
n00b
User avatar
Posts: 41
Joined: Thu Nov 23, 2006 4:47 am
Location: /dev/null

  • Quote

Post by bluepass » Tue Nov 28, 2006 9:22 pm

The problem is that you have a constant in the parameter list. I don't see why you included "int N" in there. It is a constant and it will exist throughout the entire run of the program, so you don't have to pass it to the function as an argument -- either way that wouldn't be the way you'd want to do it. You can think of it this way: when you have your lines with the #define statements the compiler goes over the entire code and replaces the constant name with the value it represents before compiling. You would never have to pass this constant to a function you're using.

Also you've had a few problems with the types you use for your code. stack is an array, so when you create the functions push and pop you must take in a pointer to the array with your current stack -- in other words in your parameter list you either take in mstack[] either *mstack; they're the same thing.

There've been a whole bunch of compilation errors I had to correct and to be honest I don't remember them all. Review your code first man, correct all the compilation errors that you can and then send the code again and I'll have a look at it. Also try to clean it up because it's very messy and for a code that simple it's a bad thing. (Yes, I've seen the Linux kernel source code, and yes I've seen some very messy code in my life, but they're usually the codes that require a lot of hacking and playing with -- your code is relatively simple.)
Top
Post Reply

6 posts • Page 1 of 1

Return to “Portage & Programming”

Jump to
  • Assistance
  • ↳   News & Announcements
  • ↳   Frequently Asked Questions
  • ↳   Installing Gentoo
  • ↳   Multimedia
  • ↳   Desktop Environments
  • ↳   Networking & Security
  • ↳   Kernel & Hardware
  • ↳   Portage & Programming
  • ↳   Gamers & Players
  • ↳   Other Things Gentoo
  • ↳   Unsupported Software
  • Discussion & Documentation
  • ↳   Documentation, Tips & Tricks
  • ↳   Gentoo Chat
  • ↳   Gentoo Forums Feedback
  • ↳   Duplicate Threads
  • International Gentoo Users
  • ↳   中文 (Chinese)
  • ↳   Dutch
  • ↳   Finnish
  • ↳   French
  • ↳   Deutsches Forum (German)
  • ↳   Diskussionsforum
  • ↳   Deutsche Dokumentation
  • ↳   Greek
  • ↳   Forum italiano (Italian)
  • ↳   Forum di discussione italiano
  • ↳   Risorse italiane (documentazione e tools)
  • ↳   Polskie forum (Polish)
  • ↳   Instalacja i sprzęt
  • ↳   Polish OTW
  • ↳   Portuguese
  • ↳   Documentação, Ferramentas e Dicas
  • ↳   Russian
  • ↳   Scandinavian
  • ↳   Spanish
  • ↳   Other Languages
  • Architectures & Platforms
  • ↳   Gentoo on ARM
  • ↳   Gentoo on PPC
  • ↳   Gentoo on Sparc
  • ↳   Gentoo on Alternative Architectures
  • ↳   Gentoo on AMD64
  • ↳   Gentoo for Mac OS X (Portage for Mac OS X)
  • Board index
  • All times are UTC
  • Delete cookies

© 2001–2026 Gentoo Foundation, Inc.

Powered by phpBB® Forum Software © phpBB Limited

Privacy Policy

 

 

magic