1

I would like to know:

  • how to point the elements of a table to an array of elements?
  • How to make the operations on?

Here is a code that I wrote:

#include <stdio.h>
#include <stdlib.h>
#define N 4

int main(int argc, char const *argv[]) {
    int i;
    int *t[N];
    int tab1[N] = {1, 2, 3, 4},
        tab2[N] = {5, 6, 7, 8},
        tab3[N] = {9, 10,11, 12},
        tab4[N] = {13, 14, 15, 16};

    for (i = 0 ; i < N; i++) {
        *t[i] = tab1[i]; 
    }

    for (i = 0; i < N; i++) {
        printf("%d\n", *t[0]);
    }
}

When I run it, nothing is happening.

fogang@les-tatates:~/tp_304$ gcc -o test test.c
fogang@les-tatates:~/tp_304$ test
fogang@les-tatates:~/tp_304$ 

i want to implement this !

K.J Fogang Fokoa
  • 209
  • 3
  • 13

3 Answers3

1

When I run it, nothing is happening.

fogang@les-tatates:~/tp_304$ gcc -o test test.c
fogang@les-tatates:~/tp_304$ test
fogang@les-tatates:~/tp_304$ 

test is a shell build-in.

To run a program called test being located inside the current work directory do:

fogang@les-tatates:~/tp_304$ ./test
alk
  • 69,737
  • 10
  • 105
  • 255
0

From the various comments you posted I guess (and I'm really guessing here) that you maybe want this:

#include <stdio.h>
#include <stdlib.h>
#define N 4

int main(int argc, char const *argv[]) {
  int *t[N];
  int tab1[N] = { 1, 2, 3, 4 },
    tab2[N] = { 5, 6, 7, 8 },
    tab3[N] = { 9, 10,11, 12 },
    tab4[N] = { 13, 14, 15, 16 };

  t[0] = tab1;
  t[1] = tab2;
  t[2] = tab3;
  t[3] = tab4;

  for (int j = 0; j < 4; j++)
  {
    for (int i = 0; i < N; i++) {
      printf("%d ", t[j][i]);
    }

    printf("\n");
  }
}

The for loop can also be written like this:

  for (int j = 0; j < 4; j++)
  {
    int *temp = t[j];
    for (int i = 0; i < N; i++) {
      printf("%d ", temp[i]);
    }

    printf("\n");
  }

Or even simpler:

int main(int argc, char const *argv[]) {
  int t[N][N] =
  {
    { 1, 2, 3, 4 },
    { 5, 6, 7, 8 },
    { 9, 10,11, 12 },
    { 13, 14, 15, 16 }
  };

  for (int j = 0; j < 4; j++)
  {
    int *temp = t[j];
    for (int i = 0; i < N; i++) {
      printf("%d ", temp[i]);
    }

    printf("\n");
  }
}
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
0

As a complement to @Jabberwocky's answer, you can even do:

#include <stdio.h>
#include <stdlib.h>
#define N 4

int main(int argc, char const *argv[]) {
    int i;
    int tab1[N] = {1, 2, 3, 4},
        tab2[N] = {5, 6, 7, 8},
        tab3[N] = {9, 10,11, 12},
        tab4[N] = {13, 14, 15, 16};
    int *t[N] = {tab1, tab2, tab3, tab4};

  for (int j = 0; j < N; j++)
  {
    for (int i = 0; i < N; i++) {
      printf("%d ", t[i][j]);
    }

    printf("\n");
  }
}
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252