【问题标题】:Trying to initialize and fill a 2D char array with using structure尝试使用结构初始化和填充二维字符数组
【发布时间】:2014-06-19 15:49:10
【问题描述】:

我想要做的是使用一个结构在函数“function1()”中创建和显示一个二维字符数组。这个数组将被发送回 main( ) 以便我可以在我的程序中进一步使用它。但是我的程序遇到了问题。我在使用指针时遇到问题。我认为我的问题出在我的指针或变量的某个地方。我已经毫不费力地尝试了几种组合。作为一个初学者,这可能是我没有想到的一些奇怪的组合。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 13
#define COL 16

typedef struct letter_array {
    char** letters;
    struct letter_array *ltr_ptr;
} larray;

void function1 (larray ** letter1[*][16]);
int function2 (larray letter2[][16]);
int function3 (larray letter3[][16]);
void function4 (int hor_ans, int ver_ans);

int main (void)
{
    larray letter_list;
    int vert, hori, **lptr;

    lptr = malloc(ROW*sizeof(int*));

    for(vert = 0; vert<ROW; vert++)
    {
        lptr [vert] = malloc(COL*sizeof(int));
    }


    printf("\n \t\t\t *** Hello! ***");


    printf("\n This program will create a random selection of 180 upper-case"
           " characters. \n\n");

    function1(&letter_list); //Problem #1


    printf("\n\nThank you for using my random character array program. \n"
           "\t\t Have a good day! \n");

    return ( 0 ) ;
}    


void function1 (larray **letter1 [][16])
{
    int i, z, funptr;

    srandom((unsigned)time(NULL));

    for(i=0; i<12; i++)
    {
        letter1 [i] <- (int*) funptr;     // Problem #2-3
        for(z=0; z<15; z++)            
        {
            letter1[i][z] = random( )%26+'A';  // Problem #4
            printf("%c ", letter1[i][z]);
        }
        printf("\n");
    }

    return ;
}

错误如下并已注释。

  1. 警告:从不兼容的指针类型传递“function1”的参数 1
  2. 警告:从不同大小的整数转换为指针
  3. 错误:一元减号的类型参数错误
  4. 警告:赋值使指针从整数而不进行强制转换

【问题讨论】:

  • 您的代码没有main
  • @ooga 哎呀......我忘了把它放在帖子里。
  • void function1 (larray ** letter1[*][16]); - 圣烟,蝙蝠侠
  • 您似乎缺少对类型的一些基本了解;如果T是一个类型,T t;是一个变量声明,那么&amp;tt的地址,就是T *类型。

标签: c function multidimensional-array struct


【解决方案1】:

我希望这会有所帮助。

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 13
#define COL 16

typedef struct letter_array {
    char** letters;
    struct letter_array *ltr_ptr;
} larray;

void function1 (larray * letter1);  // here you just need a pointer to the structure
int function2 (larray letter2[][16]);
int function3 (larray letter3[][16]);
void function4 (int hor_ans, int ver_ans);

int main (void)
{
    larray letter_list;
    int vert, hori;

    letter_list.letters = malloc(ROW*sizeof(int*)); // allocate memory to the char pointer in the structure

    for(vert = 0; vert<ROW; vert++)
    {
        letter_list.letters[vert] = malloc(COL*sizeof(int));  // allocate the second 2D
    }


    printf("\n \t\t\t *** Hello! ***");


    printf("\n This program will create a random selection of 180 upper-case"
       " characters. \n\n");

    function1(&letter_list); //Problem #1 pass a pointer to the structure


    printf("\n\nThank you for using my random character array program. \n"
       "\t\t Have a good day! \n");

    return ( 0 ) ;
}


void function1 (larray *letter1) // just needs a pointer to the structure
{
    int i, z;

    srandom((unsigned)time(NULL));

    for(i=0; i<ROW; i++)   // used ROW
    {
        //letter1->letters[i] <- (int*) funptr;     // Problem #2-3  this line not needed as near as i can tell
        for(z=0; z<COL; z++)  // used COL
        {
            letter1->letters[i][z] = random( )%26+'A';  // Problem #4 dereference pointer to member char **
            printf("%c ", letter1->letters[i][z]);
        }
        printf("\n");
    }

    return ;
}

【讨论】:

  • 你是对的!我在程序的错误位置使用了取消引用指针。你有我可以用来回顾这个概念的链接/参考吗?谢谢!
猜你喜欢
  • 2013-03-06
  • 1970-01-01
  • 2014-05-14
  • 2016-11-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-12
相关资源
最近更新 更多