0

I have an array with multiple structs. When i ask the user to enter data the first time everything works but when i ask again for the next position in the array the program crashes. If this method doesn't work souldn't the program crash in the beginning? Is something wrong with malloc?

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



struct student {
    char name[50];
    int semester;
};

struct prof {
    char name[50];
    char course[50];
};

struct student_or_prof {
    int flag;
    int size;
    int head;
    union {
        struct student student;
        struct prof prof;
    }
}exp1;
struct student_or_prof *stack;


void init(int n)
{
   stack = malloc(n);


}

int push(struct student_or_prof **pinx,int *head,int n)
{
    char name[50];

    printf("\nn= %d\n",n);
     printf("\nhead= %d\n",*head);
    if(*head==n)
    {
        printf("Stack is full.\n");
        return 1;
    }


    char x;
    printf("Student or Professor? [s/p] ");
     getchar() != '\n';
    scanf("%c",&x);

    if(x=='s')
    {

        getchar() != '\n';
       pinx[*head]->flag = 0;

       printf("\n\nGive student's name: ");
       fgets(pinx[*head]->student.name,sizeof(pinx[*head]->student.name),stdin);

       printf("\nGive student's semester: ");
       scanf("%d",&(pinx[*head]->student.semester));

       printf("\nName = %s\tSemester = %d",pinx[*head]->student.name,pinx[*head]->student.semester);

    }
    else if(x=='p')
    {
        getchar() != '\n';
       pinx[*head]->flag = 1;

       printf("\n\nGive professor's name: ");
       fgets(pinx[*head]->prof.name,sizeof(pinx[*head]->prof.name),stdin);

       printf("\nGive course: ");
       fgets(pinx[*head]->prof.course,sizeof(pinx[*head]->prof.course),stdin);

       printf("\nName = %s\tCourse = %s\n",pinx[*head]->prof.name,pinx[*head]->prof.course);
    }



    (*head)++;
    printf("\nhead= %d\n",*head);



}


int main()
{
    int n,i;
  printf("Give size: ");
  scanf("%d",&n);

  init(n);

 for(i=0;i<n;i++)
   push(&stack,&exp1.head,n);

    return 0;
}
1
  • 1
    How many student_or_prof nodes do you think will fit in the memory allocated with this stack = malloc(n); ? Commented Mar 27, 2014 at 21:42

3 Answers 3

1

You need to malloc the structure not n

malloc(sizeof(struct student_or_prof)*n)

EDIT:

And your code crashes again because pinx is a double pointer, so this operation is not valid:

pinx[*head]->flag = 0;

this is equivalent to:

*(pinx + *head)->flag = 0;

Since you are not changing what stack points to, you are better off using a single pointer instead of a double pointer.

So instead you should change your push API:

int push(struct student_or_prof *pinx,int *head,int n)

and call it like:

push(stack,&exp1.head,n);
Sign up to request clarification or add additional context in comments.

4 Comments

where does it crash? Use GDB or put some printfs to pin-point the line which is causing the crash.
it crashes there pinx[*head]->flag = 0; EDIT: stack = malloc(sizeof(struct student_or_prof)*n); is it wrong?
I don't want something like that. I called as push(&stack,&exp1.head,n); and double pointer in pinx because i want the changes to remain in stack array.
They will remain in the stack. You are passing the address of stack and then doing changes on that address. So, this can be done using a single pointer. Double pointer is an overkill.
1

malloc allocates the given number of bytes.

You have to multiply n with the size of your struct, to allocate enough memory.

1 Comment

Of course you are right but i've tried that and it crashes again
0

pinx does not point to an array, so pinx[*head] is going to access invalid memory unless *head is zero.

I think you meant (*pinx)[*head] , which accesses the N-th element of the array you allocated via malloc. For example (*pinx)[*head].prof.name etc.

BTW, your head number doesn't seem to be used at all, except for exp1.head, maybe it'd be better to remove head from the struct, and just have a single variable head?

2 Comments

No i really need head there. Everything is good now actually , brokenfoot gave me what i need
OK, good to hear. However if you ever decide you want push to be able to increase the capacity of the stack (e.g. you want to allow people to enter names without having to specify the count beforehand), you may need to return to the pointer-to-pointer version.

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.