通过指针传递值

时间:2014-09-04 16:45:38

标签: c function pointers

就我而言,我将变量被破坏并有效传递给函数Proccosvotes正确,但我没有得到正确的答案。不像我想象的那样将有效值增加1,而是向它添加随机数。例如,我应该得到大约200有效,但我得到2687468.我似乎找到了错误。有没有人看到或知道我犯的错误?

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

struct election{
    char name[20];
    int votes;
};

struct election electionCandidates[7];

void Initialize(struct election electionCandidates[], int num, FILE *ifp);
void Processvote(struct election electionCandidates[], int num, FILE *ifp, int *valid1, int *spoilt1);

int main(){
    FILE *ifp;
    char *winner[20]; 
    int valid = 0;
    int spoilt = 0;
    int num;

    ifp = fopen("elections.txt", "r"); 

    Initialize(electionCandidates, 6, ifp);

    Processvote(electionCandidates, 6, ifp, &valid, &spoilt); 

    system("PAUSE");

    printResults(electionCandidates, 6, &valid, &spoilt, *winner); 


    printf("GO");
    system("PAUSE");

    fclose(ifp);

    system("PAUSE");
    return 0;
}

void Initialize(struct election *electionCandidates, int num, FILE *ifp){
    int i = 0;
    for(i=0; i<7; i++){
        fscanf(ifp, "%[^\n]%*c", &electionCandidates[i].name);
        printf("%d: %s\n", i, electionCandidates[i].name);
    }
}

void Processvote(struct election *electionCandidates, int num, FILE *ifp, int *valid1, int *spoilt1){
    int i;
    int x=0;
    int j;
    for(i=0; i<365; i++){
        fscanf(ifp, "%d ", &x);

        for(j=1; j<=7; j++){
            if (j == x){
                electionCandidates[j].votes+=1;
                valid1++;
            }  

            if (x < 1 || x > 7)
                spoilt1++;                  
        }
        printf("valid: %d\nspoilt: %d\n", valid1, spoilt1);
    }  
}

2 个答案:

答案 0 :(得分:3)

问题1

在C中,数组从0开始计数而不是1。

因此,以下代码会导致未定义的行为,因为您调用electionCandidates[7]

    for(j=1; j<=7; j++){
        if (j == x){
            electionCandidates[j].votes+=1;

问题2

spoilt1valid1是指向单个整数的指针。如果要使用指针指向的值,则应执行以下操作:

printf("%d", *spoilt1);
*spoilt1 += 1;

问题3

你错过了printResults()的实现,所以我不确定你是怎么运行它的。

问题4

您的编译器应该为此代码打印一些警告。至少,你应该修复它们。请注意,警告通常表示代码中存在您不应忽视的实际问题。这是我的编译器报告的警告和错误:

[11:53am][wlynch@watermelon /tmp] clang foo.c
foo.c:30:5: warning: implicit declaration of function 'printResults' is invalid in C99 [-Wimplicit-function-declaration]
    printResults(electionCandidates, 6, &valid, &spoilt, *winner); 
    ^
foo.c:45:34: warning: format specifies type 'char *' but the argument has type 'char (*)[20]' [-Wformat]
        fscanf(ifp, "%[^\n]%*c", &electionCandidates[i].name);
                     ~~~~        ^~~~~~~~~~~~~~~~~~~~~~~~~~~
foo.c:66:43: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
        printf("valid: %d\nspoilt: %d\n", valid1, spoilt1);
                       ~~                 ^~~~~~
foo.c:66:51: warning: format specifies type 'int' but the argument has type 'int *' [-Wformat]
        printf("valid: %d\nspoilt: %d\n", valid1, spoilt1);
                                   ~~             ^~~~~~~
4 warnings generated.
Undefined symbols for architecture x86_64:
  "_printResults", referenced from:
      _main in foo-1d49c9.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

问题5

您应该检查您对fscanf()的来电是否确实成功。如果文件很短或无效,您可能无法更新变量的值。

答案 1 :(得分:0)

在Processvote中,valid1具有指向整数的类型指针。在你的代码中你写了valid1++,它正在递增指针的值,而不是它指向的值。试试(*valid1)++(*spoilt1)++