开发者社区> 问答> 正文

指针和引用在C中运行

好的,我在理解指针如何在C上工作以及为什么不起作用方面存在一些问题。我创建了一个内部带有3个变量的结构,并希望在函数“ LeerFicher”内部对其进行初始化。据我了解,我正在将存储方向传递给函数,因此我将能够从函数内部初始化结构的成员,对吗?CodeBlock给我这个错误:错误:请求成员'duracion'的东西不是结构或联合| 有帮助吗?

typedef struct canciones{
   int duracion;
   int popularidad;
   int valor;
}canciones;

int main()
{
   canciones arraySolucion[20];
   leerFichero("canciones.txt", &arraySolucion);
}

   void leerFichero(char *nombre_fichero, canciones *solucion[])
{

   char linea[500]; // Para guardar la linea de cada fichero
   char *token;     // Para cada token de cada linea
   int i, e;

   //Abrimos el fichero
   FILE *fp = fopen(nombre_fichero,"r");

   // Recorremos cada linea del fichero
   while(fgets(linea,500,fp)!=NULL)
   {
       for(i=0; i<20; i++)
       {
           token = strtok(linea,";"); // Separamos cada linea por ","
           while(token != NULL)
           {
               for(e=0; e<5; e++)
               {
                   printf("%s\n",token);
                   switch(e)
                   {
                   case 0:
                       **solucion[i].duracion=atoi(token);**//error is here
                       break;
}

展开
收起
kun坤 2019-11-29 11:39:03 363 0
1 条回答
写回答
取消 提交回答
  • 您声明了一个结构数组,例如
    
    canciones arraySolucion[20];
    并将其传递给类似表达式的函数
    
    &arraySolucion
    就像指向数组的指针一样。
    
    因此,表达式具有类型canciones ( * )[20]。
    
    但是,函数的相应参数具有与type canciones *solucion[]相同的类型canciones **solucion。
    
    参数类型和参数类型都没有隐式转换。
    
    此外,该函数应在main中使用之前声明。
    
    实际上,该函数可以像
    
    void leerFichero( const char *nombre_fichero, canciones solucion[], size_t n );
    叫像
    
    leerFichero("canciones.txt", arraySolucion, 20 );
    在这种情况下,您可以编写
    
       for(i=0; i < n; i++)
       {
           token = strtok(linea,";"); // Separamos cada linea por ","
           while(token != NULL)
           {
               for(e=0; e<5; e++)
               {
                   printf("%s\n",token);
                   switch(e)
                   {
                   case 0:
                       solucion[i].duracion=atoi(token);
                       break;
    也不清楚5内循环中的幻数是什么意思。
    
    这是一个演示程序。
    
    #include <stdio.h>
    
    typedef struct canciones{
       int duracion;
       int popularidad;
       int valor;
    }canciones;
    
    void leerFichero( const char *nombre_fichero, canciones solucion[], size_t n );
    
    int main( void )
    {
        enum { N = 20 };
        canciones arraySolucion[N];
        leerFichero( "canciones.txt", arraySolucion, N );
    
        for ( size_t i = 0; i < N; i++ )
        {
            printf( "%zu: %d, %d, %d\n", 
                    i,
                    arraySolucion[i].duracion, 
                    arraySolucion[i].popularidad, 
                    arraySolucion[i].valor );
        }        
    }
    
    void leerFichero( const char *nombre_fichero, canciones solucion[], size_t n )
    {
        puts( nombre_fichero );
        for ( size_t i = 0; i < n; i++ )
        {
            solucion[i].duracion    = ( int )i;
            solucion[i].popularidad = ( int )i;
            solucion[i].valor       = ( int )i;
        }
    } 
    它的输出是
    
    canciones.txt
    0: 0, 0, 0
    1: 1, 1, 1
    2: 2, 2, 2
    3: 3, 3, 3
    4: 4, 4, 4
    5: 5, 5, 5
    6: 6, 6, 6
    7: 7, 7, 7
    8: 8, 8, 8
    9: 9, 9, 9
    10: 10, 10, 10
    11: 11, 11, 11
    12: 12, 12, 12
    13: 13, 13, 13
    14: 14, 14, 14
    15: 15, 15, 15
    16: 16, 16, 16
    17: 17, 17, 17
    18: 18, 18, 18
    19: 19, 19, 19   
    
    
    2019-11-29 11:39:14
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
对象的生命期管理 立即下载
低代码开发师(初级)实战教程 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载