结构体在C二维数组 - 如何声明和使用 [英] Two Dimensional Array of Structs in C - how to declare and use

查看:186
本文介绍了结构体在C二维数组 - 如何声明和使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图得到一个非常简单的想法,在C工作,但我甚至无法获得语法下来。结果
该计划将采取从命令行一些投入并利用它们来决定做什么尺寸结构的2维数组。结果

然后,在for循环型的情况,我希望能够基于用户输入更改特定unsigned char型。因此,首先我建立结构的二维数组,包含无符号字符数组,以及一个名称为每个结构,然后我可以写这些。

命令行参数是三根弦。 第一个是行数,第二个是列数,第三个是每个结构中是无符号的字符数组的大小。

 #包括LT&;&stdio.h中GT;
#包括LT&;&stdbool.h GT;
#包括LT&;&MATH.H GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&string.h中GT;
    typedef结构{
    无符号字符*数据;
    字符*名称;
    } MYSTRUCT;    MYSTRUCT * myArr,该[] [];    INT主(INT ARGC,CHAR *的argv [])
    {
       INT Y =的atoi(argv的[0]);
       INT X =的atoi(ARGV [1]);
       INT Z =的atoi(argv的[2]);
       MYSTRUCT * myArr,该[X] [Y]      的for(int i = 0; I< X,我++)
     {
         对于(INT J = 0; J< Y; ​​J ++)
        {
           myArr,该[I] [J] =(MYSTRUCT *)malloc的(的sizeof(MYSTRUCT));
           myArr,该[I] [J]。数据=(无符号字符*)malloc的(sizeof的(无符号字符)* Z);
           myArr,该[I] [J]。名称=(字符*)malloc的(的sizeof(char)的* 64);
        }
     }     //第二部分 - 改变myArr,该的一些入门根据来自用户输入
     //假设输入已采取正确
     INT H = 5; //第5行中myArr,该
     INT K = 7;在myArr,该//第7列
     INT P = 9;在myArr,该[H] [K]。数据// 9 unsigned char型
     unsigned char型U = 241; //值写入
     字符*了newName =鲍勃;
     myArr,该并[h] [k]的。数据[P] = U;
     myArr,该并[h] [k]的。名称=了newName;     返回0;
    }

这code不起作用。事实上,它不能编译。可能有人帮助我吗?我只是想了解指针作为一种过渡从Java到C.这是什么样的事情,我会能够在Java中做的非常很容易,我希望能够做到这一点在C作为好。我听说一个2 D阵列外主要被宣布所以它是全球范围的,因为堆栈的大小限制和结构的二维数组可能只是填充它。有人可以解释的是,太?而我怎么会声明一个二维数组的大小在运行时确定的?

我打算用C和C仅这一点,太。所以,请不要为我提供C ++语法提示。谢谢你。


解决方案

  myArr,该[H] [K]。数据[P] = U;
 myArr,该并[h] [k]的。名称=了newName;

  myArr,该[H] [K]  - >数据[P] = U;
 myArr,该[H] [K] - >名称=了newName;

,因为它们是指针。在你的循环出现同样的错误。

  MYSTRUCT * myArr,该[] [];

这行是无效的和毫无意义的,因为在您重新定义它在main()。

如果你想分配一个动态数组,你可以使用:

  MYSTRUCT ** myArr,该=的malloc(X * Y * sizeof的(** myArr,该));

I've been trying to get a very simple idea to work in C but I can't even get the syntax down.
This program would take in some inputs from the command line and use them to decide what size to make a 2 dimensional array of structs.

Then, in a for-loop type situation, I'd like to be able to change a specific unsigned char based on user input. So first I build a 2D array of structs, each struct containing an array of unsigned chars as well as a name, and then I am able to write to these.

The command line arguments are three strings. First one is number of rows, second one is number of columns, third one is size of unsigned char array within each struct.

#include<stdio.h>
#include<stdbool.h>
#include<math.h>
#include<stdlib.h>
#include<string.h>
    typedef struct {
    unsigned char* Data;
    char* name;
    } myStruct;

    myStruct* myArr[][];

    int main(int argc, char *argv[])
    {
       int y = atoi(argv[0]);
       int x = atoi(argv[1]);
       int z = atoi(argv[2]);
       myStruct* myArr[x][y];

      for (int i = 0; i < x; i++)
     {
         for(int j = 0; j < y; j++)
        {
           myArr[i][j]=(myStruct*) malloc(sizeof(myStruct));
           myArr[i][j].Data=(unsigned char*)  malloc(sizeof(unsigned char)*z);  
           myArr[i][j].name = (char*) malloc(sizeof(char)*64);
        }
     }

     //part II - change some entry of myArr based on input from user
     //assume input has been taken in correctly
     int h = 5;//5th row in myArr
     int k = 7; // 7th column in myArr
     int p = 9; //9th unsigned char in myArr[h][k].Data
     unsigned char u = 241;//value to be written
     char* newName = "bob";
     myArr[h][k].Data[p]=u;
     myArr[h][k].name=newName;

     return 0;
    }

This code does not work. In fact, it does not compile. Could someone help me? I am simply trying to understand pointers as a way to transition from java to C. This is the kind of thing I'd be able to do very very easily in Java and I want to be able to do it in C as well. I've heard that a 2 D array has to be declared outside main so it is global in scope, because the stack is limited in size and a 2D array of structs just might fill it. Can someone explain that, too? And how I would declare a 2D array whose size is determined at run time?

I intend to use C and only C for this, too. So please don't provide me with C++ syntax tips. Thanks.

解决方案

 myArr[h][k].Data[p]=u;
 myArr[h][k].name=newName;

should be

 myArr[h][k]->Data[p]=u;
 myArr[h][k]->name=newName;

because they are pointers. The same error occurs in your loops.

myStruct* myArr[][];

This line is invalid and meaningless, because you re-define it in the main().

If you want to allocate a dynamical array, you can use:

myStruct **myArr = malloc(x * y * sizeof(**myArr));

这篇关于结构体在C二维数组 - 如何声明和使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆