当前位置:网站首页>Pointer array function combination in C language

Pointer array function combination in C language

2022-06-25 05:14:00 Distant Aoki

#include<stdio.h>
void initarray(int *array,int size)
{
    int i;
    for(i=0;i<size;i++)
    {
        printf(" Please enter the first %d Data :\n",i+1);
        scanf("%d",array); // Or it can be written as scanf("%d",array++);
        array++;
    }
}


void printarray(int *array,int size)
{
    int i;
    for(i=0;i<size;i++)
    {
        printf("%d\n",*array);// Or it can be written as scanf("%d",*array++);
        array++;
        
    }
}

int main()
{
    int array[5];
    int size=sizeof(array)/sizeof(array[0]);
    initarray(array,size);// The actual parameter is the first address of the array , The array name represents the address of the first element
    printarray(&array[0],size);
    return 0;
}


/* Mixed writing of pointer and array name :
1、 Pointer as array name , Subscript access
2、 Add... To the array name
3、 You can't array++,sizeof Time pointer variables occupy eight bytes
/* for example
int main()
{
    int array[3]={1,2,3};
    int *p=array;
    for(i=0;i<3;i++)
    {
        printf("%d",p[i]);1、 Pointer as array name , Subscript access
    }
    for(i=0;i<3;i++)
    {
        printf("%d",*(array+i));1、 Array name plus
        
    }
}
 

原网站

版权声明
本文为[Distant Aoki]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202210521073415.html