如何美元P c ^ $ ptty印刷阵列 [英] How to pretty-print arrays in C

查看:138
本文介绍了如何美元P c ^ $ ptty印刷阵列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有这样的疑问,我将打印数组。结果
输出应该是这样的:

So I have this question where I shall print arrays.
The output should be like this:

{element0,中element1,element2的}

{element0, element1, element2}

所以我写了这个:

#include "stdio.h"

main()
{

    int a[10]= {1, 0, 3, 10, 20, 0, 7, 8, 15, 14};
    int i;

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

好了,好如何在世界上我可以在同一行,他们都应该是插图中大括号,并用逗号隔开的写出所有的数字。我想你应该使用一些kindof指针指向所有的数字。但是,我是新来这个,从来没有认识到这和它相当棘手..

Ok, so well how in the world can I write out all the numbers in one line, where they are supposed to be inbetween curly brackets and spaced by commas. I assume you are supposed to use some kindof pointer that points to all the numbers. But I am new to this and have never learnt this and its quite tricky..

是的,但我想我应该使用指针如果我没有弄错的。

Yes but i think i am supposed to use pointers if I am not mistaken.

推荐答案

或多或少一行一行在剖析问题的code。

Analysis

Dissecting the code in the question more or less line by line.

#include "stdio.h"

该标准使用的#include&LT;&stdio.h中GT; ;可要知道足够的关于C并不需要你使用任何其他事情之前要问这个问题。

The standard uses #include <stdio.h>; you had better know enough about C not to need to ask this question before you use anything else.

main()

这一直没有合法的标准C自1999年以来(对于所有的一些C编译器还接受无whingeing)。你应该总是声明的返回类型。我会使用 INT主要(无效),因为这样,我没有从我的默认编译器选项错误,但你可以使用 INT主( )

This has not been legitimate standard C since 1999 (for all that some C compilers still accept it without whingeing). You should always declare the return type. I'd use int main(void) because that way I don't get errors from my default compiler options, but you could use int main().

{
    int a[10]= {1, 0, 3, 10, 20, 0, 7, 8, 15, 14};
    int i;

尽管上面的code是OK,一C99编译器允许你写的for(int i = 0;我小于10;我++)并忽略定义 I 循环之前。

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

正如其他人指出的那样, A [10] 是错误的。这是错误的,因为它应该是 A [I] ,因为 A [10] 超出的结束数组,因此调用未定义的行为。

As pointed out by others, the a[10] is wrong. It is wrong because it should be a[i], and because a[10] is beyond the end of the array, so you invoke undefined behaviour.

}

您不能确保出现一个换行,你不状态返回到环境中。如果你写的C89 code,你应该有返回0; 结尾(但你可以用的正本报关主要脱身( ))。如果你没有返回0; 末使用由C99标准的恩典,你必须有 INT主要(无效)或顶部类似。你不能两者兼得一次。

You don't ensure that a newline appears, and you don't return a status to the environment. If you write C89 code, you should have return 0; at the end (but you can get away with the original declaration of main()). If you don't have return 0; at the end to use the grace given by the C99 standard, you must have int main(void) or similar at the top. You can't have it both ways at once.

问题进行了更新,以表明指针是要使用和如果(我!= 0)可能不是循环体允许入内。在这种情况下,我可能会写:

The question was updated to indicate that pointers are to be used and that if (i != 0) probably isn't allowed inside the body of the loop. In that case, I'd probably write:

#include <stdio.h>

int main(void)
{
    int a[10] = { 1, 0, 3, 10, 20, 0, 7, 8, 15, 14 };
    int *p = &a[0];
    int *end = &a[10];
    char *pad = "{ ";

    while (p < end)
    {
        printf("%s%d", pad, *p++);
        pad = ", ";
    }
    printf(" }\n");
    return 0;
}

这是合法的,形成 A [10] ,并用它来比较指针,但它是不合法的取消对它的引用(使用<$ C $的地址C> A [10] )。在code放置后的空间 {和前} ,因为这就是我喜欢出现在数据。如果你不想要的空间存在,省略了空间。如果你想在逗号后没有空间,忽略这一点。我找到垫技术相当有用,在这些情况下经常使用它。可以牛肉上印刷code键处理具有被分割多行更大阵列(尽管这是更容易使用数组索引比使用指针),以及其他类似的变型。

It is legitimate to form the address of a[10] and to compare pointers with it, but it is not legitimate to dereference it (to use a[10]). The code places a space after the { and before the } because that's how I like the data to appear. If you want no space there, omit the space. If you want no space after the commas, omit that too. I find the 'pad' technique quite useful and use it quite often in these scenarios. You can beef up the printing code to handle bigger arrays that have to be split over multiple lines (though that is easier using array indexes than using pointers), and other similar variants.

示例输出:

{ 1, 0, 3, 10, 20, 0, 7, 8, 15, 14 }

如果您有多线路输出,你可能希望有良好的对齐的数字。在这种情况下,你必须使用一个转换规范,如 2D%来获得是2位数宽列右对齐的所有数字。

If you have multi-line output, you might want to have the numbers nicely aligned. In that case, you have to use a conversion specification such as %2d to get all numbers right aligned in columns that are 2 digits wide.

一个更通用的解决方案将使用一个函数来完成打印,更是这样的:

A more general solution would use a function to do the printing, more like this:

#include <stdio.h>

static void print_array(int *array, int size)
{
    int *p = array;
    int *end = array + size;
    char *pad = "{ ";

    while (p < end)
    {
        printf("%s%d", pad, *p++);
        pad = ", ";
    }
    printf(" }\n");
}

int main(void)
{
    int a[10] = { 1, 0, 3, 10, 20, 0, 7, 8, 15, 14 };
    print_array(a, 10);
    print_array(a, 5);
    return 0;
}

这篇关于如何美元P c ^ $ ptty印刷阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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