Testing some code, lots of errors...

This is a discussion on Testing some code, lots of errors... within the C Programming forums, part of the General Programming Boards category; Problem is probably obvious, it's late and I'm having trouble finding out why I'm getting some errors on this code: ...

  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
    494
    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 12: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
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,636
    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);
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,636
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    494
    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 12:25 AM.

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    Both stevesmithx and I asked you to post your current (i.e., most recent) code, not the code that you started out with.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 12:34 AM.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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 12:39 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,636
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    494
    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

Page 1 of 4 1234 LastLast
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, 01: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, 12: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, 11:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21