简体   繁体   English

如何在C中将数组传递给函数

[英]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.

相关问题 如何将字符串数组传递给 C 中的另一个函数 - How to pass arrays of strings to another function in C C 指针,了解如何将 arrays 传递给 function - C Pointers, understanding how to pass arrays to function 如何在C(首选)/ C ++中顺序将一组1D数组传递给函数 - How to sequentially pass a set of 1D arrays to a function, in C(preferred)/C++ 如何将两个数组传递给一个参数中的函数? - How to pass two arrays to a function in one argument? 如何通过引用将字符数组传递给函数? - How to pass array of char arrays to function by reference? 将带有数组的回调函数从 C# 传递到 C DLL - Pass a callback function with arrays from C# to C DLL 为什么在C和C++中不允许将arrays传值给function? - Why it is not allowed to pass arrays by value to a function in C and C++? 如何将数组传递到C中的函数(在Fedora上)? - How to pass arrays into functions in C ( on Fedora )? 我如何将 2 arrays 传递给 function,让用户在那些 2 arrays 中写一些东西,然后将它们都返回给 C 中的程序? - How do I pass 2 arrays into a function, make the user write something in those 2 arrays, and return both of them to the program in C? 如何通过函数将指针传递给c中未声明的可修改1 / 2D数组并保持其值? - How do I pass pointers to undeclared modifiable 1/2D arrays in c through a function and keep their values?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM