我有2个结构数组
myStruct leds_left[20];
myStruct leds_right[20];
我需要一种方法来组合两个结构,以便我可以像:
一样使用它*(structPtr[10]) // points to element of leds_left[10]
*(structPtr[30]) // points to element of leds_right[10]
我已经尝试了以下内容:
myStruct ** structPtr = (myStruct**) malloc((20 + 20) * sizeof(myStruct*));
structPtr[0] = &leds_right[15]; //just with one element for testing
*(structPtr[0]) = newValue;
我做错了什么?
答案 0 :(得分:2)
为了确保leds_left
和leds_right
真正分配在连续的内存中,它们必须“结构化”。这与union
结合使用可以提供您想要的内容:
#include <stdio.h>
typedef struct { int i; /* ... */ } myStruct;
static union {
struct {
myStruct leds_left[20];
myStruct leds_right[20];
};
myStruct leds_all[40];
} data;
int main()
{
for (int i = 0; i < 20; ++i) {
data.leds_left[i].i = 1 + i;
data.leds_right[i].i = -(1 + i);
}
for (int i = 0; i < 40; ++i) {
printf(" %d", data.leds_all[i].i);
}
printf("\n");
return 0;
}
在Windows 10(64位)上用cygwin中的gcc进行编译和测试:
$ gcc -std=c11 -o test-struct-array test-struct-array.c
$ ./test-struct-array.exe
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -11 -12 -13 -14 -15 -16 -17 -18 -19 -20
$
<强>注意!强>
打包和对齐可能会成为一个问题(取决于myStruct
实际包含的组件)。
答案 1 :(得分:0)
我刚刚找到了一个对我来说足够好的解决方案。因为使用组合数组的好方法是不可能的,我只是编写了一个函数,它正在检查索引,然后访问其中一个数组中的右元素。
apply plugin: 'com.android.application'
android {
packagingOptions {
exclude 'META-INF/DEPENDENCIES'
exclude 'META-INF/NOTICE'
exclude 'META-INF/LICENSE'
exclude 'META-INF/LICENSE.txt'
exclude 'META-INF/NOTICE.txt'
}
lintOptions {
disable 'MissingTranslation'
}
compileSdkVersion 25
buildToolsVersion "25.0.2"
defaultConfig {
applicationId "tech.provingground.divemonitor"
minSdkVersion 24
targetSdkVersion 25
versionCode 6
versionName "1.0.0-local"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs {
release {
storeFile file("[path/to/file]")
storePassword "[redacted]"
keyAlias "mobile keystore"
keyPassword "[redacted]"
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
signingConfig signingConfigs.release
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
wearApp project(':wear')
// https://mvnrepository.com/artifact/commons-io/commons-io
compile project(':commons')
compile 'com.google.android.gms:play-services-wearable:10.2.1'
compile 'com.google.android.gms:play-services-location:10.2.1'
compile 'commons-io:commons-io:2.5'
compile 'com.android.support.constraint:constraint-layout:1.0.1'
compile 'com.android.support:support-v4:25.3.1'
compile 'com.android.support:recyclerview-v7:25.3.1'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind
compile 'com.fasterxml.jackson.core:jackson-databind:2.9.0.pr2'
// https://mvnrepository.com/artifact/com.fasterxml.jackson.dataformat/jackson-dataformat-csv
compile 'com.fasterxml.jackson.dataformat:jackson-dataformat-csv:2.9.0.pr2'
testCompile 'junit:junit:4.12'
}
它并不完全解决我的问题,但它可以作为一种解决方法