C指针指针问题

时间:2011-04-28 20:34:35

标签: c pointers

struct instruction {
   int value;
};

int n; // Number of instructions

struct instruction **instructions; //Required to use ** and dynamic array

假设我想存储n条指令并在每条指令中存储一个值。 我怎么用**说明呢? 所以我希望以后能够从特定指令中调用一个值。

非常感谢

到目前为止,我尝试了这些,一些scanfs和动态数组创建。 因此它需要多个计数器,然后获取线程数(pthreads),然后获取每个线程内的指令数。我试图找到一种方法来存储每个线程中的指令。 **给出了结构

int
main(void) {

        scanf(" %d", &ncounters); //Ask for number of counters

        int i;

        if(counters = malloc(sizeof(struct counter)*ncounters)){
          for( i=0; i < ncounters ;i++){
            counters[i].counter = 0;
          }
        }

        scanf(" %d", &nthreads); //Ask for number of threads

        if(ninstructions = (int*) malloc(nthreads*sizeof(int))){
          for( i=0; i < nthreads ;i++){
            ninstructions[i] = 0;
          }
        }

        for(i=0; i < nthreads ;i++){

          scanf(" %d", &ninstructions[i]);  //Ask for number of instructions within threads[i]
         // Things got messy from here ...  
          instructions = malloc(sizeof(struct instruction*)*ninstructions[i]);

          for(int j=0; j < ninstructions[j] ;j++){
            instructions[j] = malloc(sizeof(struct instruction));
            int x;
            printf("Enter rep for thread %d inst %d.\n",i+1 ,j+1);
            scanf(" %d", &x);
            instructions[i][j].repetitions = x;
          }

        }

        printf(" Instruction x: %d.\n", instructions[0][0].repetitions);

//=============================================================================
// Just testing with printf
        printf("Number of counters: %d.\n", ncounters);
        printf("Number of threads: %d.\n", nthreads);

        for(i=0; i < nthreads; i++){
          printf("Thread %d has %d instructions.\n", i+1, ninstructions[i]);
        }

//=============================================================================

        free(instructions);
        free(ninstructions);
        free(counters);
    return 0;
}

我终于取得了一些进展,但在指令存储部分得到了分段错误。

struct counter *counters;

struct instruction{
  struct counter *counter;
  int repetitions;
  void (*work_fn)(long long*);
};

for(i=0; i < nthreads ;i++){

  scanf(" %d", &ninstructions[i]);  //Ask for number of instructions within threads[i]

  instructions = malloc(nthreads*sizeof(struct instruction *));

  instructions[i] = malloc(ninstructions[i]*sizeof(struct instruction));
  for(int j=0;j < ninstructions[i] ;j++){
    int Srepetition;
    char Sfunction;
    int Scounter;

    scanf(" %d %c %d", &Scounter, &Sfunction, &Srepetition);

    //Problem seems to be here "Segmentation fault" ..............

    instructions[i][j].repetitions = Srepetition;
    instructions[i][j].counter = (counters+Scounter);

    if(Sfunction == 'I'){
      instructions[i][j].work_fn(&increment);
    }else if(Sfunction == 'D'){
      instructions[i][j].work_fn(&decrement);
    }else if(Sfunction == '2'){
      instructions[i][j].work_fn(&mult2);
    }else{
      printf("error\n");
    }

    printf("Thread: %d Instruction: %d.\n", i+1, j+1);
    }
}

1 个答案:

答案 0 :(得分:2)

只需一个*足以创建一个动态数组,使用**即可创建矩阵。

struct instruction *instructions = malloc(n * sizeof(struct instruction));

/* setting some random values */
for (int i=0;i<n;i++)
     instructions[i]->value = i;

/* accessing values */
for (int i=0;i<n;i++)
     printf("instruction %d value %d\n",i,instructions[i]->value);

/* don't forget to free */
free(instructions);

请查找有关dynamic arrays in C的参考资料,以便进行更多调查。例如this one

修改

...如果你真的需要矩阵,这就是等效的代码:

int n,z; // for number cols and rows

struct instruction **instructions = malloc(n * sizeof(struct instruction *));

/* setting some random values */
for (int i=0;i<n;i++) {
     instructions[i] = malloc(z * sizeof(struct instruction));
     for (int j=0;j<z;j++) 
         instructions[i][j]->value = i * j;
}
/* accessing values */
for (int i=0;i<n;i++) {
     for (int j=0;j<z;j++) 
          printf("instruction %d,%d value %d\n",i,j,instructions[i][j]->value);
}

/* don't forget to free */ 
for (int i=0;i<n;i++)
     free(instructions[i]);
free(instructions);