Setting and getting vertex attributes in C

This compiles and runs, but I cannot get the expected results:

#include <igraph/igraph.h>
#include <vector>

using namespace std;

int main(int argc, char *argv[]) {
        igraph_vector_int_t gtypes, vtypes, etypes;
        igraph_strvector_t gnames, vnames, enames;

        igraph_vector_int_init(&gtypes, 0);
        igraph_vector_int_init(&vtypes, 0);
        igraph_vector_int_init(&etypes, 0);
        igraph_strvector_init(&gnames, 0);
        igraph_strvector_init(&vnames, 0);
        igraph_strvector_init(&enames, 0);

        igraph_set_attribute_table(&igraph_cattribute_table);

        igraph_t g;
        igraph_vector_int_t v;
        igraph_vector_int_init( &v, 10 );
        VECTOR(v)[0] = 0;
        VECTOR(v)[1] = 1;
        VECTOR(v)[2] = 0;
        VECTOR(v)[3] = 2;
        VECTOR(v)[4] = 0;
        VECTOR(v)[5] = 3;
        VECTOR(v)[6] = 1;
        VECTOR(v)[7] = 3;
        VECTOR(v)[8] = 2;
        VECTOR(v)[9] = 4;

        igraph_create(&g,&v,0,IGRAPH_UNDIRECTED);
        /* Add names */
        igraph_vector_t names;
        igraph_vector_init_range(&names, 0, igraph_vcount(&g));
        for ( int i=0; i<igraph_vcount(&g); i++ ) {
                VECTOR(names)[i] = i;
        }
        printf( "%s\n", VECTOR(names)[0] );
        SETVANV( &g, "name", &names );

        /* Can you plot it? */
        igraph_matrix_t coords;
        igraph_matrix_init(&coords, 0, 0);
        igraph_layout_reingold_tilford(&g, &coords, IGRAPH_ALL, 0, 0);
        int n=igraph_vcount(&g);
        for (long i=0; i<n; i++) {
                printf("%s %6.3f %6.3f\n", VAN(&g, "name", i), MATRIX(coords, i, 0), MATRIX(coords, i, 1));
        }

        igraph_vector_int_destroy( &v );
        igraph_destroy( &g );
}

This gives the following results:

$ ./MRE_graphNamedVertices

name  0.000  0.000
name  1.000 -1.000
name  2.000  0.000
name  3.000  1.000
name  4.000  0.000
$

Any help would be appreciated.

I see compiler warnings about serious errors, and the program crashes when run (as expected, given the errors). Can you correct these first, or ask if it’s not clear to you what needs to be corrected?

igraph_test.cpp:40:25: warning: format specifies type 'char *' but the argument has type 'igraph_real_t' (aka 'double') [-Wformat]
        printf( "%s\n", VECTOR(names)[0] );
                 ~~     ^~~~~~~~~~~~~~~~
                 %f
/opt/local/include/igraph/igraph_vector_pmt.h:69:19: note: expanded from macro 'VECTOR'
#define VECTOR(v) ((v).stor_begin)
                  ^
igraph_test.cpp:49:44: warning: format specifies type 'char *' but the argument has type 'igraph_real_t' (aka 'double') [-Wformat]
                printf("%s %6.3f %6.3f\n", VAN(&g, "name", i), MATRIX(coords, i, 0), MATRIX(coords, i, 1));
                        ~~                 ^~~~~~~~~~~~~~~~~~
                        %f
/opt/local/include/igraph/igraph_attributes.h:468:24: note: expanded from macro 'VAN'
#define VAN(graph,n,v) (igraph_cattribute_VAN((graph), (n), (v)))
                       ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2 warnings generated.

Once the errors have been corrected, can you show what results you expect, what you actually get, and point out the relevant difference?

Some more tips, which have nothing to do with the issues you see, but are good practice:

  • #include <igraph.h> instead of #include <igraph/igraph.h>
  • When indexing into igraph data structures (vectors, matrices, vertex or edge IDs), always use igraph_integer_t, not int or long.
  • Every init function should have a corresponding destroy to avoid memory leaks
  • Easy way to create small test graphs is igraph_small()