尝试用using结构初始化和填充一个2D字符数组



我要做的是使用一个结构来创建和显示函数'function1()'中的2D字符数组。这个数组将被发送回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 ttt *** Hello! ***");

    printf("n This program will create a random selection of 180 upper-case"
           " characters. nn");
    function1(&letter_list); //Problem #1

    printf("nnThank you for using my random character array program. n"
           "tt 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. 错误:unary -
  4. 类型参数错误
  5. 警告:赋值使指针从整数转换而不强制转换

希望这对你有帮助。

#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 ttt *** Hello! ***");

    printf("n This program will create a random selection of 180 upper-case"
       " characters. nn");
    function1(&letter_list); //Problem #1 pass a pointer to the structure

    printf("nnThank you for using my random character array program. n"
       "tt 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 ;
}

相关内容

  • 没有找到相关文章

最新更新