Jguillot Jguillot - 1 year ago 90
C Question

Repititives tasks and for loop

I've to check one byte in 200 tables.
I don't want to write 200 line to check it.
Do you have any tips to do it with a for loop?

My example of code :

#include <stdio.h>
#include "CUnit/Basic.h"
#include "CUnit/Console.h"
#include "CUnit/Automated.h"
#include "CUnit/CUCurses.h"
#include "CUnit/TestDB.h"

void _testu(){
CU_ASSERT_EQUAL(55, my_tab_0[5]);
CU_ASSERT_EQUAL(55, my_tab_1[5]);
CU_ASSERT_EQUAL(55, my_tab_2[5]);
CU_ASSERT_EQUAL(55, my_tab_3[5]);
CU_ASSERT_EQUAL(55, my_tab_n[5]);

Answer Source

If you have n independant arrays, you could try to build an array of pointers:

char *my_tab[] = { my_tab_0, my_tab_1, m__tab_2, ... my_tab_n };

you can they simply do:

void _testu(){
    int i;
    for(i=0; i<=n; i++) {
        CU_ASSERT_EQUAL(55, my_tab[i][5]);

It mainly make sense if you have more than one loop using it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download