English 中文(简体)
将阵列替换为C中另一阵列
原标题:Replace array with another array in C

从纯理性来看,我开始以我以前从未使用过的方式玩弄各种阵容。 我尝试制作一个数据结构阵列,使之与另一个数据组相同:

typedef struct _test {
   float value;
} test;

简单明了<代码>struct,因此我尝试:

test struct1[10];
test struct2[20];
struct1 = struct2;

我认为这样做会奏效,甚至没有汇编。 但是,我非常关心这一利益。 在复制数据的同时,是否有可能将10个阵列增加到20个?

www.un.org/Depts/DGACM/index_spanish.htm 目标-C

我实际上是在目标C下这样做的,因此,我也想听取目标C人民的意见。 我想看到,是否有可能在本文件中改变<代码>struct1的大小。

@interface Object : NSObject {
    test struct1;
}

<>Remember: 这只是出于好奇,因此,一切都可以讨论。

问题回答

但是,与你的问题并不完全相关但令人感兴趣的其他一点是,虽然不能分配阵列,但含有阵列的构件(can>/em>)被分配到:

struct test
{
    float someArray[100];
};


struct test s1 = { /* initialise with some data*/ };
struct test s2 = { /* initialise with some other data */ };

s1 = s2; /* s1 s array now contains contents of s2 s array */

这还使得能够从职能中收回固定的超负荷数据(因为不允许返回的平原阵列):

struct test FunctionThatGenerates100Floats(void)
{
    struct test result;
    for (int i = 0; i < 100; i++)
        result.someArray[i] = randomfloat();

    return result;
}

As others have said, arrays allocated like that are static, and can not be resized. You have to use pointers (allocating the array with malloc or calloc) to have a resizable array, and then you can use realloc. You must use free to get rid of it (else you ll leak memory). In C99, your array size can be calculated at runtime when its allocated (in C89, its size had to be calculated at compile time), but can t be changed after allocation. In C++, you should use std::vector. I suspect Objective-C has something like C++ s vector.

但是,如果你想要复制C中一个阵列的数据,则使用<代码>memcpy:

/* void *memcpy(void *dest, const void *src, size_t n)
   note that the arrays must not overlap; use memmove if they do */
memcpy(&struct1, &struct2, sizeof(struct1));

这只抄录了头10项内容,当然,因为第1项内容很长。 您可通过修改<代码>和格式;struct2至struct2+10和(pruct2[10])复制最后10(例如)。 当然,在C,不停在阵列的末尾是你的责任:memcpy。 没有检查。

你们也能够清楚地看到 lo,但肉类常常会更快(而且永远不会放慢)。 这是因为汇编者能够利用它所知道的每个滴子(例如,它可能知道如何在某个时候用批量复制你的16项数据,即使每个要素只有1项是粗略的)。

你们可以在C中用静态阵列来这样做,但你可以用动态分配阵列这样做。 例如,

float *struct1, *struct2, *struct3;
if(!(struct1 = malloc(10 * sizeof(float))) { 
  // there was an error, handle it here
}
if(!(struct2 = realloc(struct1, 20 * sizeof(float))) {
  // there was an error, handle it here
  // struct1 will still be valid
}
if(!(struct3 = reallocf(struct2, 40 * sizeof(float))) {
  // there was an error, handle it here
  // struct2 has been free d
}

在C,我认为,利用真正职能是一个好的地方。 然而,它只能以有活力的阵列开展工作。 声明所分配的<代码>struct1>的记忆没有任何改变:test ruct1[10];

在C阵列中,你可以根本改变其价值(即地址),你可以转而使用。

显然,如果你宣布其阵列具有固定规模,那么<代码>试验方向1[10],则不能再作改动。 你们需要做的是宣布它为一点点:

test *struct1;

然后,你必须使用<条码>小型来分配阵列,并可使用<条码>realloc在保留原阵列内容时加以改装。

struct1 = malloc(10*sizeof(*struct1));
//initialize struct1 ...
test *struct2 = realloc(struct1, 20*sizeof(*struct1));

如果你重新使用目标C,你就知道你只能使用NSMutableArray,后者自动进行真正的trick滴,以重新定位,储存你提出的许多物品,从而增加你的记忆。

But you re trying to do this with struct? What would that even mean? Suppose you increase the amount of memory available to struct1 in Object. It s still a struct with one member, and doesn t do anything more.

是否打算 目标能够包含一个扩大的构件?

typedef struct _test2 {
    float value;
    NSObject *reference;
} test2;

但是,你通常仍然可以进入<条码>参考,因为它不是目标中已知的一部分。

Object *object2;
...
NSLog(@"%@", object2.struct1.reference); // does not compile

如果你知道你有改装的物体之一,你可以这样做。

Object *object2;
...
NSLog(@"%@", ((test2)(object2.struct1)).reference);

而且,你还可以将目标2移至期望反对的任何东西。 只有在第1部分是最后的反对成员的情况下,它才有机会工作,而没有带有分级反对的过错。

那么,一些类型的真正trick计可能会奏效,但我并不认为是真实的,因为这种 intended意在用于分配小块的物体,而C功能用于分配目标C中未暴露的物体的细节,因此,你不应假定这种.。 如果您凌驾于<条码><>>>。 那么,你也许能够确保使用小型水池。

你们还必须看到,在目标C中,目标C对存在物体的不止一位点人来说是常见的。 真菌可能移动物体,除非你纠正所有点子,否则该物体就会被 se弃。





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...