简体   繁体   English

访问结构数组中的值

[英]accessing values in a struct array

I am passing in an array into a function straightflush. 我将数组传递给函数straightflush。 I use a counting loop so i can get to all of the elements but for some reason, even tho the counter i increases, i get the value and suit for the first element of the array. 我使用一个计数循环,所以我可以使用所有元素,但是由于某种原因,即使计数器增加,我也可以得到值并适合数组的第一个元素。 Therefore, only my spadesCount increases as it always shows 4 for the value and spade for the suit. 因此,只有我的spadesCount会增加,因为它的值和西装的spade总是显示4。

struct card{
    int value;
    char suit;
};


int straightflush(struct card hand[], int n)
    {
        int clubsCount = 0;
        int diamondsCount = 0;
        int heartCount = 0;
        int spadesCount =0;
        int i;
        for(i=0; i<n; i++)
        {
            if (hand[i].suit == 'c')
            {
                clubsCount++;
            }
            else if (hand[i].suit == 'd')
            {
                diamondsCount++;
            }
            else if (hand[i].suit == 'h')
            {
                heartCount++;
            }
            else{
                spadesCount++;
            }
        }
    return 0;
    }

here is my main: 这是我的主要内容:

int main(){
    struct card hand1[] = {{4,'s'}, {9,'s'},{12,'c'},{11,'s'},{8,'s'},
        {6,'d'}, {3,'d'},{7,'s'},{10,'s'},{12,'d'}};
    printf ("%d\n", straightflush(hand1, 10));
}

I just run your code and the four count variables have correct values. 我只运行您的代码,并且四个count变量具有正确的值。 I think it's because you are returning 0 at the end of your straightflush function, the output is always 0. 我认为这是因为在straightflush函数的结尾处返回0,所以输出始终为0。

You can use a debugger or add the following line before the return statement in straightflush() to prove that your counts are actually accurate. 您可以使用调试器,也可以在straightflush()中的return语句之前添加以下行,以证明计数实际上是正确的。

printf("%d %d %d %d\n", clubsCount, diamondsCount, heartCount, spadesCount);

Your return value has nothing to do with the values you read thus the printf statement in your main() function is not printing the count of any thing, it is just printing 0 no matter what. 您的返回值与读取的值无关,因此main()函数中的printf语句不打印任何东西的计数,无论如何都只是打印0。

If you want the counts accessible outside of striaghtflush() you need to either use global variables for those counts (a generally shunned idea) or pass some values in by reference. 如果您希望在striaghtflush()之外访问计数,则需要对这些计数使用全局变量(通常被避开的想法),或者通过引用传递一些值。 An example of this would be: 例如:

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

void editValues( int *numDiamonds, int *numClubs, int *numHearts, int *numSpades ){
    *numDiamonds = 3;
    *numClubs = 5;
    *numHearts = 7;
    *numSpades = 11;
}

int main(int argc,char**argv)
{
    int numD=0, numC=1, numH=2, numS=3;
    printf("%d %d %d %d\n", numD, numC, numH, numS);
    editValues(&numD, &numC, &numH, &numS);
    printf("%d %d %d %d\n", numD, numC, numH, numS);
    return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM