468,512 Members | 1,288 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

a bit stuck- subscripted value is neither array nor pointer...

okay, i have tried,googled and read but havn't managed to figure out where the error is.

bascically,my file reads an in put file which looks like this:

Expand|Select|Wrap|Line Numbers
  1. Is 2 1 50
  2. R1 2 9 500
  3. R2 1 0 100
  4. R3 2 2 900
  5. R4 2 1 100
then it finds the max node (i.e. 9 in this case) creates a matrix by max_node,

then outputs the new matrix with the values from the input file... supposedly.

but i can't seem to get it to work at all.

the error is this:

c.c:40: error: subscripted value is neither array nor pointer.

and the file is this

Expand|Select|Wrap|Line Numbers
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3.  
  4. int main (void)
  5.  
  6. {    
  7.   char R,I,line [256];
  8.  
  9.     int i,j, node1, node2, max_node=0;
  10.     double value, **matrix, *mtx, *temp;
  11.  
  12. FILE *program1;    
  13.  
  14.   program1=fopen("text.txt", "r");
  15.  
  16.   if (program1==NULL)
  17.     {
  18.       printf("Unable to read file\n");
  19.       return (0);
  20.     }
  21.  
  22. while (fscanf(program1,"%s %d %d %lf", line, &node1, &node2, &value)!=EOF)
  23.     {    if (max_node<node1)    
  24.     {
  25.         max_node=node1;
  26.     } 
  27.         else if (max_node<node2)
  28.     {
  29.         max_node=node2;
  30.     }    
  31.     }         
  32.     printf("max_node %d\n", max_node);        // All working, Max_node correct
  33.  
  34. temp=(double *)malloc(max_node*max_node*sizeof(double));
  35. matrix=(double **)malloc(max_node*sizeof(double *));
  36.  
  37. rewind(program1);
  38. //printf("%d",node1);
  39. while (fscanf(program1,"%s %d %d %lf", line, &node1, &node2, &value)!=EOF)
  40. {    
  41.      if (line[0]=='R')
  42.       { // Resistor
  43.       printf("%d",node1);
  44.     temp[node1][node1] += 1.0/value; // Diagonal  <<---LINE 40 - ERROR HERE
  45.     temp[node2][node2] += 1.0/value; // Diagonal
  46.     temp [node1][node2] -= 1.0/value; // Off-Diagonal
  47.     temp [node2][node1] -= 1.0/value; // Off-Diagonal
  48.      }
  49.     else if (line[0]== 'I')
  50.     {    // Current Source
  51.  
  52.       mtx[node1]+=value;
  53.       mtx[node2]-=value;
  54.     }
  55.  
  56.     for (i=0;i<max_node;i++)
  57.     {
  58.         for(j=0;j<max_node;j++)
  59.         {
  60.         printf("%lf    ", temp[i][j]);
  61.         }
  62.         printf("\t%lf\n", matrix[i]);
  63.         }
  64.     }*/
  65.  
  66. }
  67.  
i hope it's clear to where the error is.

thanks, andy
Dec 6 '07 #1
1 3211
RRick
463 Expert 256MB
Your temp array is of the right size, but the wrong dimension. Temp's dimension is one, not two. To make a 2 dimensional array, you need 2 steps and a loop.
Expand|Select|Wrap|Line Numbers
  1.  
  2.  #include <stdlib.h>
  3.  
  4.  int main (void)
  5.  {   
  6.     int rows=4, cols=5;
  7.     double ** temp;
  8.  
  9.     temp = (double**) malloc( sizeof( double *) * rows);
  10.     for ( int ind=0; ind<rows; ind++)
  11.     {
  12.         temp[ ind] = (double*) malloc( sizeof( double) * cols);
  13.     }
  14. }
  15.  
Dec 6 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by No | last post: by
3 posts views Thread by Yeren | last post: by
2 posts views Thread by Bryan Field-Elliot | last post: by
reply views Thread by Mark Phillips | last post: by
4 posts views Thread by =?Utf-8?B?TWF1cg==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.