debugging - C Why Variable is unused? -
i need debugging program, how come program tells me have unused variable, have defined table mst[num_pt] right after create structure. ive tried moving around , defining in other places assigning mst[0] @ other places no luck. have syntax error? snippet of code referring near end thought might have entire program.
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <time.h> int main(int argc, char *argv[]) { file *fp; int max_x, max_y, num_pt; int *x_coordinate, *y_coordinate; int inputfile = 0, outputfile = 0; int i; int count,dist,spot; char black[24],white[24]; typedef struct{ char colour[24]; int x; int y; int pos; }table; strcpy(black,"black"); strcpy(white,"white"); if (argc == 1) { /* generate random instances, accepting parameters stdin */ return 1; } (i = 1; < argc; i++) { if (strcmp (argv[i], "-i") == 0) inputfile = i+1; else if (strcmp (argv[i], "-o") == 0) outputfile = i+1; } if (inputfile == 0) { printf("error file not exit!\n"); return -1; } if ((fp = fopen(argv[inputfile], "r")) == null) { printf("error name of file not exist!\n"); return -2; } while (fscanf(fp, "%d", &max_x) != 1) { if (ferror(fp)) { /* read error */ printf("error max value not equal 1!\n"); fclose(fp); return -3; } if (feof(fp)) { /* no integer read */ printf("error no numbers read in file!\n"); fclose(fp); return -4; } fscanf(fp, "%*[^\n]"); /* skip rest of line */ } if (fscanf(fp, "%d", &max_y) != 1) { /* max_y not following max_x */ printf("error format x not follwing y\n"); fclose(fp); return -5; } while (fscanf(fp, "%d", &num_pt) != 1) { if (ferror(fp)) { /* read error */ printf("error num_pt != 1\n"); fclose(fp); return -6; } if (feof(fp)) { /* no integer read */ printf("error file empty!\n"); fclose(fp); return -7; } fscanf(fp, "%*[^\n]"); /* skip rest of line */ } x_coordinate = (int *)malloc(num_pt * sizeof(int)); y_coordinate = (int *)malloc(num_pt * sizeof(int)); (i = 0; < num_pt; i++) { while (fscanf(fp, "%d", &x_coordinate[i]) != 1) { if (ferror(fp)) { printf("coordinate reading error!\n"); /* read error */ fclose(fp); return -8; } if (feof(fp)) { /* no integer read */ printf("error no integers read\n"); fclose(fp); return -9; } fscanf(fp, "%*[^\n]"); /* skip rest of line */ } if (fscanf(fp, "%d", &y_coordinate[i]) != 1) { /* y_coordinate not following x_coordinate */ printf("error x/y not following each other\n"); fclose(fp); return -10; } } fclose(fp); for(count=0;count<num_pt;count++){ strcpy(nodes[count].colour,white); nodes[count].pos = count; nodes[count].x = x_coordinate[count]; nodes[count].y = y_coordinate[count]; } table nodes[num_pt],parent[1],small[num_pt],mst[num_pt]; mst[0] = nodes[0]; strcpy(nodes[0].colour,black); parent[0] = nodes[0]; for(spot=1;spot<num_pt;spot++){ for(count=0;count<num_pt;count++){ dist = (nodes[count].x-parent[0].x)+(nodes[count].y-parent[0].y); if (dist > 0 && strcmp(nodes[count].colour,white) == 0){ small[0] = nodes[count]; } } printf("found node %d --> nodes %d closest\n",parent[0].pos,small[0].pos); parent[0] = small[0]; mst[spot] = small[0]; strcpy(nodes[small[0].pos].colour,black); } //int labelx=0,labely=0; /* if (outputfile > 0) { if ((fp = fopen(argv[outputfile], "w")) == null) { printf("error, can't open print file\n"); return -2; } fprintf(fp, "##################################################\n"); fprintf(fp, "#%s\n", argv[inputfile]); fprintf(fp, "#area [0, max_x] x [0, max_y]\n"); fprintf(fp, "%d\t%d\n", max_x, max_y); fprintf(fp, "#number of points num_pt\n"); fprintf(fp, "%d\n", num_pt); fprintf(fp, "#coordinates\n"); (i = 0; < num_pt; i++) { fprintf(fp, "%d\t%d\n", x_coordinate[i], y_coordinate[i]); } fprintf(fp, "#end of instance\n"); fprintf(fp, "#edges of mst prim's algorithm:\n"); fclose(fp); } */ printf("choosing point 0 root ...\n"); (i = 1; < num_pt; i++) { printf("point %d has distance %d tree, parent 0;\n", i,(abs(x_coordinate[0] - x_coordinate[i])) + (abs(y_coordinate[0] - y_coordinate[i]))); } printf("#edges of mst prim's algorithm\n"); /* for(count=0;count<num_pt;count++){ totalmst=totalmst+mst[count].dist; printf("%d %d %d\n",mst[count].coord[1],mst[count].coord[0],mst[count].dist); } printf("the total length of mst %d.\n",totalmst); */ free(x_coordinate); free(y_coordinate); return 0; }
the console returns:
ass2.c: in function ‘main’: ass2.c:105:12: error: ‘nodes’ undeclared (first use in function) strcpy(nodes[count].colour,white); ^ ass2.c:105:12: note: each undeclared identifier reported once each function appears in ass2.c:110:47: warning: variable ‘mst’ set not used [-wunused-but-set-variable] table nodes[num_pt],parent[1],small[num_pt],mst[num_pt];
don't worry mst
issue. take care of nodes
issue , worry subsequent errors. chances mst
issue disappear.
also, cannot declare arrays runtime-determined sizes way. expect if increased warnings (-wall
gcc
) might more clarity on how seeing came be.
do this: table* nodes,small,mst; table parent[1];
nodes = malloc(sizeof(/* "whatever things in array" */) * num_pt); /* * likewise other `table` arrays. */
also, seem using 2 different vars named nodes
! check loop right above table
array declarations. ain't right here.
Comments
Post a Comment