1

Am I forced to use loops and realy long lines or is something like this possible in c?

int a[5] = {1, 2, 3, 5, 8};
printf("%d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4]); // this is ok but
printf("%d %d %d %d %d\n", a);                // is this somehow possible??

I am asking if there is a way.

3
  • Just try it! You will see it if you get an error or what result you get Commented Nov 29, 2014 at 12:21
  • I did and I googled also but I get answers regarding expandable arrays.. Commented Nov 29, 2014 at 12:22
  • 1
    It is not possible to know its size because it receives as a pointer in the function passed the array. So it is impossible. Commented Nov 29, 2014 at 12:27

3 Answers 3

3

Point 1.

printf("%d %d %d %d %d\n", a[0], a[1], a[2], a[3], a[4]);

Perfectly ok, but think of the horror when you have, say 10,000 elemnts.

Point 2.

printf("%d %d %d %d %d\n", a); 

Problem with basic concept. You did not supply enough values to print.

Baseline: Stick to loops while printing array elements one by one.

Sign up to request clarification or add additional context in comments.

1 Comment

Nice answer. Just adding that if you need that logic in several places, simply make your own print_array(const int * array, size_t length) function.
0

This line:

printf("%d %d %d %d %d\n", a); 

is actually just plain undefined behavior because 1) the format string expects 5 arguments, but you only provide one and 2) it expects a %d but you provide an array.

There isn't a built-in way to output the contents of an array, except for character arrays. For %s, it will print up to the terminating null byte or up to the maximum amount of characters specified by the precision. Even if you were able to provide the size of the array, the printf specification doesn't support it. Just use a loop.

Comments

-1

Thy the following trick

#include <stdio.h>

int main(void) 
{
    struct { int a[5]; } A = { { 1, 2, 3, 5, 8 } };

    printf( "%d %d %d %d %d\n", A.a[0], A.a[1], A.a[2], A.a[3], A.a[4] );
    const char *f = "%d %d %d %d %d\n";
    printf( f, A );                 

    return 0;
}

The output is

1 2 3 5 8
1 2 3 5 8

8 Comments

I'm not entirely sure what kind of trick you are pulling off with this. It looks to me like you are using the ABI of your machine to achieve what the C standard does not allow you to do. Have I missed something? An explanation of how you achieve this effect would be nice.
@cmaster Yes you are missed. This is entirely valid code. The stack contains five integer numbers. If you will find a machine where the result will be different then let me know.:)
So it's really ABI programming. And yes, I can give you a system configuration where this fails miserably: An AMD64 CPU, 64 bit linux, gcc gives this output in the second line: 511975424 8 4195931 1 1. And I'm pretty certain that this is not a compiler bug but simple and plain undefined behavior.
@cmaster It would be good if you llok the assembler code and how the data are placed in the stack. That is what is the reason of such a result.
Well, in my ABI arguments are passed in registers if they are not part of a struct. It's clear why your program fails so miserably on my machine. But actually, I don't really care how my compiler places this object on the stack, or how it passes arguments (as long as it uses the fastest method possible). This trick relies on stuff that is not part of the C standard. Consequently it's not portable at best and should never be used.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.