# How to include edge attributes when calling add\_edges()?

**URL:** <https://igraph.discourse.group/t/how-to-include-edge-attributes-when-calling-add-edges/1511>\
**Category:** Usage\
**Tags:** C\
**Created:** [16 March 2023 23:25 UTC](https://igraph.discourse.group/t/how-to-include-edge-attributes-when-calling-add-edges/1511 "2023-03-16T23:25:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![eroubenoff](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/eroubenoff/32/916_2.png) [@eroubenoff](https://igraph.discourse.group/u/eroubenoff)\
**Post date:** [16 March 2023 23:25 UTC](https://igraph.discourse.group/t/how-to-include-edge-attributes-when-calling-add-edges/1511/1 "2023-03-16T23:25:52Z")

</div>

My program contains a dynamic graph, and edges of various kinds are added and removed over the lifecycle of the program. I would like to be able to include edge attributes in the same call as `add_edges`. I can see that this is possible from the documentation, however I can’t quite figure out how to do this. Of course, can add the attribute in after adding the edges, but I think it would streamline things considerable to do all at once

Could someone point me to an example or to the appropriate documentation?

Thanks!

 ![Screen Shot 2023-03-16 at 7.24.02 PM](https://global.discourse-cdn.com/free1/uploads/igraph/original/1X/9d8698fde2f9f7ca0e1019a581284570df66d6e1.png)

---

<div class="post-metadata">

**Author:** ![szhorvat](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/szhorvat/32/3_2.png) [@szhorvat](https://igraph.discourse.group/u/szhorvat)\
**Post date:** [17 March 2023 08:08 UTC](https://igraph.discourse.group/t/how-to-include-edge-attributes-when-calling-add-edges/1511/2 "2023-03-17T08:08:09Z")

</div>

The documentation is indeed quite sparse on using attributes. A partial rework of attribute handling is one of the big ticket items for version 0.11, though the work hasn’t started yet. The docs should be improved at the same time.

For now I recommend that you look at some of the import/export functions for usage examples, for example `igraph_read_graph_gml()`. While it’s not the simplest reader, here you can follow the relevant part of the code without having to look into Bison source files. [igraph/gml.c at master · igraph/igraph · GitHub](https://github.com/igraph/igraph/blob/master/src/io/gml.c#L696)

In short:

- `attr` must be a pointer to an `igraph_vector_ptr_t`, where each entry points to an attribute record.
- Attribute records are represented as `igraph_attribute_record_t`, with the following fields:
  - `name` is a null-terminated string, the attribute name
  - `type` should be one of `IGRAPH_ATTRIBUTE_NUMERIC`, `IGRAPH_ATTRIBUTE_BOOLEAN` or `IGRAPH_ATTRIBUTE_STRING`
  - `value` will be a pointer to corresponding igraph vector type, i.e. `igraph_vector_t`, `igraph_vector_bool_t` or `igraph_strvector_t`. It must have an appropriate length, matching the number of vertices for vertex attribute, the number of edges for edge attributes, or 1 for graph attributes.

- All the data passed in through `attr` will be copied. In other words, its ownership does not change. You still own the data and you are responsible for freeing it after the `igraph_add_edges()` call.

---

<div class="post-metadata">

**Author:** ![eroubenoff](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/eroubenoff/32/916_2.png) [@eroubenoff](https://igraph.discourse.group/u/eroubenoff)\
**Post date:** [17 March 2023 12:41 UTC](https://igraph.discourse.group/t/how-to-include-edge-attributes-when-calling-add-edges/1511/3 "2023-03-17T12:41:49Z")

</div>

Thank you - this is extremely helpful! I will try to implement this later today, and will write back with any questions. I appreciate your time.
