📜  C返回数组

📅  最后修改于: 2020-10-22 01:28:18             🧑  作者: Mango

#### 什么是数组？

``````#include
void getarray(int arr[])
{
printf("Elements of array are : ");
for(int i=0;i<5;i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[5]={45,67,34,78,90};
getarray(arr);
return 0;
}
``````

``````#include
void printarray(char *arr)
{
printf("Elements of array are : ");
for(int i=0;i<5;i++)
{
printf("%c ", arr[i]);
}
}
int main()
{
char arr[5]={'A','B','C','D','E'};
printarray(arr);
return 0;
}
``````

``````#include
int *getarray()
{
int arr[5];
printf("Enter the elements in an array : ");
for(int i=0;i<5;i++)
{
scanf("%d", &arr[i]);
}
return arr;
}
int main()
{
int *n;
n=getarray();
printf("\nElements of array are :");
for(int i=0;i<5;i++)
{
printf("%d", n[i]);
}
return 0;
}
``````

• 使用动态分配的数组
• 使用静态数组
• 使用结构

``````#include
int *getarray(int *a)
{

printf("Enter the elements in an array : ");
for(int i=0;i<5;i++)
{
scanf("%d", &a[i]);
}
return a;
}
int main()
{
int *n;
int a[5];
n=getarray(a);
printf("\nElements of array are :");
for(int i=0;i<5;i++)
{
printf("%d", n[i]);
}
return 0;
}
``````

``````#include
#include
int *getarray()
{
int size;
printf("Enter the size of the array : ");
scanf("%d",&size);
int *p= malloc(sizeof(size));
printf("\nEnter the elements in an array");
for(int i=0;i``````

``````#include
int *getarray()
{
static int arr[7];
printf("Enter the elements in an array : ");
for(int i=0;i<7;i++)
{
scanf("%d",&arr[i]);
}
return arr;

}
int main()
{
int *ptr;
ptr=getarray();
printf("\nElements that you have entered are :");
for(int i=0;i<7;i++)
{
printf("%d ", ptr[i]);
}
}
``````

``````#include
#include
struct array
{
int arr[8];
};
struct array getarray()
{
struct array y;
printf("Enter the elements in an array : ");
for(int i=0;i<8;i++)
{
scanf("%d",&y.arr[i]);
}
return y;
}
int main()
{
struct array x=getarray();
printf("Elements that you have entered are :");
for(int i=0;x.arr[i]!='\0';i++)
{
printf("%d ", x.arr[i]);
}
return 0;
}
``````