Snoopy
Snoopy

Reputation: 71

How to print each element from array of unknown length in C

.I have a big array of coordinates that looks like this

triangle_t teapot_model[] = {
{
.x1=5,
.y1=10,
},
{
.x1=20,
.y1=30,
},
(keeps going)

How can I print all of the items in this array without knowing their position? I want this output:

Output:
.x1=5 y1=10
.x1=20 .y1=30

Upvotes: 1

Views: 693

Answers (1)

SaltyPleb
SaltyPleb

Reputation: 353

Array in C always has a size although implicit in your case.

To simply print each element of your array, using the below code should suffice

int sizearray = sizeof teapot_model  / sizeof *teapot_model;

for (int i = 0; i < sizearray; i++) 
{
    printf(".x1=%d .y1=%d\n", teapot_model[i].x1, teapot_model[i].y1);
}

Upvotes: 4

Related Questions