C 数组上的双指针和指向表的表元素

C 数组上的双指针和指向表的表元素,c,arrays,pointers,C,Arrays,Pointers,我想知道: 如何将表的元素指向元素数组 如何在上进行操作 以下是我编写的代码: #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[

我想知道:

  • 如何将表的元素指向元素数组
  • 如何在上进行操作
以下是我编写的代码:

#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]);
    }
}

从您发布的各种评论中,我猜(我真的在这里猜)您可能想要:

#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");
  }
}
或者更简单:

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");
  }
}
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 }
};
对于(int j=0;j<4;j++)
{
int*temp=t[j];
对于(int i=0;i
作为@Jabberwocky答案的补充,您甚至可以:

#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");
  }
}
#包括
#包括
#定义n4
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};
对于(int j=0;j
当我运行它时,什么都没有发生

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

要运行位于当前工作目录内的名为
test
的程序,请执行以下操作:

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

对我来说,它做了一些事情:它崩溃了。请回答你的问题并透露你期望的输出。@fogangfokoa好吧,如果你认为你有一个很好的理由的话。。。。1) 必须删除第二个循环,并将其替换为
t[0]=tab1;t[1]=tab2
并使用
t[0][0]
t[0][1]
等形式访问元素,最后一个循环可以是
printf(“%d\n”,t[2][i])
*t[i]=tab1[i]
您没有为存储在
t
中的指针分配任何内存。取消引用并为其分配某些内容是可以的。我希望:t[0]包含tab1。你明白我的意思吗@Jabberwocky//printf(“%d”,temp[i]);printf(“%d”,*temp[i])的工作方式不同@fogangfokoa抱歉,您的评论不清楚(作为整个问题的补充)。这就是我所做的,我们已经了解到,问题是数组的打印数据。此行:printf(“%d”,t[i][j]);不管用。这正是我想做的!你能告诉我什么是不工作吗?我在这里按列获取值。你想要什么?当我编译并运行.out时,我没有得到值。
fogang@les-tatates:~/tp_304$ ./test