如何使用指针在另一个函数中分配数组 [英] How to use pointers to allocate an array inside another function
问题描述
这里是我在main中分配数组时使用的代码。
#include< stdio.h>
#include< stdlib.h>
#include< windows.h>
#include< time.h>
#define ESC_KEY 27
#define NUM_1_KEY 49
#define NUM_2_KEY 50
void find_two_largest(int a [],int n,int * ,int * second_largest);
void arrayInit(int *,int *,int,int);
void randGenArray(int [],int);
void inputArray(int [],int);
void result(int,int);
void loading(void);
int menu(void);
int main(void)
{
system(color f5);
int n,i,最大,最大_2,* a;
arrayInit(a,& n,2,10);
if(menu())
randGenArray(a,n);
else
inputArray(a,n);
find_two_largest(a,n,& largest,& largest_2);
结果(最大,最大_2);
返回0;
$ b $ void find_two_largest(int a [],int n,int * largest,int * second_largest)
{
int i = 0, J = 0;
系统(cls);
loading();
*最大= 0;
* second_largest = *最大;如果(*最大值 *最大值= a [i],则
为(i = 1; i< n; i ++)
}
for(j = 1; j if(* largest == a [j])
continue;
else {
if(* second_largest< a [j])
* second_largest = a [j];
}
}
return;
void randGenArray(int a [],int n)
{
srand(time(NULL));
int i;
for(i = 0; i a [i] = rand()%100;
睡眠(10);
printf(\\\
>> Integer%d:%d,i + 1,a [i]);
}
printf(\\\
\\\
\\\
按任意键继续...);
getch();
return;
}
void inputArray(int a [],int n)
{
int i;
for(i = 0; i< n; i ++){
printf(\\\
请输入整数%d:,i + 1);
scanf(%d,& a [i]);
}
return;
}
int菜单(void)
{
char _char;
printf(\\\
请选择以下选项之一:\ n 1.手动填充数组\ n 2.用随机数填充数组\ \ n);
while(1)
{
_char = getch();
switch(_char)
{
case ESC_KEY:
printf(\\\
\\\
谢谢您使用我们的软件!\\\
\\\
);
exit(0);
case NUM_1_KEY:
system(cls);
返回0;
case NUM_2_KEY:
system(cls);
返回1;
默认值:
break;
$ b void arrayInit(int * a,int * n,int min,int max)
{
printf (\\\
请输入数组的长度:);
do {
scanf(%d,n);
if(* n max)
printf(\\\
范围有限,请输入%d和%d.\
之间的值,min,max );
} while(* n max);
a =(int *)calloc(* n,sizeof(int));
return;
}
void loading(void)
{
printf(\\\
Loading);
printf(。);
睡眠(300);
printf(。);
睡眠(300);
printf(。);
睡眠(300);
系统(cls);
return;
void result(int l,int l2)
{
system(cls);
printf(\\\
最大=%d第二大=%d,l,l2);
睡眠(500);
printf(\\\
\\\
\\\
谢谢您使用我们的软件!; D\\\
\\\
);
return;
}
但是,如果您将此行从arrayInit剪切并粘贴到main并更改* n到n - 它会工作!
a =(int *)calloc(* n,sizeof(int));
我很抱歉询问这些愚蠢而明显的事情,但我并没有自己弄明白。感谢您的任何建议。
这是一个简单的程序,它会告诉你如何做到这一点 -
#include
#include< stdlib.h>
void create(int ** p,int n); //函数将参数作为int **并且n是元素的数量
int main(void){
int * a;
int n = 5,i; //声明并初始化n
create(& a,n); //将a的地址传递给函数
for(i = 0; i< n; i ++){
a [i] = i; //将i的值存储在[i]
printf(%d \ n,i)中; //打印一张[i]
}
免费(a); //释放分配的内存
return 0;
}
void create(int ** p,int n){
* p = calloc(n,sizeof(int)); //分配内存给* p(type-是int *)
}
I'm 2nd year computer engineer and still in learning process of C language. I'd like to undesrtand how to dynamically alocate an array by using function instead of allocate inside the main.
Here is the code that works when I allocate array inside main.
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
#include <time.h>
#define ESC_KEY 27
#define NUM_1_KEY 49
#define NUM_2_KEY 50
void find_two_largest(int a[], int n, int *largest, int *second_largest);
void arrayInit(int *,int *, int, int);
void randGenArray(int [], int);
void inputArray(int[], int);
void result(int, int);
void loading(void);
int menu(void);
int main(void)
{
system("color f5");
int n,i,largest,largest_2, *a;
arrayInit(a,&n, 2, 10);
if(menu())
randGenArray(a,n);
else
inputArray(a,n);
find_two_largest(a,n,&largest,&largest_2);
result(largest,largest_2);
return 0;
}
void find_two_largest(int a[], int n, int *largest, int *second_largest)
{
int i=0,j=0;
system("cls");
loading();
*largest = 0;
*second_largest = *largest;
for (i=1;i<n;i++){
if(*largest<a[i])
*largest=a[i];
}
for(j=1;j<n;j++){
if(*largest==a[j])
continue;
else{
if(*second_largest<a[j])
*second_largest=a[j];
}
}
return;
}
void randGenArray(int a[], int n)
{
srand(time(NULL));
int i;
for(i=0; i<n; i++){
a[i]=rand()%100;
Sleep(10);
printf("\n>> Integer %d: %d", i+1, a[i]);
}
printf("\n\n\nPress any key to continue...");
getch();
return;
}
void inputArray(int a[], int n)
{
int i;
for(i=0; i<n; i++){
printf("\n Please enter integer %d: ", i+1);
scanf("%d",&a[i]);
}
return;
}
int menu(void)
{
char _char;
printf("\n Please choose one of the following options:\n 1.Fill array manually\n 2.Fill array by random numbers\n\n ");
while(1)
{
_char = getch();
switch(_char)
{
case ESC_KEY:
printf("\n\n Thank you for using our software!\n\n");
exit(0);
case NUM_1_KEY:
system("cls");
return 0;
case NUM_2_KEY:
system("cls");
return 1;
default:
break;
}
}
}
void arrayInit(int *a,int *n, int min, int max)
{
printf("\n Please enter a length of the array: ");
do{
scanf("%d", n);
if (*n<min||*n>max)
printf("\nThe ranged is limited. Please enter the value between %d and %d.\n", min, max);
} while(*n<min||*n>max);
a = (int*)calloc(*n,sizeof(int));
return;
}
void loading(void)
{
printf("\n Loading");
printf(".");
Sleep(300);
printf(".");
Sleep(300);
printf(".");
Sleep(300);
system("cls");
return;
}
void result(int l, int l2)
{
system("cls");
printf("\n Largest = %d Second Largest = %d",l,l2);
Sleep(500);
printf("\n\n\n Thank you using our software! ;D\n\n");
return;
}
But if you cut and paste this line from arrayInit to main and change *n to n - it will work!
a = (int*)calloc(*n,sizeof(int));
I'm sorry for asking about so stupid and obvious things but I didn't figure it out by myself. Thank you for any advice.
Here is a simple program which will show you how to do that -
#include <stdio.h>
#include <stdlib.h>
void create(int **p,int n); // function taking arguments as int ** and n is number of elements
int main(void) {
int *a;
int n=5,i; // declare and initialize n
create(&a,n); // pass address of a to function
for(i=0;i<n;i++){
a[i]=i; // store value of i in a[i]
printf("%d\n",i); // print a[i]
}
free(a); // free the allocated memory
return 0;
}
void create(int **p, int n){
*p=calloc(n,sizeof(int)); // allocate memory to *p (type- is int *)
}
这篇关于如何使用指针在另一个函数中分配数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!