二维数组声明 - 目标C [英] 2D Array Declaration - Objective C

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

问题描述

有没有申报整数的二维数组中的两个步骤的方法吗?我有一个范围的问题。这就是我要做的:

  //我知道Java,所以这是什么,我试图复制一个例子:int数组[] [];
阵列=新INT [10] [10];

现在,在OBJ-C我想要做类似的事情,但我不能得到正确的语法。现在我把它一步到位,但我不能用它的if语句之外,其中我现在有它:

  int数组[10] [10]; //这是基于一个例子,我在网上找到,但我需要
                   //定义上比分配一个单独的行中的大小

谁能帮我这个?我知道它可能是一个更基本的问题,但你不能使用邮件的关键字新之外(据我所知),你不能将消息发送到整数。 (

的* 编辑1: 的**

我的问题是有关的范围。

  //不知何故声明数组
阵列[] [] //我知道这是不是有效的,但我需要它没有大小// if语句
如果(条件)
阵列[1] [2]
其他
阵列[3] [4]//我需要访问它的国际单项体育联合会之外// ...后来在code
阵列[0] [0] = 5;


解决方案

这是我的$ P $创建一个二维数组的方式pferred,如果你知道某个边界的大小:

  INT(* myArray的)[DIM2];myArray的=释放calloc(DIM1,sizeof的(* myArray的));

和它可以在一个呼叫被释放:

 免费(myarray的);

不幸的是,界必须固定为这个工作。

不过,如果你不知道任何界限的,这应该工作太:

 静态内联INT ** create2dArray(INT W,INT高)
{
    为size_t大小= sizeof的(INT)* 2 + W *的sizeof(INT *);
    INT ** ARR =的malloc(大小);
    为int *的大小=(INT *)改编;
    尺寸[0] = W;
    尺寸[1] = H;
    ARR =(INT **)(尺寸+ 2);    的for(int i = 0; I<瓦;我++)
    {
        改编[I] =释放calloc(H,sizeof的(** ARR));
    }    返回ARR;
}静态内嵌无效free2dArray(INT ** ARR)
{
     为int *的大小=(INT *)改编;
     INT W =尺寸[-2]
     INT H =尺寸[-1];     的for(int i = 0; I<瓦;我++)
         免费(ARR [I]);     免费(安培;大小[-2]);
}

Is there a way to declare a 2D array of integers in two steps? I am having an issue with scope. This is what I am trying to do:

//I know Java, so this is an example of what I am trying to replicate:

int Array[][];
Array = new int[10][10];

Now, in OBJ-C I want to do something similar, but I cant get the syntax right. Right now I have it in one step, but I cannot use it outside of the If-Statement in which I currently have it:

int Array[10][10]; //This is based on an example I found online, but I need 
                   //to define the size on a seperate line than the allocation

Can anyone help me out with this? I know its probably a more basic question, but you can't use the keyword "new" outside of a message (to my knowledge) and you cant send messages to ints. :(

*EDIT 1:**

My problem is scope related.

//Declare Array Somehow
Array[][] //i know this isn't valid, but I need it without size

//if statement
if(condition)
Array[1][2]
else
Array[3][4]

//I need to access it outside of those IFs

//... later in code
Array[0][0] = 5;

解决方案

This is my preferred way of creating a 2D array, if you know the size of one of the boundaries:

int (*myArray)[dim2];

myArray = calloc(dim1, sizeof(*myArray));

And it can be freed in one call:

free(myArray);

Unfortunately, one of the bounds MUST be fixed for this to work.

However, if you don't know either of the boundaries, this should work too:

static inline int **create2dArray(int w, int h)
{
    size_t size = sizeof(int) * 2 + w * sizeof(int *);
    int **arr = malloc(size);
    int *sizes = (int *) arr;
    sizes[0] = w;
    sizes[1] = h; 
    arr = (int **) (sizes + 2);

    for (int i = 0; i < w; i++)
    {
        arr[i] = calloc(h, sizeof(**arr));
    }

    return arr;
}

static inline void free2dArray(int **arr)
{
     int *sizes = (int *) arr;
     int w = sizes[-2];
     int h = sizes[-1];

     for (int i = 0; i < w; i++)
         free(arr[i]);

     free(&sizes[-2]);
}

这篇关于二维数组声明 - 目标C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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