Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a list in C using list()? (Trying to save and extract elements to/from the list)

Tags:

c

list

How can I create a list in C using list()? Not a linked list, just a regular list of elements. I am coming from python where I can just use list = []. But the only thing that turns up when I Google "list in C" are linked lists. It appears that there is a function list() that I am assuming creates a list. I cannot figure out how to extract the first and second values:

    int L = list(1,2,3,4,5);
    int a = L[0];
    int b = L[1];

I need to make a list in C to store two values, a numerator (n) and a denominator (d). I am creating a fraction calculator and I want n and d to be the two values I store in the fractions (lists). I don't want to write a program with f1 = (n1, d1) through fk = (nk, dk).

like image 250
Dan O'Connell Avatar asked Jan 17 '26 05:01

Dan O'Connell


2 Answers

The equivalent in C would be an array of elements:

int x[10];

Is an array of ints and its size = 10

Initializing an array in C can be done in several ways, for example:

int x[10] = {0,1,2,3,4,5,6,7,8,9};
int x[] = {0,1,2,3,4,5,6,7,8,9};

Or you can assign a single element:

int x[10];
x[0] = 0;
x[1] = 1;
.
.
x[9] = 9;

You should read more about arrays and how they work in c.

There is no such thing as a list in C. list is not in the C standard library, so I'm not sure where you found that. In C, you would generally use an array.

int L[] = {1,2,3,4,5};
int a = L[0];
int b = L[1];
like image 35
Michael Mior Avatar answered Jan 19 '26 18:01

Michael Mior



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!