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

时间:2018-12-21 11:31:32

标签: c algorithm queue

此代码尝试执行队列,但是该队列有两个字段:数字和单词。我的问题是字段“ word”打印不正确(字段“ number”很好),输出是问号(没有字符串)。 输出看起来像这样

https://ibb.co/gjF446F

#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[];

} 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;



   }

我认为printf(“%s \ n”,n.word)无法正常工作。但是,如果我不使用struct,那么一切都可以正常工作。

2 个答案:

答案 0 :(得分:2)

您需要为单词分配内存。例如这样的

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

} data;

更好的方法是动态地为单词分配内存。

答案 1 :(得分:0)

#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;



   }