gpt4 book ai didi

c - 如何使用指针在另一个函数中分配数组

转载 作者:太空宇宙 更新时间:2023-11-04 03:35:34 24 4
gpt4 key购买 nike

我是二年级计算机工程师,仍在学习 C 语言。我想了解如何使用函数而不是在 main 内部分配来动态分配数组。

这是当我在 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;
}

但是如果您将此行从 arrayInit 剪切并粘贴到 main 并将 *n 更改为 n - 它会起作用!

a = (int*)calloc(*n,sizeof(int));

我很抱歉问了这么愚蠢和明显的事情,但我自己没有弄明白。感谢您的任何建议。

最佳答案

这是一个简单的程序,它将向您展示如何做到这一点 -

 #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 *)
}

Working Code

关于c - 如何使用指针在另一个函数中分配数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33011574/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com