[英]How to pass arrays to a function in C
I am taking a number in the main function, make it an array in make_array function. 我在主函数中输入一个数字,在make_array函数中使其成为数组。 In the palindrome
function, I need to check the array which i made in the make_array
function but it is not visible in the palindrome
function. 在palindrome
函数中,我需要检查我在make_array
函数中make_array
的数组,但是在palindrome
函数中不可见。
How can I solve this problem? 我怎么解决这个问题?
#include<stdio.h>
#define N 5
void make_array(int n);
int palindrome(int ar[],int size);
int main()
{
int num;
printf("Enter a number to check: ");scanf("%d",&num);
make_array(num);
if(palindrome(/*Don't know what should I write here*/))
printf("It is palindrome");
else
printf("It is not palindrome");
}
void make_array(int n)
{
int arr[N];
int digit,i=0;
while(n>0){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(i=0; i<N; i++)
printf("%d ",arr[i]);
}
int palindrome(int ar[],int size)
{
int i,j;
int temp[N];
j=N;
for(i=0; i<N; i++)
temp[i]=ar[i];
for(i=0; i<N; i++){
if(temp[j-1]!=ar[i])
return 0;
j--;
}
return 1;
}
The best way is to leave allocation to the caller. 最好的方法是将分配留给调用方。 Then you can simply do 那你就可以做
int main()
{
int num;
printf("Enter a number to check: ");scanf("%d",&num);
int array[num];
fill_array(num, array);
where fill_array
does what "make_array" does in your code, minus the allocation part. fill_array
在代码中执行“ make_array”的操作,减去分配部分。
void fill_array(int num, int array[num]);
The palindrome function could be rewritten similarly: 回文函数可以类似地重写:
int palindrome(int size, int array[size])
All of this uses the concept of variable-length arrays (VLA). 所有这些都使用可变长度数组(VLA)的概念。
I have done some modification in your code so please refer it. 我已经对您的代码进行了一些修改,请参考。
#include<stdio.h>
#include "stdafx.h"
#define N 5
int make_array(int n, int *arr);
int palindrome(int ar[],int size);
int main()
{
int num;
int arr[N];
int iRet;
printf("Enter a number to check: ");scanf_s("%d",&num);
iRet = make_array(num, arr);
if(palindrome(arr, iRet))
printf("It is palindrome");
else
printf("It is not palindrome");
}
int make_array(int n, int *arr)
{
//int arr[N];
int digit,i=0;
while(n>0){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(int j=0; j<i; j++)
printf("%d ",arr[j]);
return i;
}
int palindrome(int ar[],int size)
{
int i,j;
int temp[N];
j=size;
for(i=0; i<size; i++)
temp[i]=ar[i];
for(i=0; i<size; i++){
if(temp[j-1]!=ar[i])
return 0;
j--;
}
return 1;
}
The problem is with your make array function. 问题出在您的make数组函数上。 When a function is called, the stack grows down and registers and pointers are allocated to save the point from which that function was called, now, here you send n by value and your function creates a place on the stack for an array that you fill- BUT- when your function returns- the stack pointer returns back up to the caller( if your function has a return value it will be saved in a pre-allocated place, but other than that all of the other function data on stack is unavailable.). 调用函数时,堆栈会减小,并分配寄存器和指针以保存调用该函数的点,现在,在此处按值发送n,函数将在堆栈上为要填充的数组创建一个位置-但是-函数返回时-堆栈指针返回到调用者(如果函数具有返回值,它将保存在预先分配的位置,但除此之外,堆栈上的所有其他函数数据均不可用)。
So in general, if you want a function to create an array that could be used later on it must be allocated on heap you can either return int* or send foo(int**) to the function that will hold the add. 因此,通常来说,如果您希望函数创建一个以后可以使用的数组,并且必须在堆上分配该数组,则可以返回int *或将foo(int **)发送给将保留添加内容的函数。 of the new allocated array. 新分配的数组。
another option is to allocate that array[N] in your main, and send foo(int arr[], int n, size_t size) to the function. 另一种选择是在您的main中分配该array [N],然后将foo(int arr [],int n,size_t size)发送给该函数。 Since the array was allocated by the caller in main- this memory will be valid for all of the main function life. 由于数组是由调用者在main中分配的,因此该内存将在所有main函数生命期内均有效。
so option 1) 所以选项1)
int main()
{
int num;
int* array;
printf("Enter a number to check: ");scanf("%d",&num);
array = make_array(num, N);
if(palindrome(array, N))
printf("It is palindrome");
else
printf("It is not palindrome");
free(array); /*free heap allocation */
}
int* make_array(int n, size_t size)
{
int* arr;
int digit ,i=0;
arr = malloc(sizeof(int)*size);
if(NULL == arr)
{
return NULL; /* malloc failed*/
}
while(n>0 && i<size){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(i=0; i<N; i++)
printf("%d ",arr[i]);
return arr;
}
or 2) 或2)
int main()
{
int num;
int array[N];/*array saved on stack in main function */
printf("Enter a number to check: ");scanf("%d",&num);
make_array(array,num, N);
if(palindrome(/*Don't know what should I write here*/))
printf("It is palindrome");
else
printf("It is not palindrome");
}
void make_array(int* arr, int n, size_t size)
{
int digit,i=0;
if(NULL == arr)/*if arr is not a valid pointer*/
{
return;
}
while(n>0 && i<size){
digit=n%10;
arr[i]=digit;
n/=10;
i++;
}
printf("Array: ");
for(i=0; i<N; i++)
printf("%d ",arr[i]);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.