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

by Snoopy   Last Updated October 10, 2019 20:26 PM

.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
Tags : c arrays


Answers 1


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);
}
SaltyPleb
SaltyPleb
October 10, 2019 19:58 PM

Related Questions


Updated June 03, 2016 09:11 AM

Updated May 27, 2015 01:11 AM

Updated March 21, 2017 01:26 AM

Updated September 25, 2017 03:26 AM

Updated February 28, 2017 17:26 PM