打印一个数组,不知道大小 [英] Print an array, without knowing the size

查看:112
本文介绍了打印一个数组,不知道大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



  int printArray(int * arr,int size){

int i = 0;
while(size--){
printf(%d,,*(arr + i ++));
}

return EXIT_SUCCESS;如何在不使用尺寸

>参数?



LE我试图做这样的事情:

  int printArray(int * arr){

int i = 0;
while(*(arr + i)){
printf(%d,,*(arr + i ++));
}

return EXIT_SUCCESS;
}

但每个输出后面跟着三个未知值:( in :1 2 3 4; out :1 2 3 4 343443 -432244 324333)。

谢谢!

解决方案

您可以将地址传递给最后一个元素:

  int printArray(int * arr,int * end){

do {
printf(%d,,* arr); ((arr ++)!= end);
}

return EXIT_SUCCESS;
}


I have this function:

int printArray(int* arr, int size) {

    int i = 0;
    while (size--) {
        printf("%d, ", *(arr + i++));
    }

    return EXIT_SUCCESS;
}

How can I print the array without using the size parameter?

L.E. I tried to do something like this:

int printArray(int* arr) {

    int i = 0;
    while (*(arr + i)) {
        printf("%d, ", *(arr + i++));
    }

    return EXIT_SUCCESS;
}

but every output is followed by three unknown values: (in: 1 2 3 4; out: 1 2 3 4 343443 -432244 324333).

Thanks!

解决方案

You could pass in the address to the last element:

int printArray(int* arr, int* end) {

    do {
        printf("%d, ", *arr);
    } while( (arr++) != end);

    return EXIT_SUCCESS;
}

这篇关于打印一个数组,不知道大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆