2016-11-13 46 views
-1

我只是需要一些幫助來調試我的程序,我的程序如何告訴我,我有一個未使用的變量,但很明顯,我已經在創建結構後立即定義了表mst [num_pt]。我嘗試過將它移動並在其他地方定義它,以及在其他地方分配mst [0],但沒有運氣。我有語法錯誤嗎?我所指的代碼片段接近尾聲,但我認爲這可能有助於我的整個程序。C爲什麼變量未使用?

#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) { 
    /* to generate random instances, accepting parameters from stdin */ 
    return 1; 
    } 
    for (i = 1; i < 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 does not exit!\n"); 
    return -1; 
    } 
    if ((fp = fopen(argv[inputfile], "r")) == NULL) { 
    printf("Error name of file does not exist!\n"); 
    return -2; 
    } 
    while (fscanf(fp, "%d", &max_x) != 1) { 
    if (ferror(fp)) { 
     /* read error */ 
     printf("Error max value does not equal 1!\n"); 
     fclose(fp); 
     return -3; 
    } 
    if (feof(fp)) { 
     /* no integer to read */ 
     printf("Error no numbers to read in file!\n"); 
     fclose(fp); 
     return -4; 
    } 
    fscanf(fp, "%*[^\n]"); /* skip the rest of line */ 
    } 
    if (fscanf(fp, "%d", &max_y) != 1) { 
    /* max_y not following max_x */ 
    printf("Error format for 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 to read */ 
     printf("Error file is empty!\n"); 
     fclose(fp); 
     return -7; 
    } 
    fscanf(fp, "%*[^\n]"); /* skip the rest of line */ 
    } 
    x_coordinate = (int *)malloc(num_pt * sizeof(int)); 
    y_coordinate = (int *)malloc(num_pt * sizeof(int)); 
    for (i = 0; i < 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 to read */ 
    printf("Error no integers to read\n"); 
    fclose(fp); 
    return -9; 
     } 
     fscanf(fp, "%*[^\n]"); /* skip the 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 to be 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"); 
    for (i = 0; i < 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 the MST by Prim's algorithm:\n"); 

    fclose(fp); 
} 
    */ 



    printf("Choosing point 0 as root ...\n"); 
    for (i = 1; i < num_pt; i++) { 
    printf("Point %d has a distance %d to the tree, parent 0;\n", i,(abs(x_coordinate[0] - x_coordinate[i])) + (abs(y_coordinate[0] - y_coordinate[i]))); 
    } 
    printf("#Edges of the MST by 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 the MST is %d.\n",totalmst); 
    */ 
    free(x_coordinate); 
    free(y_coordinate); 
    return 0; 
} 

控制檯回報:

ass2.c: In function ‘main’: 
ass2.c:105:12: error: ‘nodes’ undeclared (first use in this function) 
    strcpy(nodes[count].colour,white); 
      ^
ass2.c:105:12: note: each undeclared identifier is reported only once for each function it appears in 
ass2.c:110:47: warning: variable ‘mst’ set but not used [-Wunused-but-set-variable] 
    table nodes[num_pt],parent[1],small[num_pt],mst[num_pt]; 
+1

你不覺得告訴我們*哪個*變量是有意義的嗎?請在您的問題中包含完整的錯誤/警告消息。 – kaylum

+0

對不起,我剛剛更新了我的問題,但是我指的是mst [num_pt]。 – brzmath

+0

而確切的消息? – kaylum

回答

0

不要擔心mst問題。 照顧nodes問題,然後擔心後續的錯誤。機會是mst問題將會消失。

此外,您不能用這種方式聲明具有運行時確定大小的數組。 我希望如果你增加了警告(-Wallgcc),你可能會更清楚你的看法是如何發生的。

做這樣的事情: table * nodes,small,mst; table parent [1];

nodes = malloc(sizeof(/* "whatever the things are in this array" */) * num_pt); 
/* 
* Likewise for the other `table` arrays. 
*/ 

此外,你似乎使用2個不同的變種,名爲nodes! 檢查您的table數組聲明上面的循環。有些事情不對。

+0

「你不能用這種方式聲明具有運行時確定大小的數組」。你可以,那些被稱爲[可變長度數組(VLA)](https://en.wikipedia.org/wiki/Variable-length_array)。 – kaylum

+0

@kaylum - 感謝你的教訓,我認爲我們可以說是對的。根據您分享的鏈接:_「支持VLA的編程語言包括...,C99(儘管隨後在C11中降級爲condi這些實現不需要支持;在某些平臺上,可以用'alloca()'或類似函數)來實現......「_ –