Tag: 数组

为什么数组索引从’0’开始

我对C / C ++数组有疑问。 为什么数组的索引从’0’开始而不是从’1’开始? 这有什么数学原因吗?

读取txt文件中的数字列表并以C格式存储到数组中

我有一个整数列表,每行一个数字,并希望将这些数字存储在一个整数数组中,以便稍后在程序中使用。 例如在java中你会做这样的事情: FileReader file = new FileReader(“Integers.txt”); int[] integers = new int [100]; int i=0; while(input.hasNext()) { integers[i] = input.nextInt(); i++; } input.close(); 如何在C中完成?

打印返回结构的成员

我在打印从函数返回的结构的成员时遇到问题: #include struct hex_string { char a[9]; }; struct hex_string to_hex_string_(unsigned x) { static const char hex_digits[] = “0123456789ABCDEF”; struct hex_string result; char * p = result.a; int i; for (i = 28; i >= 0; i -= 4) { *p++ = hex_digits[(x >> i) & 15]; } *p = 0; printf(“%s\n”, result.a); /* works […]

C多维数组中奇怪的数组语法

我知道这是真的: x[4] == 4[x] 多维数组的等价物是什么? 以下是真的吗? x[4][3] == 3[x[4]] == 3[4[x]]

C和C ++中的可变长度数组(VLA)

可能重复: 在文件范围内可变修改的数组 我有一些关于VLA及其行为的概念,我需要澄清一下。 自C99起AFIK可以将VLA声明为本地范围: int main(int argc, char **argv) { // function ‘main’ scope int size = 100; int array[size]; return 0; } 但它在全球范围内被禁止: const int global_size = 100; int global_array[global_size]; // forbidden in C99, allowed in C++ int main(int argc, char **argv) { int local_size = 100; int local_array[local_size]; return 0; } 上面的代码在C99中声明了一个VLA,因为const修饰符不会创建编译时值。 在C […]

指向多种类型的指针数组,C

使用malloc可以有多个类型的数组吗? 编辑: 目前我有: #include #include #include #define int(x) *((int *) x) int main() { void *a[10]; a[0] = malloc(sizeof(int)); int(a[0]) = 4; char *b = “yola.”; a[1] = malloc(strlen(b)*sizeof(char)); a[1] = b; printf(“%d\n”, int(a[0])); printf(“%s\n”, a[1]); } 但它很乱。 其他方法? 编辑:清理了一下。

可以在GCC中声明的静态数组的最大大小是多少?

怎么决定? 这取决于编译器/架构/主机系统吗? 例: int array[0x8000000000000000]; 对于x86_64位系统中的这一行,GCC输出: Error “size of array ‘array’ is too large”.

C:使用malloc扩展数组

我对malloc和C有点新手。 我想知道如果需要的话我可以用malloc扩展一个固定大小的数组的大小。 例: #define SIZE 1000 struct mystruct { int a; int b; char c; }; mystruct myarray[ SIZE ]; int myarrayMaxSize = SIZE; …. if ( i > myarrayMaxSize ) { // malloc another SIZE (1000) elements myarrayMaxSize += SIZE; } 上面的例子应该说明我想要完成的事情。 (顺便说一句:我需要这个用于我编写的解释器:使用固定数量的变量,如果需要更多变量,只需动态分配它们)

找到一对数字,其差别是未排序数组中的输入值’k’

正如标题中所提到的,我想找到差异为K的元素对 example k=4 and a[]={7 ,6 23,19,10,11,9,3,15} output should be : 7,11 7,3 6,10 19,23 15,19 15,11 我已经读过SO中的先前post“ 在数组中找到添加到给定总和的数字对 ” 为了找到有效的解决方案,需要多长时间? 时间复杂度是O(nlogn)还是O(n) ? 我试图通过分而治之的技术来做到这一点,但我没有得到退出条件的任何线索…… 如果一个有效的解决方案包括使用两个指针对输入数组进行排序和操作元素,那么我认为我应该采用最小的O(nlogn) … 是否存在任何与O(n)相关的数学相关技术。 任何帮助表示赞赏..

排序结构数组的成员

给定一个结构数组(在C中)我试图以数字顺序打印出性别组和子顺序的结果。 例如: struct employee{ char gender[13] char name[13]; int id; }; 假设我像这样定义结构数组: struct employee info[2]={{“male”,”Matt”,1234},{“female”,”Jessica”,2345},{“male”,”Josh”,1235}}; 我怎么能打印结果像 1234 Matt 1235 Josh 2345 Jessica