Thread: Testing some code, lots of errors...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    98

    Testing some code, lots of errors...

    Problem is probably obvious, it's late and I'm having trouble finding out why I'm getting some errors on this code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    
    double pop (double stack, int *ptr);
    void push (double stack, int *ptr, double x);
    
    void main (void)
    {
    	int *ptr;
    	int stackSize=0;
    	ptr=&stackSize;
    	double stack[4]={1.0, 2.0, 3.0};
    
    	push(stack, ptr, 9.0);
    	printf("%lf\n", pop(stack, ptr));
    	push(stack, ptr, 8.0);
    	push(stack, ptr, 7.0);
    	push(stack, ptr, 6.0);
    	printf("%lf\n", pop(stack, ptr));
    	printf("%lf\n", pop(stack, ptr));
    	printf("%lf\n", pop(stack, ptr));
    }
    
    double pop (double stack, int *ptr)
    {
    	double value=0;
    	value=stack[*ptr];
    	--*ptr;
    	return value;
    }
    
    void push (double stack, int *ptr, double x)
    {
    	stack[*ptr]=x;
    	++*ptr;
    }
    I'm most worried about
    error C2143: syntax error : missing ';' before 'type'
    on line
    Code:
    double stack[4]={1.0, 2.0, 3.0};
    and the fact that I get
    error C2065: 'stack' : undeclared identifier
    everywhere I used stack, when it is clearly identified...

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    you are passing an array to both of your push and pop function(as 1st argument) but wrongly defined them as double in their function prototypes as well as definition.
    Also main returns int.
    However the error you are getting seems to be weird though.
    Edit:
    Which compiler are you using?
    I think the error might be because the declarations should be made first.
    try moving your
    Code:
    ptr=&stackSize;
    below and see if the error goes.
    Last edited by stevesmithx; 12-15-2008 at 01:02 AM.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Prior to the 1999 edition of the C standard, declarations within a block must always come at the start of a block. As such, this is incorrect, and my guess is that it is the reason for the error that you are most worried about:
    Code:
    ptr=&stackSize;
    double stack[4]={1.0, 2.0, 3.0};
    Considering that "ptr" is not a particularly descriptive name, I think that you should just drop it and write:
    Code:
    int stackSize = 0;
    double stack[4] = {1.0, 2.0, 3.0};
    
    push(stack, &stackSize, 9.0);
    /* ... */
    There are other problems as well. For example, your pop and push functions take a double as the first argument, but you probably want a pointer to a double instead.

    Oh, and change void main to int main.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    Prior to the 1999 edition of the C standard, declarations within a block must always come at the start of a block. As such, this is incorrect, and my guess is that it is the reason for the error that you are most worried about:
    Code:
    ptr=&stackSize;
    double stack[4]={1.0, 2.0, 3.0};
    Considering that "ptr" is not a particularly descriptive name, I think that you should just drop it and write:
    Code:
    int stackSize = 0;
    double stack[4] = {1.0, 2.0, 3.0};
    
    push(stack, &stackSize, 9.0);
    /* ... */
    There are other problems as well. For example, your pop and push functions take a double as the first argument, but you probably want a pointer to a double instead.

    Oh, and change void main to int main.
    Doing this and what the poster above you solves the previous errors but causes about 20 more...

    Code:
    double pop (double stack[], &stackSize);
    void push (double stack[], &stackSize, double x);
    
    int main (void)
    {
    	double stack[4]={1.0, 2.0, 3.0};
    	int stackSize=0;
    
    	push(stack, stackSize, 9.0);
    	printf("%lf\n", pop(stack, stackSize));
    	push(stack, stackSize, 8.0);
    	push(stack, stackSize, 7.0);
    	push(stack, stackSize, 6.0);
    	printf("%lf\n", pop(stack, stackSize));
    	printf("%lf\n", pop(stack, stackSize));
    	printf("%lf\n", pop(stack, stackSize));
    }
    
    double pop (double stack[], &stackSize)
    {
    	double value=0;
    	value=stack[&stackSize];
    	--&stackSize;
    	return value;
    }
    
    void push (double stack[], &stackSize, double x)
    {
    	stack[&stackSize]=x;
    	++&stackSize;
    }
    All having to do with pop, push and their prototypes...
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(5) : error C2059: syntax error : ','
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(5) : error C2143: syntax error : missing ')' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(5) : error C2143: syntax error : missing '{' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(5) : error C2059: syntax error : '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(5) : error C2059: syntax error : ')'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(6) : error C2059: syntax error : ','
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(6) : error C2143: syntax error : missing ')' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(6) : error C2143: syntax error : missing '{' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(6) : error C2059: syntax error : '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(6) : error C2059: syntax error : ')'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(13) : warning C4013: 'push' undefined; assuming extern returning int
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(14) : warning C4013: 'pop' undefined; assuming extern returning int
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(23) : error C2059: syntax error : ','
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(23) : error C2143: syntax error : missing ')' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(23) : error C2143: syntax error : missing '{' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(23) : error C2059: syntax error : '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(23) : error C2059: syntax error : ')'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(31) : error C2059: syntax error : ','
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(31) : error C2143: syntax error : missing ')' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(31) : error C2143: syntax error : missing '{' before '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(31) : error C2059: syntax error : '&'
    1>c:\documents and settings\edward\my documents\visual studio 2008\projects\cstuff\cothertwo\citemtwo.c(31) : error C2059: syntax error : ')'

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sparrowhawk
    Doing this and what the poster above you solves the previous errors but causes about 20 more...
    You need to understand pointer syntax. What you want is to declare and define these functions:
    Code:
    double pop (double stack[], int *stackSize);
    void push (double stack[], int *stackSize, double x);
    You would use them like this:
    Code:
    push(stack, &stackSize, 9.0);
    pop(stack, &stackSize);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    You need to understand pointer syntax. What you want is to declare and define these functions:
    Code:
    double pop (double stack[], int *stackSize);
    void push (double stack[], int *stackSize, double x);
    You would use them like this:
    Code:
    push(stack, &stackSize, 9.0);
    pop(stack, &stackSize);
    Changed, still the exact same list of errors posted above.

    Too be honest, I started out using pointers but I got some errors there and tried to switch it around... But so far I've just been getting more and more errors... There's something simpler that's causing a bunch of errors to happen...

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sparrowhawk
    Changed, still the exact same list of errors posted above.
    You probably did not make all the corresponding changes. What is your current code?

    Do you understand that if stackSize is an int, then &stackSize returns the address of the int, and pointers store addresses? Do you understand that if stackSize is a pointer, then *stackSize returns the object that the pointer points to?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Quote Originally Posted by Sparrowhawk View Post
    Changed, still the exact same list of errors posted above.

    Too be honest, I started out using pointers but I got some errors there and tried to switch it around... But so far I've just been getting more and more errors... There's something simpler that's causing a bunch of errors to happen...
    Please post your most recent code as such.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    You probably did not make all the corresponding changes. What is your current code?

    Do you understand that if stackSize is an int, then &stackSize returns the address of the int, and pointers store addresses? Do you understand that if stackSize is a pointer, then *stackSize returns the object that the pointer points to?
    Yes infact I started out with

    Code:
    char pop (char **ptr);
    void push (char x, char **ptr);
    
    int main (void)
    {
    	char *top;
    	char **ptr;
    	char stack[9]="coolcwer";
    	top=stack+7;
    	ptr=&top;
    	printf("&#37;s\n", stack);
    	printf("%c\n", pop(ptr));
    	printf("%c\n", pop(ptr));
    	push ('u', ptr);
    	printf("%s\n", stack);
    }
    
    char pop (char **ptr)
    {
    	char value='u';
    	value=**ptr;
    	**ptr='x';
    	--*ptr;
    	return value;
    }
    
    void push (char x, char **ptr)
    {
    	++*ptr;
    	**ptr=x;
    }
    Which works like a charm, but I really REALLY didn't want use pointers to pointers... And it didn't seem I could change the pointers value like I needed too... Which is change the value it pointed to in the array inside the function and keep those changes outside the function... So the next step would be a pointer to a pointer to do that... But there must be a better way.

    [edit] - For whatever reason something is causing the program to act nutty... When I try to see what arguments push and pop have it only shows they have 1 argument defined, but you can see that there are 2 for pop and 3 for push... All its seeing right now is pop (double stack[]) and push (double stack[])...
    Last edited by Sparrowhawk; 12-15-2008 at 01:25 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Both stevesmithx and I asked you to post your current (i.e., most recent) code, not the code that you started out with.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    Quote Originally Posted by laserlight View Post
    Both stevesmithx and I asked you to post your current (i.e., most recent) code, not the code that you started out with.
    Code:
    double pop (double stack[], int *stackSize);
    void push (double stack[], int *stackSize, double x);
    
    int main (void)
    {
    	double stack[4]={1.0, 2.0, 3.0};
    	int *stackSize;
    	int stackSz=0;
    	stackSize=&stackSz;
    	
    
    	push(stack, stackSize, 9.0);
    	printf("&#37;lf\n", pop(stack, stackSize));
    	push(stack, stackSize, 8.0);
    	push(stack, stackSize, 7.0);
    	push(stack, stackSize, 6.0);
    	printf("%lf\n", pop(stack, stackSize));
    	printf("%lf\n", pop(stack, stackSize));
    	printf("%lf\n", pop(stack, stackSize));
    }
    
    double pop (double stack[], int *stackSize)
    {
    	double value=0;
    	value=stack[*stackSize];
    	--stackSize;
    	return value;
    }
    
    void push (double stack[], int *stackSize, double x)
    {
    	stack[*stackSize]=x;
    	++stackSize;
    }
    I got it working, unfortunately the problem I started with in the beginning is still here...

    Output is:
    9.000000
    6.000000
    6.000000
    6.000000
    Press any key to continue . . .
    If you can't tell, it's a problem with changing the value the pointer points too so I can get other values in the array.

    [edit] - So I suppose the question is can I change the value the pointer points too without using a pointer to a pointer...
    Last edited by Sparrowhawk; 12-15-2008 at 01:34 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sparrowhawk
    it's a problem with changing the value the pointer points too so I can get other values in the array.
    Ah yes. If stackSize is a pointer then --stackSize decrements the pointer, not what the pointer points to. You should have written --*stackSize. The same goes for ++stackSize versus ++*stackSize.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Registered User
    Join Date
    Oct 2008
    Posts
    98
    nevermind got it fixed... which is odd because I did this before and instead it decided to crash
    Last edited by Sparrowhawk; 12-15-2008 at 01:39 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Sparrowhawk
    Still some problems... The way I see it, the original push should overwrite 1.0 thus the first pop should give 9.0
    Yes, that should indeed be expected. The bug is in the implementation of pop. Think of why you pop off 2 instead of 9 (i.e., you pop off something is that not logically on the stack yet).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    Changing stackSz=-1 for example causes the first entry to be 1.0 and go out of bounds... same with stackSz=1
    Edit:
    Ah,Nevermind.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. set testing code
    By zxcv in forum C++ Programming
    Replies: 8
    Last Post: 12-22-2008, 02:58 AM
  2. Problem : Threads WILL NOT DIE!!
    By hanhao in forum C++ Programming
    Replies: 2
    Last Post: 04-16-2004, 01:37 PM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. linked list source code errors
    By C++_Learner in forum C++ Programming
    Replies: 1
    Last Post: 04-18-2002, 11:04 PM
  5. Reqd. C source code for Testing purposes
    By DP in forum C Programming
    Replies: 5
    Last Post: 01-14-2002, 12:45 PM