c - 如何在 C 中将结构体数组中的名称值作为引用传递?

标签 c arrays struct pass-by-reference

im 应该能够打印 print 函数中的所有国家并将其传递给第二个 if 语句,但它似乎没有打印。我知道这是

printf("%s\n", ctryList[numCountries].countryName);

部分,但我不知道它出了什么问题。

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

const int MAX_COUNTRY_NAME_LENGTH = 50;

typedef struct CountryTvWatch_struct {
   char countryName[50];
   int tvMinutes;
} CountryTvWatch;

void PrintCountryNames(CountryTvWatch ctryList[], int numCountries)
{
   int i;
   for(i = 0; i < numCountries; i++)
   {
   printf("%s\n", ctryList[numCountries].countryName); 
   }
   return;
} 

int main(void) {
   // Source: www.statista.com, 2010
   const int NUM_COUNTRIES = 4;

   CountryTvWatch countryList[NUM_COUNTRIES];
   char countryToFind[MAX_COUNTRY_NAME_LENGTH];
   bool countryFound = false;
   int i = 0;

   strcpy(countryList[0].countryName, "Brazil");
   countryList[0].tvMinutes = 222; 
   strcpy(countryList[1].countryName, "India");       
   countryList[1].tvMinutes = 119;
   strcpy(countryList[2].countryName, "U.K.");        
   countryList[2].tvMinutes = 242;
   strcpy(countryList[3].countryName, "U.S.A.");      
   countryList[3].tvMinutes = 283;

   printf("Enter country name: \n");
   scanf("%s", countryToFind);

   countryFound = false;
   for (i = 0; i < NUM_COUNTRIES; ++i) { // Find country's index
      if (strcmp(countryList[i].countryName, countryToFind) == 0) {
         countryFound = true;
         printf("People in %s watch\n", countryToFind);
         printf("%d minutes of TV daily.\n", countryList[i].tvMinutes);
      }
   }
   if (!countryFound) {
      printf("Country not found, try again.\n");
      printf("Valid countries:\n"); 
      PrintCountryNames(countryList, NUM_COUNTRIES); 
   }

   return 0;
}

最佳答案

以下建议代码:

  1. 合并对问题的评论
  2. 正确检查 I/O 错误
  3. 让用户知道可以选择哪些国家/地区
  4. 在水平和垂直方向上都有适当的间距,以提高可读性
  5. 执行所需的功能
  6. 干净地编译
  7. 记录了包含每个头文件的原因

现在建议的代码:

#include <stdio.h>     // scanf(), printf()
#include <stdlib.h>    // exit(), EXIT_FAILURE
#include <string.h>    // strcmp()
#include <stdbool.h>   // bool, true, false

#define MAX_COUNTRY_NAME_LENGTH  50
#define NUM_COUNTRIES  4

struct CountryTvWatch_struct
{
    char countryName[ MAX_COUNTRY_NAME_LENGTH ];
    int tvMinutes;
};
typedef struct CountryTvWatch_struct CountryTvWatch;


// prototypes
void PrintCountryNames( CountryTvWatch ctryList[], int numCountries );


int main(void)
{
   // Source: www.statista.com, 2010

   CountryTvWatch countryList[NUM_COUNTRIES];
   char countryToFind[ MAX_COUNTRY_NAME_LENGTH+1];

   strcpy(countryList[0].countryName, "Brazil");
   countryList[0].tvMinutes = 222;

   strcpy(countryList[1].countryName, "India");
   countryList[1].tvMinutes = 119;

   strcpy(countryList[2].countryName, "U.K.");
   countryList[2].tvMinutes = 242;

   strcpy(countryList[3].countryName, "U.S.A.");
   countryList[3].tvMinutes = 283;

   // let user know what countries are available and how they are spelled
   PrintCountryNames(countryList, NUM_COUNTRIES);

   printf("Enter country name: \n");

   // Note: following statement 
   //     checks for error
   //     includes a MAX_CHAR modifier that is one less than
   //    the length of the input field
   if( 1 != scanf("%49s", countryToFind) )
   {
       perror( "scanf failed" );
       exit( EXIT_FAILURE );
   }

   // implied else, scanf successful


   bool countryFound = false;

   for ( int i = 0; i < NUM_COUNTRIES; ++i )
   { // Find country's index
      if (strcmp(countryList[i].countryName, countryToFind) == 0)
      {
         countryFound = true;
         printf("People in %s watch\n", countryToFind);
         printf("%d minutes of TV daily.\n", countryList[i].tvMinutes);
         break;  // exit the search loop early
      }
   }

   if (!countryFound)
   {
      printf("Country not found, try again.\n");
      printf("Valid countries:\n");
      PrintCountryNames(countryList, NUM_COUNTRIES);
   }

   return 0;
}


void PrintCountryNames( CountryTvWatch ctryList[], int numCountries )
{
    for( int i = 0; i < numCountries; i++ )
    {
        printf("%s\n", ctryList[ i ].countryName);
    }
}

关于c - 如何在 C 中将结构体数组中的名称值作为引用传递?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47266537/

相关文章:

c - 如何在关闭 TCP 连接之前发送消息 [POSIX]

arrays - 如何将 NSManagedObject 值转换为具有键和值对的字典?

c - 一对变量/结构的偏移量是否相同?

c++ - 如何按名称公开类的字段

c - 定义复数变量时出错

c++ - Pthreads,与 pthread_join(pthread_t, void**) 混淆

c - 思考TAILQ的tqe_prev不指向前一个节点的目的

c - 二维阵列矩阵无法正确打印

java - 去除字符串中的重复字符

c - C 结构的 sizeof() 部分 - 有点像