Touchpad
Touchpad

Reputation: 722

json alike objects in c

I have a background in javascript and php but and I'm currently trying to learn C however I've hit a bit of a snag, in both those languages there are objects

$obj = new stdClass(); || const obj = {};

however I've been unable to find something simulair in C, is there no such thing or am I just looking in the wrong place?

Or is there perhaps a better way to do things like this in C?

Ok I think I got the answer from Cody Gray but just to clearify a bit for everyone:

I Javascript you could do something like:

const person = {
  name: "Donald Duck",
  age: 82,
  location: "Duckburg",
};

console.log(person.name, person.age, person.location);

in php you would do something like

$person = new stdClass();

$person->name = "Donald Duck";
$person->age = 82;
$person->location ="Duckburg";

echo $person->name . " " . $person->age . " " . $person->location

my question is if/how you could do this in C

Upvotes: 0

Views: 97

Answers (3)

ulix
ulix

Reputation: 373

There is a plethora of C libraries on the web. For example, take a look at https://github.com/DaveGamble/cJSON/blob/master/README.md

Upvotes: 0

Zeta
Zeta

Reputation: 105876

You use structs. However, C is a completely other beast than those other two languages. First of all, it's statically typed: the type of all your variables and functions must be known at compile time, com­plete­ly. This also means that you cannot just go ahead and use an "object" and fill it with arbitrary many fields like in JS:

var person = {};
person.name = "Donald Duck";
person.age  = 82;              // I'm sure that Donald is always depicted younger
person.localtion = "Duckburg"; // woo hoo

Later, you may decide that a person (or rather duck) should also have a size:

person.size = 9001;

That's not possible in C, where, when you define a structure, you limit yourself to the fields in the structure, and only those fields:

struct person {
    const char * name;
    int          age;
    const char * location;
};

You can then start to create your actual persons:

struct person donald;

donald.name     = "Donald Duck";
donald.age      = 82;
donald.location = "Uncle Scrooge's Money Bin";

But if you try to add donald.size now you will get yelled at by the compiler.

That being said, C works so different on so many levels, it's going to be a wild ride. For example, you cannot go ahead and use "Donald" + "Duck" or "Donald " . "Duck"`. There is no string concatenation operator. You have to do things by hand, manage memory yourself, and keep many things in mind.

Depending on how you want to use the language, you might want to start with C++ and then delve into its belly where C still lingers. Because C does not have objects. C++ does. But keep in mind that while most C programs can be compiled in C++, they're somewhat distinct languages.

Upvotes: 3

Cody Gray
Cody Gray

Reputation: 244682

C doesn't have "objects", as it is a procedural programming language, rather than an object-oriented one.

However, it does have a way to group related data, and that is via a structure (struct keyword). This is what you may know more generically as a user-defined type. It isn't quite the same as a "class" or "object" in other languages, because it doesn't have any associated member functions (at least, not without a lot of crazy trickery that isn't really part of the language and certainly isn't idiomatic), but it is very handy nevertheless.

The syntax would be something like:

struct Person
{
    char name[100];
    int  age;
    char location[100];
};

Then, in your code, you would just do:

struct Person donaldDuck = { "Donald Duck", 82, "Duckburg" };


printf("Name: %s\nAge: %d\nLocation: %s\n",
       donaldDuck.name,
       donaldDuck.age,
       donaldDuck.location);

Notice that fields of the struct are accessed using the . operator, like many other languages.

See it online!

Upvotes: 1

Related Questions