# How to print a vector

**URL:** <https://igraph.discourse.group/t/how-to-print-a-vector/1597>\
**Category:** Usage\
**Tags:** C\
**Created:** [23 June 2023 05:12 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597 "2023-06-23T05:12:32Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pei\_Chang](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/pei_chang/32/958_2.png) [@Pei\_Chang](https://igraph.discourse.group/u/Pei_Chang)\
**Post date:** [23 June 2023 05:12 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597/1 "2023-06-23T05:12:32Z")

</div>

Hi, I am new to both igraph an C/C++. Recently, I found there’s a problem with my code that I cannot print out the vector I want.

> ```auto
> int main() {
> igraph_t graph;
> igraph_vector_int_t neis;
> 
> // create a graph
> igraph_empty(&graph, 6, IGRAPH_UNDIRECTED);
> igraph_add_edge(&graph, 0, 1);
> igraph_add_edge(&graph, 0, 2);
> igraph_add_edge(&graph, 0, 3);
> igraph_add_edge(&graph, 1, 4);
> igraph_add_edge(&graph, 1, 5);
> 
> // get the neighbors of vertex 0
> igraph_neighbors(&graph, &neis, 0, IGRAPH_ALL);
> 
> // print out the vector
> for (int i = 0; i < igraph_vector_size(&neis); i++)
> {
> printf(" %d ", (int)igraph_vector_e(&neis, i));
> }
> printf("\n");
>   
>     
> igraph_destroy(&graph);
> 
> return 0;
> }
> 
> ```

Terminal showed error:  
\*_error C2664: 'igraph\_real\_t igraph\_vector\_e(const igraph\_vector\_t ,igraph\_integer\_t)’_

I don’t know what I did wrong. How to print out all of the neighbors of vertex 0?

---

<div class="post-metadata">

**Author:** ![Pei\_Chang](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/pei_chang/32/958_2.png) [@Pei\_Chang](https://igraph.discourse.group/u/Pei_Chang)\
**Post date:** [23 June 2023 06:06 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597/2 "2023-06-23T06:06:03Z")

</div>

I found another problem. If I type **igraph\_neighbors(&graph, &neis, 0, IGRAPH\_ALL);** in the code, the terminal would not show errors, but any outcome will disappear.

Like this,

> ```auto
> int main() {
> igraph_t graph;
> igraph_vector_int_t neis;
> 
> // create a graph
> igraph_empty(&graph, 6, IGRAPH_UNDIRECTED);
> igraph_add_edge(&graph, 0, 1);
> igraph_add_edge(&graph, 0, 2);
> igraph_add_edge(&graph, 0, 3);
> igraph_add_edge(&graph, 1, 4);
> igraph_add_edge(&graph, 1, 5);
> 
> // get the neighbors of vertex 0
> igraph_neighbors(&graph, &neis, 0, IGRAPH_ALL);
> 
> cout<<"Hello"<<endl;
>   
>     
> igraph_destroy(&graph);
> 
> return 0;
> }
> 
> ```

It will not print Hello in the terminal. However, if I delete **igraph\_neighbors(&graph, &neis, 0, IGRAPH\_ALL);** there will be **Hello** in terminal.

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [23 June 2023 07:13 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597/3 "2023-06-23T07:13:02Z")

</div>

In C, every data structure will need to be explicitly initialised and freed.

In this case, you need to make sure to first initialise the vector `neis` by doing

```auto
    igraph_vector_int_init(&neis, 0);

```

The second argument is the length of the vector that is allocated. The actual length of the vector does not matter much, since the `igraph_neighbours` function will properly resize the vector as needed. However, it does require the vector data structure to be initialised before use.

After use, you can destroy the vector using ` igraph_vector_int_destroy()`.

You may want to take a closer look at [the tutorial](https://igraph.org/c/html/latest/igraph-Tutorial.html#tut-lesson-2).

---

<div class="post-metadata">

**Author:** ![Pei\_Chang](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/pei_chang/32/958_2.png) [@Pei\_Chang](https://igraph.discourse.group/u/Pei_Chang)\
**Post date:** [23 June 2023 11:35 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597/4 "2023-06-23T11:35:04Z")

</div>

Thank you! My second problem was solved. But the first problem still exists.

I cannot print out all of the neighbors of vertex 0 with the code below

> ```auto
> for (int i = 0; i < igraph_vector_size(&neis); i++)
> {
> printf(" %d ", (int)igraph_vector_e(&neis, i));
> }
> printf("\n");
> 
> ```

What’s wrong with my code?

---

<div class="post-metadata">

**Author:** ![vtraag](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/vtraag/32/38_2.png) [@vtraag](https://igraph.discourse.group/u/vtraag)\
**Post date:** [23 June 2023 12:22 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597/5 "2023-06-23T12:22:49Z")

</div>

Note that the type is `igraph_vector_int_t` not `igraph_vector_t` (note the **`int`** ). Hence, you should use `igraph_vector_int_size` and `igraph_vector_int_e`.

Note that `igraph_vector_int_e` is deprecated, and that it is replaced by `igraph_vector_int_get`. However, in most cases, you should use `VECTOR(neis)[i]` instead of `igraph_vector_int_get(&neis, i)`. Note that you use `neis` when using `VECTOR`, _not_ `&neis`. You can read more about this in the [tutorial.](https://igraph.org/c/html/latest/igraph-Tutorial.html#tut-lesson-2)

---

<div class="post-metadata">

**Author:** ![Pei\_Chang](https://yyz2.discourse-cdn.com/free1/user_avatar/igraph.discourse.group/pei_chang/32/958_2.png) [@Pei\_Chang](https://igraph.discourse.group/u/Pei_Chang)\
**Post date:** [23 June 2023 19:14 UTC](https://igraph.discourse.group/t/how-to-print-a-vector/1597/6 "2023-06-23T19:14:09Z")

</div>

Thank you! That really helps a lot!
