C: odd behaviour with nested loop and array -



C: odd behaviour with nested loop and array -

i have array of int terminated '\0' created elsewhere in code. know it's null terminated because i've tested it.

say, example, array [7, 8, 9, 11, 12, '\0']

when feed function:

int edgesincluster(graph *g, int *nodes) { int count = 0; int = 0; int j = 0; while(nodes[i] != '\0') { while(nodes[j] != '\0') { if(i<j) { printf("%i %i\n", nodes[i], nodes[j]); count += isneighbour(g, nodes[i], nodes[j]); } j++; } i++; } homecoming count; }

the printf outputting:

7 7 7 8 7 9 7 11 7 12

when should outputting:

7 8 7 9 7 11 7 12 8 9 8 11 8 12 9 11 9 12 11 12

which means reason either 'i' isn't beingness incremented (but can see is) or nodes[0] == '\0' know isn't true since loop 'j' works fine.

so, ideas what's going on?

p.s. when alter whiles loops, works if know length of 'nodes'

you don't reset j after inner loop.

but why such horribly verbose code? seek this:

size_t count = 0; (size_t = 0; nodes[i]; ++i) { (size_t j = + 1; nodes[j]; ++j) { printf("%i %i\n", nodes[i], nodes[j]); count += isneighbour(g, nodes[i], nodes[j]); } } homecoming count;

(if want strict ansi c89, have pull declarations of i , j out of loops of course.)

also note '\0' char-literal of value 0, might well, , more correctly, 0. , instead of if (x != 0) can if (x), did in loops.

c arrays loops

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -