【问题标题】:C change pointer address of array in other function to change the position of values in the arrayC在其他函数中更改数组的指针地址以更改数组中值的位置
【发布时间】:2021-05-19 14:36:54
【问题描述】:
 typedef struct {
   char* value;
   char* type;
 } CARD;

 CARD cards[52];

 void initializeCards() { 

   char* types[10] = {"Spade", "Club", "Hearts", "Diamonds"};
   char* values[13] = {"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "B", "D", "K"};

   shuffleArray(&values, 4); //I've also tried *values, or just values

   for (int i = 0; i < 4; i++) {
     for (int e = 0; e < 13; e++) {
       cards[i*13 + e].type = types[i];
       cards[i*13 + e].value = values[e];
     }
   }
 }//initializeCards.


 //Code in this function comes from this thread: https://stackoverflow.com/questions/34844003/changing-array-inside-function-in-c
 //I've tried many different things inside this function, none of these seem to work.
 
 void shuffleArray(char** array, uint8_t size) {

    free(*array);

    *array = malloc(size * sizeof(int));
    //char* temp = (*array)[0];
    //(*array)[0] = 1;

    (*array)[0] = "test";

    printf("%s\n", &array[0]);
 }//shuffleArray.

我正在尝试将一副纸牌放入一个数组中,这些纸牌应该被洗牌,但我不想用纸牌洗牌数组,我想洗牌这两个较短的数组“值”和“类型”,因为它们更短,因此洗牌的迭代次数更少。

我尝试了很多不同的方法,但都没有奏效,我想要的是更改数组的指针 在函数 'shuffleArray' 中,以便其他函数中的数组也会自动更改。

如果你们中的任何人可以帮助我正确地执行以下操作,我想我会设法完全改组它们:

//This is pseudo-code, if I get this correctly I should be able to shuffle them.
temp = array[0]
array[0] = array[size-1]
array[size-1] = temp

【问题讨论】:

    标签: arrays c pointers struct


    【解决方案1】:

    不完全清楚您要做什么,但您当然不能 free 一个不是通过 malloc(和系列)分配的数组。也许你想要这样的东西:

    #include <stdlib.h>                                                                
    #include <stdio.h>
    #include <time.h>                                                                  
    #include <assert.h>                                                                
                                                                                       
    struct card {                                                                      
            char *value;                                                               
            char *suit;                                                                
    };                                                                                 
                                                                                       
    struct card cards[52];                                                             
                                                                                       
    void initializeCards(void);                                                        
    void shuffleArray(char **, size_t);                                                
                                                                                       
    int                                                                                
    main(void)                                                                         
    {                                                                                  
            srand(time(NULL));                                                         
            initializeCards();                                                         
            for( int i = 0; i < 52; i++ ){                                             
                    printf("%s of %s\n", cards[i].value, cards[i].suit);               
            }                                                                          
    }                                                                                  
                                                                                       
    void                                                                               
    initializeCards(void)                                                              
    {                                                                                  
            char *suits[] = { "Spades", "Clubs", "Hearts", "Diamonds" };               
            char *values[] = {                                                         
                    "Ace", "2", "3", "4", "5", "6", "7",                               
                    "8", "9", "10", "Jack", "Queen", "King"                            
            };                                                                         
                                                                                       
            shuffleArray(values, sizeof values / sizeof *values);                      
            shuffleArray(suits, sizeof suits / sizeof *suits);                         
            struct card *c = cards;                                                    
                                                                                       
            for(unsigned i = 0; i < sizeof suits / sizeof *suits; i++ ){               
                    for( unsigned e = 0; e < sizeof values / sizeof *values; e++ ){    
                            assert( c < cards + sizeof cards / sizeof *cards );        
                            c->suit = suits[i];                                        
                            c->value = values[e];                                      
                            c += 1;                                                    
                    }                                                                  
            }                                                                          
    }                                                                                  
      
    void                                                                               
    shuffleArray(char **array, size_t n)                                               
    {                                                                                  
            for( unsigned i = 0; i < n - 1; i++ ){                                    
                    unsigned j = i + rand() % (n - i);                                 
                    char *t = array[i];                                                
                    array[i] = array[j];;                                              
                    array[j] = t;                                                      
            }                                                                          
    } 
    

    【讨论】:

    • 对于长度表达式,这样使用“sizeof values / sizeof values[0]”会不会更清楚
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-04-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多