用指针求非空数组长度的时候输出异常,求帮助

这是把求数组长度禁掉的代码,运行结果为输出2-100001的数,运行正常。


#include <stdio.h>
#include <string.h>
#define true 1
#define false 0
#define max  100000
/*int zhishu(int a)
{
    int j;
    int k=0;
    for(j=2;j<a;j++)
    {
        if(a%j==0)
        {
        k++;    
        }
    }
    if(k!=0)
    {
        printf(" %d bu shi zhishu ",a);
        
    }
    else if(k==0)
    {
            printf("%d is zhishu",a);
    }

    
}*/


int zhishu_1( long int *str)
{
    int i=0;
    int k=0; 
    int c;
    int cac=0;
    int len=0;
/*    while(*(str++)!=0)
    {    
        len++;

    }
    printf("%d  is lenth\n",len);
    for(i=1;i<=max;i+=1)
    {
        
        printf("%d\n",*(str+i));
    }
}*/

int main()
{
    long int  buf[max];
    int x,y;
    for(x=1;x<=max;x+=1)
    {
        buf[x-1]=x;
    
    }    
    zhishu_1(buf);
    
    return 0;
    
}

这是把求数组长度不注释的代码,运行结果输出:
100001 is lenth
100001
10359712
0
4199400
0
0
0
44
0
4225568
0
0
0
0
0
0
0
0
0
349435474
4401
0
下面是不注释的代码


#include <stdio.h>
#include <string.h>
#define true 1
#define false 0
#define max  100000
/*int zhishu(int a)
{
    int j;
    int k=0;
    for(j=2;j<a;j++)
    {
        if(a%j==0)
        {
        k++;    
        }
    }
    if(k!=0)
    {
        printf(" %d bu shi zhishu ",a);
        
    }
    else if(k==0)
    {
            printf("%d is zhishu",a);
    }

    
}*/


int zhishu_1( long int *str)
{
    int i=0;
    int k=0; 
    int c;
    int cac=0;
    int len=0;
    while(*(str++)!=0)
    {    
        len++;

    }
    printf("%d  is lenth\n",len);
    for(i=1;i<=max;i+=1)
    {
        
        printf("%d\n",*(str+i));
    }
}

int main()
{
    long int  buf[max];
    int x,y;
    for(x=1;x<=max;x+=1)
    {
        buf[x-1]=x;
    
    }    
    zhishu_1(buf);
    
    return 0;
    
}

这种问题很常见,就是你没有保存指针原来的值就移动指针,结果指针移动到了尾部之后又没把指针重置为原来的值
应对方法也很简单,在函数内部加一个指针变量tmp储存传入的str的值,当str移动到末尾后再讲tmp赋值给str