开发者

pulling values from pointers in a loop

开发者 https://www.devze.com 2023-01-18 08:45 出处:网络
getLine is a function that gets a line, I\'m trying to combine lines together outside the getLine function. When ever I try doing this in a loop it messes up the output. I bet it has to do with the po

getLine is a function that gets a line, I'm trying to combine lines together outside the getLine function. When ever I try doing this in a loop it messes up the output. I bet it has to do with the pointers, but I have spend many hours trying to figure it out.

  int num;
  int matrix[370];

  i=1;
  j=0;
  while(*(point=getLine(infile)) != -2){ 
      n[j]=*point;
      if(n[0] != n[j]){
        printf("matrix dim error 1");
        break;
      }
      while (i<=n[j]){
        matrix[i+(3*j)] = *(point+(i+(3*j)));
        i++;
        printf("%d", matrix[i+(3*j)]);
      }
      printf("%d %d %d\n", matrix[1],matrix[2],matrix[3]);
      j++;开发者_如何学JAVA
  }
  fclose( infile );
}

int *getLine(FILE *infile){

  int l=0;
  int line[7];
  int i=1;
  int *point;

  while ((l=getNum(infile)) != -1){
    if(l==EOF){
      line[0]=EOF;
      point = &line[0];
      return(point);
    }
    line[i]=l;
    i++;
  }
  if(i==1){
    line[0]=-2;
    point = &line[0];
    return(point);
  }
  line[0]=(i-1); //stores the length of the line in first space
  printf("%d %d %d\n",line[1],line[2],line[3]);
  point = &line[0];
  printf("%d\n",*point);
  return(point);
}

int getNum(FILE *infile) {
  int c=0;
  int value=0;


  while ((c=fgetc(infile)) != '\n') {
    if(c==EOF){
      return(EOF);
    }
    if((c==32)||(c==13)){
      if(value != 0){ //Making sure a number has been gotten
        //printf("%d\n\n", value);
        return(value);
      }
      //otherwise keep getting characters
    }
    else if ((c<=47)||(c>=58)){
      printf("incorrect number input %d\n", c);
      exit(1);
    }
    else {
      value = (10*value) + (c - '0');
    }
  }
  return(-1);//flags that the end of line has been hit
}


There is one problem:

int *getLine(FILE *infile){
    int line[7];
    int *point;

    point = &line[0];
    return(point);
}

You return a pointer to a local variable. It becomes invalid when you return from the function. You could allocate it instead on the heap, or let the caller provide it as an argument.


Instead of

while (i<=n[j]){

didn't you mean

while (i<=n[j][0]){

More Edit: That's actually ok, i overlook the * in the assignment.

Edit: Some more things:

  • there is no check that the range of int is not exceeded in getNum
  • there is no check in getLine that more than 7 values are read (which would blow int line[7]
  • the matrix calculation in my opinion assumes that there are 3 values read, getLine can deliver up to 7
  • matrix[i+(3*j)] = *(point+(i+(3*j))); ?? point is only 7 int big!!! so for the second value it will read beyond defined data. Shouldn't it read matrix[i+(3*j)] = point[i];

hth

Mario

BTW: I strongly recommend:

  • resort to std-lib functions
  • better naming (i and j in the same source are strongly discouraged)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号