簡體   English   中英

給結構變量賦值

[英]Assigning value to struct variable

我正在構造用於創建有向圖的鄰接表。 我讀取了兩個字符,然后從每個字符中創建一個頂點,除非已經創建了帶有該字符的頂點。 我不熟悉結構,所以這使我絆倒了。 值尚未分配,我最終得到一個空列表。 我假設這與我忽略的指針有關,但我似乎找不到問題。 我希望有人能提供幫助。 我將包括我的代碼和示例輸出。

我的

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

/* Forward declaration */
struct EDGETAG;

typedef struct
{
    char c;
    bool isVisited;
    struct EDGETAG* p;
} VERTEX;

typedef struct EDGETAG
{
    VERTEX* v;
    struct EDGETAG* q;
} EDGE;


int main (int argc, char* argv[]);

int adjListSearch (char a, int size, VERTEX* adjList);

adjListSearch.c

#include "my.h"

int adjListSearch (char a, int size, VERTEX* adjList)
{

int i;


if(size == 0)
{
printf("empty\n");
return 1;
}

for(i = 0; i<size; i++)
{
    if( adjList[i].c = a)
    {
    return 0;
    printf("found\n");
    }
}
printf("notfound\n");
return 1;

}

main.c

#include "my.h"

int main (int argc, char* argv[])
{




int y = 0;
int x = 0;
VERTEX *adjList;
adjList  = (VERTEX*)calloc(26, sizeof(VERTEX));
//adjList = malloc(sizeof(VERTEX)*26);
FILE* p;
char *fp;
char a;
char b;
int check1 = 0;
int check2 = 0;
int size = 0;
int searchsuccess = 0;
int i;
//Statements



p = fopen( argv[1], "r");



while(fscanf(p," %c %c", &a, &b)!= EOF)  
{
printf("a: %c b: %c\n",a,b);

    check1 = adjListSearch(a,size,adjList);

    if(check1==1)
    {

        adjList[size].c = a;
        size = size +1;

    }   
        //printf("%c\n", adjList[size].c);  

    check2 = adjListSearch(b,size,adjList);
    if(check2==1)
    {
        adjList[size].c = b;
        size = size +1;
    }

}
//End While
printf("Size: %d", size);
for(i=0;i<size;i++)
{
printf("%c\n", adjList[size].c);
}


free(p);


return 0;
}
//End main

樣本輸出

a: A b: B
a: B b: C
a: E b: X
a: C b: D
Size: 1

如您所見,沒有字符被打印為在結構內部。

編輯1:

I would want the expected output to look like 
a: A b: B
a: B b: C
a: E b: X
a: C b: D
Size: 6
A
B
C
E
X
D

編輯2如果有人可以幫助,仍然對此不知所措。

有各種各樣的問題。

  1. 您應該使用fclose(p); 而並非free(p);
  2. 您應該打印printf("%c\\n", adjList[i].c); 而不是printf("%c\\n", adjList[size].c);
  3. 正如brokenfoot在他的答案中指出的那樣,您需要比較a而不是在adjListSearch()中分配它。
  4. 有多個未使用的變量(我使用的編譯選項會抱怨它們)。
  5. 不需要聲明main() (盡管它對程序沒有任何損害)。
  6. 您無需檢查是否要使用argv[1]
  7. 您無需檢查是否已打開文件。
  8. 您確實應該使用while (fscanf(p, " %c %c", &a, &b) == 2) ,盡管僅在該程序中要處理的字符數奇數才有意義。 至少您正在測試fscanf() ,這比我們看到的許多問題還要多。

在下面的代碼中,我打印了更多有用的狀態消息。

main.c

#include "my.h"

int main(int argc, char *argv[])
{
    VERTEX *adjList;
    adjList  = (VERTEX *)calloc(26, sizeof(VERTEX));
    FILE *p;
    char a;
    char b;
    int check1 = 0;
    int check2 = 0;
    int size = 0;
    int i;

    if (argc != 2)
    {
        fprintf(stderr, "Usage: %s file\n", argv[0]);
        return 1;
    }

    if ((p = fopen(argv[1], "r")) == 0)
    {
        fprintf(stderr, "Failed to open file %s for reading\n", argv[1]);
        return 1;
    }

    while (fscanf(p, " %c %c", &a, &b) == 2)
    {
        printf("a: %c b: %c\n", a, b);

        check1 = adjListSearch(a, size, adjList);

        if (check1 == 1)
        {
            printf("Adding a = %c\n", a);
            adjList[size++].c = a;
        }
        check2 = adjListSearch(b, size, adjList);
        if (check2 == 1)
        {
            printf("Adding b = %c\n", b);
            adjList[size++].c = b;
        }
    }
    printf("Size: %d\n", size);
    for (i = 0; i < size; i++)
    {
        printf("%c\n", adjList[i].c);
    }

    fclose(p);

    return 0;
}

adjListSearch.c

#include "my.h"

int adjListSearch(char a, int size, VERTEX *adjList)
{
    int i;

    if (size == 0)
    {
        printf("empty\n");
        return 1;
    }

    for (i = 0; i < size; i++)
    {
        if (adjList[i].c == a)
        {
            printf("found %c\n", a);
            return 0;
        }
    }
    printf("not found %c\n", a);
    return 1;
}

數據

A B
B C
E X
C D

樣品輸出

a: A b: B
empty
Adding a = A
not found B
Adding b = B
a: B b: C
found B
not found C
Adding b = C
a: E b: X
not found E
Adding a = E
not found X
Adding b = X
a: C b: D
found C
not found D
Adding b = D
Size: 6
A
B
C
E
X
D

adjListSearch() ,更改

if( adjList[i].c = a)

if( adjList[i].c == a)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM