C Pointer Pointer Question -


struct instruction {    int value; };  int n; // number of instructions  struct instruction **instructions; //required use ** , dynamic array 

say want store n number of instructions , store value in each instructions. how **instructions?? want able call value specific instruction later.

many thanks

so far tried these, few scanfs , dynamic array creating. takes number of counters, takes number of thread (pthreads), takes number of instructions within each thread. trying find way store instructions in each thread. ** structure given

int main(void) {          scanf(" %d", &ncounters); //ask number of counters          int i;          if(counters = malloc(sizeof(struct counter)*ncounters)){           for( i=0; < ncounters ;i++){             counters[i].counter = 0;           }         }          scanf(" %d", &nthreads); //ask number of threads          if(ninstructions = (int*) malloc(nthreads*sizeof(int))){           for( i=0; < nthreads ;i++){             ninstructions[i] = 0;           }         }          for(i=0; < nthreads ;i++){            scanf(" %d", &ninstructions[i]);  //ask number of instructions within threads[i]          // things got messy 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 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);  //============================================================================= // testing printf         printf("number of counters: %d.\n", ncounters);         printf("number of threads: %d.\n", nthreads);          for(i=0; < nthreads; i++){           printf("thread %d has %d instructions.\n", i+1, ninstructions[i]);         }  //=============================================================================          free(instructions);         free(ninstructions);         free(counters);     return 0; } 

i got progress, getting segmentation fault @ instruction storing part.

struct counter *counters;  struct instruction{   struct counter *counter;   int repetitions;   void (*work_fn)(long long*); };  for(i=0; < nthreads ;i++){    scanf(" %d", &ninstructions[i]);  //ask 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 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);     } } 

with 1 * enough create dynamic array, ** creating matrix.

struct instruction *instructions = malloc(n * sizeof(struct instruction));  /* setting random values */ (int i=0;i<n;i++)      instructions[i]->value = i;  /* accessing values */ (int i=0;i<n;i++)      printf("instruction %d value %d\n",i,instructions[i]->value);  /* don't forget free */ free(instructions); 

please references dynamic arrays in c investigate more. instance this one

edit

... if need matrix equivalent code:

int n,z; // number cols , rows  struct instruction **instructions = malloc(n * sizeof(struct instruction *));  /* setting random values */ (int i=0;i<n;i++) {      instructions[i] = malloc(z * sizeof(struct instruction));      (int j=0;j<z;j++)           instructions[i][j]->value = * j; } /* accessing values */ (int i=0;i<n;i++) {      (int j=0;j<z;j++)            printf("instruction %d,%d value %d\n",i,j,instructions[i][j]->value); }  /* don't forget free */  (int i=0;i<n;i++)      free(instructions[i]); free(instructions); 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -