这个代码哪里有问题?为什么输出不正常

 #include <stdio.h>
#include <string.h>
#include <stdlib.h>
typedef int bool;
struct Array
{
    int * pHead;//头指针
    int len;//数组长度
    int cnt;//当前元素个数
};
void initArray(struct Array *p,unsigned length);//初始化数组函数
void showArray(struct Array *p);//输出数组
bool isEmpty(struct Array *p);//判断数组元素是否为0
bool isFull(struct Array *p);//判断元素是否为满
void appand(struct Array *p,int i);//末尾追加元素
void sort(struct Array *p);//int jj;
int main()
{
    struct Array arr;
    initArray(&arr, 5);
    appand(&arr, 3);
    appand(&arr, 1000);
    appand(&arr, 5);
    printf("sort before:");
    showArray(&arr);
    sort(&arr);
    printf("\nsort after:");
    showArray(&arr);
}//初始化数组
void initArray(struct Array *p,unsigned length)
{
    if(length==0)
    {
        printf("数组长度不合法\n");
        exit(0);
    } //
    p->pHead=(int *)malloc(sizeof(int));
    if((p->pHead=(int *)malloc(sizeof(int)))==NULL)
    {
        printf("初始化失败\n");    //
        exit(0);
    }
    p->len=length;
    p->cnt=0;
   // free(p->pHead);
}
//数组输出函数
void showArray(struct Array *p)
{
   // struct Array *h=(struct Array*)malloc(sizeof(struct Array));
   // h=p;
    int i;
    if(p->len==0) printf(NULL);
    if(p->cnt==0) printf("数组元素为空\n");
    for (i=0; i<p->cnt; i++)
    {
        printf("%d ",p->pHead[i]);
    }
    //return p;

}//判断数组是否为空
bool isEmpty(struct Array *p)
{
    return p->cnt==0?1:0;
}//判断数组是否满
bool isFull(struct Array *p)
{
    return p->cnt==p->len?1:0;
}//末尾追加元素
void appand(struct Array *p,int i)
{
    //判断当前元素是否已满
    if(isFull(p)) printf("元素已满,添加失败\n");
    p->pHead[p->cnt]=i;
    p->cnt=p->cnt+1;
}//排序冒泡
void sort(struct Array *p)
{  int i,j;
    int temp;
    for (i=0; i<=p->cnt-1; i++)
    {
        for (j=0; j<=p->cnt-i-i ; j++)
        {
            if (p->pHead[j]>p->pHead[j+1])
            {
                temp=p->pHead[j];
                p->pHead[j]=p->pHead[j+1];
                p->pHead[j+1]=temp;
            }
        }
    }
}


输出有时候会变 正常应该输出 3 5 1000 但是它输出有时候会变成各种奇怪的值偶尔

 // Q695942.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"


#include <stdio.h>
#include <string.h>
#include <stdlib.h>
//typedef int bool;
struct Array
{
    int * pHead;//头指针
    int len;//数组长度
    int cnt;//当前元素个数
};
void initArray(struct Array *p,unsigned length);//初始化数组函数
void showArray(struct Array *p);//输出数组
bool isEmpty(struct Array *p);//判断数组元素是否为0
bool isFull(struct Array *p);//判断元素是否为满
void appand(struct Array *p,int i);//末尾追加元素
void sort(struct Array *p);//int jj;
int main()
{
    struct Array arr;
    initArray(&arr, 5);
    appand(&arr, 3);
    appand(&arr, 1000);
    appand(&arr, 5);
    printf("sort before:");
    showArray(&arr);
    sort(&arr);
    printf("\nsort after:");
    showArray(&arr);
}//初始化数组
void initArray(struct Array *p,unsigned length)
{
    if(length==0)
    {
        printf("数组长度不合法\n");
        exit(0);
    } //
    //p->pHead=(int *)malloc(sizeof(int));
    if((p->pHead=(int *)malloc(sizeof(int) * 5))==NULL)
    {
        printf("初始化失败\n");    //
        exit(0);
    }
    else
    {
        memset(p->pHead, 0, 5 * sizeof(int));
    }
    p->len=5;
    p->cnt=0;
   // free(p->pHead);
}
//数组输出函数
void showArray(struct Array *p)
{
   // struct Array *h=(struct Array*)malloc(sizeof(struct Array));
   // h=p;
    int i;
    if(p->len==0) printf(NULL);
    if(p->cnt==0) printf("数组元素为空\n");
    for (i=0; i<p->cnt; i++)
    {
        printf("%d ",p->pHead[i]);
    }
    //return p;

}//判断数组是否为空
bool isEmpty(struct Array *p)
{
    return p->cnt==0?1:0;
}//判断数组是否满
bool isFull(struct Array *p)
{
    return p->cnt==p->len?1:0;
}//末尾追加元素
void appand(struct Array *p,int i)
{
    //判断当前元素是否已满
    if(isFull(p)) printf("元素已满,添加失败\n");
    p->pHead[p->cnt]=i;
    p->cnt=p->cnt+1;
}//排序冒泡
void sort(struct Array *p)
{  int i,j;
    int temp;
    for (i=0; i<=p->cnt-1; i++)
    {
        for (j=0; j<=p->cnt-i-2 ; j++)
        {
            if (p->pHead[j]>p->pHead[j+1])
            {
                temp=p->pHead[j];
                p->pHead[j]=p->pHead[j+1];
                p->pHead[j+1]=temp;
            }
        }
    }
}


struct Array
{
int * pHead;//头指针
int len;//数组长度
int cnt;//当前元素个数
};
有两种情况:
第一种情况是*pHeadl里面本身是5个元素,而你里面只存储了三个元素,后两个元素里面还是有值的,这个值就是你说的不知道什么值,应该在输出的时候
检查一下,*pHeaad里面有几个元素,按实际元素输出。
第二种情况是:*pHead指针没有释放,应该把这里面的指针释放,如果不释放,第二次再存数据的时候,可能原先的数据还在。