开发者

Whats wrong with my code?

Lately I have been making my project for stacking, but I have a problem with my code for pushing a node:

void push(struct stackNode *Stackptr , int data)
{
     struct stackNode *conductor;

     conductor = malloc(sizeof(struct stackNode));
     if(conductor !=NULL)
     {
                conductor->data=data;
    开发者_如何学编程            conductor->nxt=*Stackptr;
                Stackptr = conductor ;
                  }     
     else{
         printf("Wrong insertion"); 
          }

     }

But it won't compile. If you want to see the rest of my code here it is:

#include<stdio.h>
#include<stdlib.h>

struct stackNode
{
       int data;
       struct stackNode *nxt;
       };

struct stackNode *Stack;


void instructions()
{
     printf("        Please Select a Number to do the following\n\n");
     printf("              [1]Push a value\n");
     printf("              [2]Pop a value\n");
     printf("              [2]Peek a node\n");
     printf("              [3]Display\n");
     printf("              [4]Exit\n               ");
     }

     int main ()
     {
         int value; 
         int choice;
         Stack = NULL;

         instructions();

     do
     {
       scanf("%d",&choice);
         system("cls");


         if(choice == 1)
             {
                 printf("Enter your Desired Digit: ");

                                       }

              if(choice == 2)
              {
                        printf(" "); 
                        }       
              if(choice == 3)
              {

                        printf(" ");
                        }
                if(choice == 4 )
                {

                          printf("bye!");
                          return 0;
                          }      



         }while(choice !=4);

         getch();
         }

    void push(struct stackNode *Stackptr , int data)
    {
         struct stackNode *conductor;

         conductor = malloc(sizeof(struct stackNode));
         if(conductor !=NULL)
         {
                    conductor->data=data;
                    conductor->nxt=*Stackptr;
                    Stackptr = conductor ;
                      }     
         else{
             printf("Wrong insertion"); 
              }

         }


In the push function you are assigning a stacknode, and not a pointer to a stacknode.

Here is the error:

test.c:76:31: error: incompatible types when assigning to type ‘struct stackNode *’ from type ‘struct stackNode’

Removing the * assigns the pointer, which is what you want.

So this works:

conductor->nxt=Stackptr;

However, there are other issues with the program.


I'm going to give a quick run-through of finding the fault; Quick compile, error is;

74: error: incompatible types when assigning to type ‘struct stackNode *’ from type ‘struct stackNode’

Right, so now I know to have a look at line 74. Down on line 74 is;

  conductor->nxt=*Stackptr;

Without even having to think out the problem or even get your head around pointers (which you need to, but this is just a quick debug) So lets remove the * and change it to;

  conductor->nxt=Stackptr;

And thats one problem sorted. There are many others within this code though. Make sure you debug and test frequently, and have a go at interpreting the errors. If all else fails, make sure you post a question on here with the error.


Since you are changing Stackptr in the push method you need to make this change visible to the caller of push by either

  • returning the changed Stackptr or
  • passing the address of Stackptr.


This:

conductor->nxt=*Stackptr;

looks wrong. The nxt field should be a pointer, but you're de-referencing so you're trying to stuff the entire node in there.

There are many other issues with the code, but this was the first obvious compile-time error I saw.


It will be easier if you use typedef:

typedef struct _stacknode
{
    int data;
    struct _stacknode *nxt;
} StackNode;

StackNode *Stack;

Also, as it seems you are learning "C" in the process, check some finished stack implementation. It's quite a common example in programming books or online tutorials. Probably there are tons of them in github. Study it, then try your own.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜