0

I am getting garbage / junk values as output when my program is run and the data displayed.

Why is it so?

Can someone help me to understand how to properly pass by pointers and not get junk values?

This program is about stack creation of struct books type variables.

By default shouldn't the variable bks pass by pointer and change when b is changed?

bks is still storing garbage value.

Here is my code:

#include<stdio.h>
#include<stdlib.h>
struct books
{
    int yrpub;
    char name[100],author[50];
};
int top=-1;
int push(struct books b[],int top,int n)
{
    if(top==n-1)
        return -1;
    else
    {
        ++(top);
        printf("Enter books info: \n");
        printf("Enter name: ");
        gets(b[top].name);
        printf("Enter author: ");
        gets(b[top].author);
        printf("Enter Year of publish: ");
        scanf("%d",&b[top].yrpub);
        return top;
    }
}
void display(struct books b[],int top)
{
    int i;
    if(top==-1)
        printf("No books in the stack...");
    for(i=0;i<=top;i++)
    {
        printf("Details of book %d: \n",i+1);
        printf("Name: %s\nAuthor: %s\nYear of publish: %d\n",b[i].name,b[i].author,b[i].yrpub);
    }
    system("pause");
}

int main()
{
    struct books bks[10];
    int ch;
    system("cls");
    printf("Select an option:\n");
    printf("1. Push book\n2. Pop book\n3. Peep book\n4. Display all books info\n5. Exit\n");
    printf("Enter a choice: ");
    scanf("%d",&ch);
    fflush(stdin);
    switch(ch)
    {
    case 1:
        system("cls");
        top=push(bks,top,10);
        break;

    case 4:
        system("cls");
        display(bks,top);
        break;

    case 5:     exit(0);

    default:    printf("\nWrong choice...Please retry.");
        long i,j;
        for(i=0;i<1000000;i++)
            for(j=0;j<100;j++);
    }
    main();
}
johnsyweb
  • 136,902
  • 23
  • 188
  • 247
  • `fflush(stdin);` may be undefined behavior – Grijesh Chauhan Aug 31 '13 at 06:32
  • 1
    If this is C++ code, then calling `main` is undefined behavior. – Caesar Aug 31 '13 at 06:33
  • A global variable called `top` and a function parameter called `top` is a recipe for confusion. Use `-Wshadow` if you compile with gcc to warn you of such problems. Unless you're running on Windows, `fflush(stdin)` is undefined behaviour. – Jonathan Leffler Aug 31 '13 at 06:38
  • Sorry my bad... I removed C++ tag... However even if I create a new function without having the need to call main, it still gives garbage value... –  Aug 31 '13 at 06:39
  • Yes, I am using Dev-cpp in Windows... and I have used fflush(stdin) so that the '\n' entered during the choice does go into the name part if I push new book into stack... –  Aug 31 '13 at 06:41

2 Answers2

2

Each time you recursively call main(), you create a new array bk.

The information you entered in the previous invocation of main() is hidden from the new one.

  • To iterate is human; to recurse, divine.

In this context, give up divinity for humanity. Use iteration — in this context it is better.

This is your primary problem; there may also be other off-by-one or other errors.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Actually the question mentions the use of functions only so I need to pass the stack array to the function and maintain it... Isn't there a way out of this other than iterations? –  Aug 31 '13 at 06:47
  • You could, but probably shouldn't, make `bk` into a global array. You can certainly pass the array of structures to functions — you already do. Iterating is most sensible here. – Jonathan Leffler Aug 31 '13 at 06:51
0
push:
if(top==n-1)
        return -1;
main:
top=push(bks,top,10);

top is reset when the stack is full

Edit: And the second problem is main being called again, struct books bks[10] is reset in the next main, it is a recursion. Declare bks as global or go with a while loop instead of recursion.

while (1) {
    getChoices();
    if (exit)
        /* exit from here */
    process();
}
Sakthi Kumar
  • 3,047
  • 15
  • 28
  • Yes it is a problem, but my main question is if even I add only 1 book details even then when I am displaying the details it is giving garbage values... –  Aug 31 '13 at 06:37
  • Okay thanks for the answer... Since I cannot use recursion since questions explicitly mentions use of functions, I made bks static and it solved my problems... thanks. –  Aug 31 '13 at 06:49