C data structure to python dictionary

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
vorn10
Posts: 1
Joined: Wed Feb 27, 2019 6:02 pm

Re: C data structure to python dictionary

Post by vorn10 » Wed Feb 27, 2019 6:04 pm

How does one construct and access a set of key-value pairs in C? To use a silly simple example, let's say I want to create a table which translates between an integer and its square root.

If I were writing javascript, I could just do this:

var squareRoots = {
4: 2,
9: 3,
16: 4,
25: 5
}
and then access them like:

var squareRootOf25 = squareRoots[5]
How do I do this in C? What if I want to use one type of enum as the key and another type of enum as the value?

User avatar
dhylands
Posts: 3821
Joined: Mon Jan 06, 2014 6:08 pm
Location: Peachland, BC, Canada
Contact:

Re: C data structure to python dictionary

Post by dhylands » Wed Feb 27, 2019 7:39 pm

C doesn't have a dictionary type data structure, so you need to create one. This means you need to define your own structure and create functions for initializing, adding, removing, looking up etc, and the call those functions.

In the context of MicroPython, you could use the micropython routines for doing this.

It really depends on what you're trying to accomplish.

Post Reply