printf显示错误的输出,第[C]行末尾有奇怪的问号

printf显示错误的输出,第[C]行末尾有奇怪的问号,c,algorithm,queue,C,Algorithm,Queue,这段代码试图执行队列,但该队列有两个字段:number和word。我的问题是字段“word”打印不正确(字段“number”很好) 预期产出: 二十二 abc 十二, efg 654 xyz 不幸的是,输出是这样的 #包括 #包括 #包括 #包括 #定义最大容量1000 #定义最大长度为100 类型定义结构{ 整数; 字符词[]; }数据; 数据存储阵列[最大容量]; int峰值=0; int rear=-1; int itemCount=0; int front(){ 返回阵列[peak]。

这段代码试图执行队列,但该队列有两个字段:number和word。我的问题是字段“word”打印不正确(字段“number”很好)

预期产出:

二十二 abc

十二, efg

654 xyz

不幸的是,输出是这样的

#包括
#包括
#包括
#包括
#定义最大容量1000
#定义最大长度为100
类型定义结构{
整数;
字符词[];
}数据;
数据存储阵列[最大容量];
int峰值=0;
int rear=-1;
int itemCount=0;
int front(){
返回阵列[peak]。编号;
}
布尔是空的{
返回itemCount==0;
}
bool isFull(){
return itemCount==最大容量;
}
int size(){
返回项目计数;
}  
空白插入(数据x){
如果(!isFull()){
如果(后==最大容量-1){
后部=-1;
}       
int indeks=++后部;
intArray[indeks].number=x.number;
strcpy(intArray[indeks].word,x.word);
itemCount++;
}
}
数据删除(){
数据dat=intArray[peak++];
如果(峰值==最大容量){
峰值=0;
}
项目计数--;
返回数据;
}
无效打印(int N){

对于(int i=0;i您需要为word分配内存。例如:

typedef struct{
   int number;
   char word[100];

} data;
更好的方法是动态地为word分配内存
#include <stdio.h> 
#include <stdlib.h> 
#include <limits.h>
#include <string.h> 

#define MAX_capacity 1000
#define Max_len_napis 100


typedef struct{
   int number;
   char word[100];

} data;

data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;

int front() {
   return intArray[peak].number;
}

bool isEmpty() {
   return itemCount == 0;
}

bool isFull() {
   return itemCount == MAX_capacity;
}

int size() {
   return itemCount;
}  

void insert(data x) {

   if(!isFull()) {

      if(rear == MAX_capacity-1) { 
         rear = -1;            
      }       

      int indeks = ++rear;
      intArray[indeks].number = x.number;
      strcpy (intArray[indeks].word, x.word);

      itemCount++;
   }
}


data remove() {
   data dat = intArray[peak++];

   if(peak == MAX_capacity) {
      peak = 0;
   }

   itemCount--;
   return dat;  
}


void print(int N){


    for(int i=0;i<N;i++){
        data n = remove();           
      printf("%d\n",n.number);
      printf("%s\n",n.word); // that's line doesn't work correctly




    }



}


int main() {
   data tab[3];

   tab[0].number = 22;
   strcpy (tab[0].word, "abc");
   insert(tab[0]);

   tab[1].number = 12;
   strcpy (tab[1].word, "efg");
   insert(tab[1]);

   tab[2].number = 654;
   strcpy (tab[2].word, "xyz");
   insert(tab[2]);



   int siz = size();
   print(siz);





    return 0;



   }
#包括 #包括 #包括 #定义最大容量1000 #定义最大长度为100 类型定义结构{ 整数; 字符字[100]; }数据; 数据存储阵列[最大容量]; int峰值=0; int rear=-1; int itemCount=0; int front(){ 返回阵列[peak]。编号; } 布尔是空的{ 返回itemCount==0; } bool isFull(){ return itemCount==最大容量; } int size(){ 返回项目计数; } 空白插入(数据x){ 如果(!isFull()){ 如果(后==最大容量-1){ 后部=-1; } int indeks=++后部; intArray[indeks].number=x.number; strcpy(intArray[indeks].word,x.word); itemCount++; } } 数据删除(){ 数据dat=intArray[peak++]; 如果(峰值==最大容量){ 峰值=0; } 项目计数--; 返回数据; } 无效打印(int N){
for(int i=0;i
char-word[];
保留一个零字符数组。我肯定这不是您想要的。
char-word[]
是一个灵活的数组成员。您应该为它分配内存。谢谢,它可以工作!但是为什么?word[]有什么问题?@Pawel,word[]在不初始化它时会分配0字节。word[]是定义灵活大小数组的一种方法。“灵活”并不意味着数组将自动增长,如果您尝试向其中写入更多数据。灵活意味着编译器在word[]=“hello”将计算初始化字符串的长度并分配(6字节)所需的内存量。
#include <stdio.h> 
#include <stdlib.h> 
#include <limits.h>
#include <string.h> 

#define MAX_capacity 1000
#define Max_len_napis 100


typedef struct{
   int number;
   char word[100];

} data;

data intArray[MAX_capacity];
int peak = 0;
int rear = -1;
int itemCount = 0;

int front() {
   return intArray[peak].number;
}

bool isEmpty() {
   return itemCount == 0;
}

bool isFull() {
   return itemCount == MAX_capacity;
}

int size() {
   return itemCount;
}  

void insert(data x) {

   if(!isFull()) {

      if(rear == MAX_capacity-1) { 
         rear = -1;            
      }       

      int indeks = ++rear;
      intArray[indeks].number = x.number;
      strcpy (intArray[indeks].word, x.word);

      itemCount++;
   }
}


data remove() {
   data dat = intArray[peak++];

   if(peak == MAX_capacity) {
      peak = 0;
   }

   itemCount--;
   return dat;  
}


void print(int N){


    for(int i=0;i<N;i++){
        data n = remove();           
      printf("%d\n",n.number);
      printf("%s\n",n.word); // that's line doesn't work correctly




    }



}


int main() {
   data tab[3];

   tab[0].number = 22;
   strcpy (tab[0].word, "abc");
   insert(tab[0]);

   tab[1].number = 12;
   strcpy (tab[1].word, "efg");
   insert(tab[1]);

   tab[2].number = 654;
   strcpy (tab[2].word, "xyz");
   insert(tab[2]);



   int siz = size();
   print(siz);





    return 0;



   }