1

I run the following code in c. Why do the two main bodies lead to different outputs

#include <stdio.h>
#include <stdlib.h>
int enqueue(int* ptr,int h,int t,int n,  int v)
{
    ptr[h]=v;
    return 0;
}

int main()
{
    int* Q=(int*)malloc(n*sizeof(int));
    int h=0, t=0;
    int a= enqueue(Q,h,t,n,5); 
    printf("%d %d ",a,Q[h]);
    return 0;
}

OUTPUT: 0 5

Now if I change main function to this:

int main()
{
    int* Q=(int*)malloc(n*sizeof(int));
    int h=0, t=0;
    printf("%d %d ",enqueue(Q,h,t,n,5),Q[h]);
    return 0;
}

OUTPUT: 0 0

  • What is `el`? It isn't declared anywhere. – wallyk Oct 18 '19 at 20:42
  • As an aside, I recommend that you read and understand [the question on why not to cast the return value of `malloc()` and family in C](/q/605845). You simply need `int *Q = malloc(sizeof *Q * n);` – Toby Speight Oct 21 '19 at 10:37

0 Answers0