首页 > 解决方案 > 将结构传递给C中的函数时损坏

问题描述

所以我有这个结构,我已经将front的值设置为0,但是当它进入一个函数时,这个值会跳转到一个非常高且看似随机的数字,例如“1868038144”。我不知道为什么会发生这种情况以及如何解决它。这是结构的结构:

#define MQA 3 //Defines the maximum amount of queues
#define MQS 5 //Defines the maximum queue size of each message queue (amount of messages)
#define MLN 200 //Defines the maximum size of a message


typedef struct MessQ{//A single message queue
    char messages[MQS][MLN];
    char id[50];
}MessQ_t;


typedef struct MsgQs{//An array of message queues
    MessQ_t * queue;
    int front; //helps define the next queue to be added
    int back;
}MsgQs_t;

这是我将其传递给的函数:

//creates a new queue and which is assigned an identifier passed as argument
void createQ(MsgQs_t *mq, char ident[50]){
    printf("This front: %d\n ",mq->front);
    if(mq->front<=MQA){
        printf("THIS IS THE FRONT %d\n",mq->front);
        strcpy( mq->queue[mq->front].id, "yes");
        mq->front++;//Increments this to keep track of where the next queue should be put
        printf("THIS IS THE FRONT %d\n",mq->front);
    }
    else{printf("Out of room \n");}
}

这是我用来主要引用它的代码:

printf("THIS IS THE FRONT %d\n",msq.front);
char ide[50]="ThisIsTheId";
createQ(&msq, ide);

标签: cfunctionstructpass-by-reference

解决方案


推荐阅读