一开始我想,"他们需要使用指针算术,这样对象就不会被'[]'运算符自动解引用了。
然后我意识到,不...C语言中的数组没有空槽位。
我得出结论,提问者正在:
使用结构体数组。
将其用作结构体指针数组。
peoro的解决方案非常好。但我建议稍微修改一下。
如果您想采用简单/懒惰的方式,请在结构体中添加一个".exists"属性。
简单并不是坏事,机器中的零件越多,出错的可能性就越大。
以下代码演示了两件事:
使用.peoro标志修改的解决方案伪造稀疏数组。
使用双指针的实际稀疏数组。
#include
#include
#include
int main( void ){
printf("[BEG:main]\n");
typedef struct MyStruct{
int whatever;
} MyStruct;
int num = 16; //:sixteen_elements
//:USE CALLOC HERE! If you use malloc you'll
//:end up with something even worse than
//:null pointers... Pointers that point to
//:random places in memory.
//:
//: It will make your:
//: if( arr[i] != NULL )...
//: look before you leap check worthless.
MyStruct** arr =(
calloc(
1 //:allocating 1 item: arr
//:Amount of memory taken up by
//:all 16 MyStruct pointers in array.
, sizeof(MyStruct*)*num
)
);;
//:Initialize only the EVEN slots:
for(int i = 0; i < num; i+=2 ){
//:Create new MyStruct in slot i,
//:initialized with junk data.
arr[i]= malloc(sizeof(MyStruct));
};;
//:If element not null, set it's whatever:
for(int i = 0; i < num; i++){
if(NULL != arr[i]){
arr[i] -> whatever = i;
};;
};;
//:Loop and print to confirm:
for(int i = 0; i < num; i++){
if(NULL != arr[i]){
printf("whatever: %d\n", arr[i] -> whatever);
};;
};;
//:ALTERNATIVELY:
//:If we were going to use peoro's method,
//:I would advise adding a ".exists" flag
//:to your struct.
typedef struct DoublePointersAreTooMuchWork{
int exists;
//:Because we are going to use malloc this
//:time, we have no guarantee what this
//:value will be. but you will probably
//:see all of them == 0. If you set
//: num=1000 you'll probably see a non-zero
//: entry somewhere. But, no guarantees!
int mystery_value;
} MyStruct02;
MyStruct02* arr2 = malloc(sizeof(MyStruct02)*num);
for(int i = 0; i < num; i++ ){
if( i%2 ){ //:evens
arr2[i].exists = 1;
}else{
arr2[i].exists = 0;
};;
};;
for(int i = 0; i < num; i++ ){
if( arr2[i].exists ){
printf("Exists:val:%d\n", arr2[i].mystery_value);
}else{
printf("[Pretend_I_Dont_Exist]\n");
};
}
printf("[END:main]\n");
} //[[main]____________________________________]//
/** ****************************************** ***
OUTPUT:
[BEG:main]
whatever: 0
whatever: 2
whatever: 4
whatever: 6
whatever: 8
whatever: 10
whatever: 12
whatever: 14
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[Pretend_I_Dont_Exist]
Exists:val:0
[END:main]
*** ****************************************** **/
趁我在这里。如果你想从命令行运行,请将文件命名为:"NAE.C99",然后创建一个称为"NAE.SH"的bash文件并将其放入其中。
双击脚本以运行它,或者在git bash终端中使用"./NAE.SH"。
##################################################
############################# SC[ hkmf-strict ] ##
##################################################
base_name_no_extension="NAE"
##################################################
MY_COMMAND_STRING=$(cat << GCC_COMMAND_01
gcc
-x c
-c $base_name_no_extension.C99
-o my_object_file.o
-m64
GCC_COMMAND_01
)
C=$MY_COMMAND_STRING ############################
C=$C"-Werror " ## WarningsAreErrors ##
C=$C"-Wfatal-errors " ## StopAtFirstError ##
C=$C"-Wpedantic " ## UseStrictISO_C ##
C=$C"-Wall " ## WarnAboutAnyWeirdCode ##
C=$C"-Wextra " ## "-Wall" WarningsExtra ##
C=$C"-std=c99 " ## VersionOf_C_ToUse ##
MY_COMMAND_STRING=$C ############################
echo $MY_COMMAND_STRING
$MY_COMMAND_STRING
C1=" gcc -o EXE.exe my_object_file.o "
C2=" ./EXE.exe "
C3=" rm my_object_file.o "
C4=" rm EXE.exe "
$C1 && echo "OK:"$C1 || "FAIL:$C1"
$C2 && echo "OK:"$C2 || "FAIL:$C2"
$C3 && echo "OK:"$C3 || "FAIL:$C3"
$C4 && echo "OK:"$C4 || "FAIL:$C4"
##################################################
read -p "[END_OF_BUILD_SCRIPT:PressAnyKey]:"
##################################################
############################# SC[ hkmf-strict ] ##
##################################################
顺便说一下,这是C99代码。我尽量避免使用任何C99特定的功能来编写它。