在C中将结构传递到函数时发生损坏

在C中将结构传递到函数时发生损坏,c,function,struct,pass-by-reference,C,Function,Struct,Pass By Reference,所以我有这个结构,我把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) #de

所以我有这个结构,我把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");}
}

你最好给我们一个完整的,最好是可编译的,最少可复制的例子。如果没有这一点,就很难说问题出在您提供给我们的代码上还是程序中的其他地方。在
void createQ(MsgQs_t*mq,…
中,您正在通过值传递
mq
,因此当函数返回时,在
create
中所做的任何更改都将丢失,并且在调用方中从未看到过。这两个错误都是不相关的,但同样致命。此外,您还需要为
char id[50]之外的所有值定义适当的常量;
-为什么
50
没有常量?所以有一个关于如何创建MCVE()(或MRE或现在使用的任何名称)的简介,或者有一个关于如何创建SSCCE()的外部站点。使用调试器可以帮助您找到存在问题的行。
printf("THIS IS THE FRONT %d\n",msq.front);
char ide[50]="ThisIsTheId";
createQ(&msq, ide);