C语言 在 C 中的单独函数中打印数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5630478/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 08:22:06  来源:igfitidea点击:

Printing Arrays in separate Function in C

carraysfunction

提问by Mike

I'm trying to print all of the values in the four arrays by sending them to a separate function. But, the problem is I can't get the function to print the all of the integers in the array because I'm not sure what I could set the condition statement in the forloop to, that would be universal to any array of any size.

我试图通过将它们发送到单独的函数来打印四个数组中的所有值。但是,问题是我无法获得打印数组中所有整数的函数,因为我不确定我可以将for循环中的条件语句设置为什么,这对任何大小的任何数组都是通用的.

Right now the function only prints the first 11 numbers. I assume that's because the first number in that array is 11.

现在该函数只打印前 11 个数字。我认为这是因为该数组中的第一个数字是 11。

    #include <stdio.h>

    void print_array(int a[]);

    void find_max(int b[]);

    void find_min(int c[]);

    void search(int d[]);

    void SORT(int e[]);

    int main(void)
    {
        int first[11] = {7,7,7,7,7,7,7,7,7,7,7};

        int second[14] = {11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2};

        int third[16] = {-2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13};

        int fourth[23] = {-3, 4, 33, 22, 9, -100, 2, 56, 57, 55, 2, 90, 2234, 32, 8, 123, 2, 33, 22, 22, 33, -1, -3}; 


        print_array(&second[0]);


        return(0);
    }

    void print_array(int a[])
    {

        int i;
        for(i=0;i<*a;i++)
        {
            printf("%d ",a[i]);

        }
    }

回答by MikeP

Pass a second argument to your function that takes the length of the array. For example:

将第二个参数传递给采用数组长度的函数。例如:

print_array(int *array, int length)
{
    for (int i = 0; i < length; i++) { /* whatever */ }
}

回答by Ilya Kogan

The function has no way of knowing when the array ends. This piece of data simply does not exist unless you pass it manually. The array is just a sequence of bytes in the memory, it has no end delimiter. So you should add a parameter to the function telling it the length of the array.

该函数无法知道数组何时结束。除非您手动传递,否则这条数据根本不存在。数组只是内存中的一个字节序列,它没有结束分隔符。所以你应该向函数添加一个参数,告诉它数组的长度。

Yep, this is how it works in C.

是的,这就是它在 C 中的工作方式。

回答by Wes Hardaker

Change the function to:

将函数更改为:

void print_array(int a[], size_t a_size) {
    int i;
    for(i=0; i< a_size;i++)
    // ...

And change the calling of the function to pass in the size:

并更改函数的调用以传入大小:

    print_array(second, sizeof(second)/sizeof(second[0]));

Which will calculate the memory size of the array (for a 4 int array on a 32 bit system it'll be 16) and divide it by the size of an int (on a 32 bit system, it's 4 bytes).

这将计算数组的内存大小(对于 32 位系统上的 4 int 数组,它将是 16)并将其除以 int 的大小(在 32 位系统上,它是 4 个字节)。

回答by DU Jiaen

in C you can make it with a function and macro:

在 C 中,您可以使用函数和宏来实现:

void printArray_(int *a, int len) {
    for (int i = 0; i < len; i++) printf("%d ", a[i]);
}

#define printArray(arr) printArray_((arr), sizeof(arr)/sizeof(arr[0]))

int main(int argc, _TCHAR* argv[])
{   
    int data[] = { 1,2,3,4 };
    printArray(data);
    return 0;
}

output:

输出:

1 2 3 4

回答by Nimo

Change this line

改变这一行

print_array(&second[0]);

To

print_array(&second);

Because, &second[0] just passes the reference to the element at 0th position,which will not be able to traverse the array.

因为,&second[0] 只是传递了对第 0 个位置元素的引用,这将无法遍历数组。

And we cannot traverse the array passed by reference without the size.As there are arrays of varied size, we can compute the size of the array by,

并且我们不能在没有大小的情况下遍历通过引用传递的数组。由于有不同大小的数组,我们可以通过以下方式计算数组的大小,

             int array_length = sizeof(array)/sizeof(array[0]);

Change the line

换线

void print_array(int a[])

To

void print_array(int *a,int array_length)

And the function of array printing will be as,

而数组打印的功能将是,

void print_array(int *a,int array_length){
    int i;
    for(i=0;i<array_length;i++){
        printf("%d ",*a);
        a++;          //for incrementing the position of array.
    }
}