1

I am implementing a small program to exercise with double pointers This is the main program:

#include <stdio.h>
#include "serial.h"
#include "stack.h"

int main(void) {
    serial_init();  

    /* Array to hold stack entries */
    int stack[10]={0};
    /* Stack pointer */
    int *stack_p = stack;

    /* Code to call the functions */

    push(&stack_p, 1); 
    push(&stack_p, 2); 
    push(&stack_p, 3); 
    push(&stack_p, 4); 

    printf("popped value: %d\r\n", pop(&stack_p));
    printf("popped value: %d\r\n", pop(&stack_p));
    printf("popped value: %d\r\n", pop(&stack_p));
    printf("popped value: %d\r\n", pop(&stack_p));
}        

void push(int **sp, int value) {
    /*  Implement it */ 
}

int pop(int **sp) {
    /*  Implement it */ 
}

I have implemented the push function it seems ok. However, the pop return back just the last element and then 10

void push(int **sp, int value) {
/* implemented it*/

 int *pt; 
 pt=&value; // Store the value to the pointer 

 printf("Push value is is %d\r\n", *pt);
 sp = &pt; // associate the pointer to the pointer of pointer
 ++(*pt);   
}

int pop(int **sp) {
 /* implemented it */
 int value;
 int *pt;
 pt=&value;

 sp = &pt;
 *pt--;

 return value;
}   
4
  • Try (*pt)--. Otherwise, rightward operators generally bind tighter than leftward ones. Also, where is the pointer pt given a value? Commented Mar 14, 2019 at 16:17
  • 4
    I have formatted part 1 of your code. Why don't you click edit and similarly format the other parts? (If you want my attention, type @thb and your question in the comments. The system will flag me.) Commented Mar 14, 2019 at 16:21
  • 1
    Hint: never use actual tabs for indentation but only spaces. Otherways you'll have enless formatting problems when you share code code Commented Mar 14, 2019 at 16:23
  • What makes you thing push is working? You never actually assign value to anything and &value is the address of a temporary copy of the argument passed. The way to see if either operation is working is to inspect the stack array in main() - use a debugger it will be lot faster that posting questions. Commented Mar 14, 2019 at 17:55

1 Answer 1

6

Your push and pop functions are overly complicated and totally wrong:

You want this:

void push(int **sp, int value) {
  **sp = value;   // put value onto top of the stack
  (*sp)++;        // increment stack pointer
}

int pop(int **sp) {
  (*sp)--;        // decrement stack pointer
  return **sp;    // return value which is on nthe op of the stack
}

Your wrong code for push with explanations in comments:

void push(int **sp, int value) {
  int *pt; 
  pt=&value; // here you put the pointer to the local variable value
             // into pt, but local variables disappear as soon
             // as the function has finished

  //  the printf is the only thing one more or less correct
  //  but you could just print directly 'value' like this:
  //    printf("Pushed value is %d\r\n", value);
  //
  printf("Push value is is %d\r\n", *pt);

  sp = &pt;  // this only assigns the pointer to the local variable pt to 
             // the local variable sp

  ++(*pt);   // here you increment actually the local variable
             // value which is pointless 
}

By the way: the initialisation to zero of the whole stack is not necessary, although it might help during the debugging process. So you can write the declaration of the stack like this:

int stack[10];  // no initialisation necessary

Exercise for you:

Explain the exact reason why it is not necessary to initialize all elements of the stack to zero.

Sign up to request clarification or add additional context in comments.

2 Comments

Hello, Thanks. I do not know why. I have read around and seems the best way is to initialize to zero to avoid garbage
@AntonioArnone yes that's true, but here even if you don't initialize to zero, you won't see any garbage where you don't want to. When you push, it will overwrite prexesting garbage with the pushed value and when you pop it will read the previously pushed value... no garbage involved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.