C指针指针问题

发布于 2024-11-03 19:12:01 字数 3230 浏览 0 评论 0原文

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);
    }
}
struct instruction {
   int value;
};

int n; // Number of instructions

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

Say I want to store n number of instructions and store a value in each instructions.
How would I do that with the **instructions??
So I want to be able to call a value from a specific instruction later.

Many Thanks

So far I tried these, a few scanfs and dynamic array creating.
So it takes number of counters, then takes number of thread (pthreads), then takes number of instructions within each thread. I am trying to find a way to store the instructions in each thread. ** structure is given

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;
}

I finally got some progress, but getting segmentation fault at the instruction storing part.

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

温暖的光 2024-11-10 19:12:01

只需一个 * 就足以创建一个动态数组,使用 ** 您将创建一个矩阵。

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);

请查找有关C 中的动态数组的参考资料以了解更多信息。例如 这个

编辑

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

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);

With just one * is enough to create a dynamic array, with ** you would be creating a matrix.

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);

Please look for references about dynamic arrays in C to investigate more. For instance this one

edit

... if you really need matrix this is the equivalent code:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文