Tag: 数组

的示例

我找不到方法的有效例子[NSDictionary getObjects:andKeys:] 。 我能找到的唯一例子 ,不编译。 我提供了错误/警告,以防有人搜索它们。 我感到困惑的原因是因为NSDictionary上的大多数方法都返回NSArray 。 但是,在文档中它声明此方法的out变量作为C数组返回。 以下是您按照链接示例可能获得的错误消息/警告: NSDictionary *myDictionary = …; id objects[]; // Error: Array size missing in ‘objects’ id keys[]; // Error: Array size missing in ‘keys’ [myDictionary getObjects:&objects andKeys:&keys]; for (int i = 0; i < count; i++) { id obj = objects[i]; id key = keys[i]; } 。 […]

如何在预先分配的内存上指向2D / 3D空间

我内存优化了我用于嵌入式使用的代码。 它运行良好,但结果是我获得了大量的1D,2D和3D mallocs,并在函数中间释放,从而减慢了执行时间。 出于几个原因,我决定修改我的方式。 我想在执行开始时用一个malloc分配所有可用的内存,并指出每个数组需要的正确内存空间。 有关信息,我现在在x86上执行此操作,所以我没有任何内存空间问题。 我用这种方式声明我的数组: unsigned char *memory; memory = (unsigned char *)malloc(MYSIZE*sizeof(unsigned char)); type* p1; p1 = (type *)((int)memory + p1_offset); type** p2; p2 = (type **)((int)memory + p2_offset); for (int i=0 ; i<p2_height ; i++) { p2[i] = (type *)((int)p2 + p2_width*sizeof(type)); } 虽然它适用于我的1D指针,但它为我的2D指针声明返回了一个段错误。 我检查了我的偏移量,它们与内存指针相比很好。 由于我没有经验这种方式来宣布我的指针,也许我在这里误解了一些东西,所以如果有人可以向我解释更多关于这种技术的话,我会很高兴!

如何在C中创建字符串数组?

我正在从一本书中自学C,我正在尝试创建一个填字游戏。 我需要创建一个字符串数组但仍然遇到问题。 另外,我不太了解数组…… 这是代码片段: char word1 [6] =”fluffy”, word2[5]=”small”,word3[5]=”bunny”; char words_array[3]; /*This is my array*/ char *first_slot = &words_array[0]; /*I’ve made a pointer to the first slot of words*/ words_array[0]=word1; /*(line 20)Trying to put the word ‘fluffy’ into the fist slot of the array*/ 但我不断收到消息: crossword.c:20:16: warning: assignment makes integer from pointer without a cast […]

&C中数组的运算符定义

最近的一个问题促成了以数组和指针为中心的讨论。 问题是参考scanf(“%s”, &name) vs scanf(“%s”, name) 。 对于以下代码,Microsoft实际上在VS2010(也许是早期版本?)中为您解决了这个问题, #include int main() { char name[30]; printf(“Scan \”name\” – “); scanf(“%s”, name); printf(“Print \”&name\” – %s\n”, &name); printf(“Print \”name\” – %s\n”, name); printf(“Pointer to &name – %p\n”, &name); printf(“Pointer to name – %p\n”, name); printf(“\n\n”); printf(“Scan \”&name\” – “); scanf(“%s”, &name); printf(“Print \”&name\” – %s\n”, &name); printf(“Print […]

高速缓存内存优化数组转置:C

typedef int array[2][2]; void transpose(array dst, array src) { int i, j; for (j = 0; j < 2; j++) { for (i = 0; i < 2; i++) { dst[i][j] = src[j][i]; } } } src数组从地址0开始,dst数组从地址0x10开始。 L1数据缓存,直接映射,写分配,8字节块大小。 缓存总大小为16个数据字节。 src和dst数组的每个条目的命中或错过是什么? 答案是: src: [0][0] -> miss, [0][1] -> miss, [1][0] -> miss, [1][1] -> hit dst: […]

printf()打印整个数组

假设我的C程序中有以下代码: #include void PrintSomeMessage( char *p ); int main(int argc, char *argv[]) { char arr[10] = “hello”; PrintSomeMessage(&arr[0]); return 0; } void PrintSomeMessage(char *p) { printf(“p: %s”,p); } 为什么输出的结果是“你好”而不是单个字符“h”? 但我明白,如果我在格式化程序中放入”%c” ,它将只打印一个字母。 但是,此地址中每个字母的内存地址都不同。 请有人向我解释一下吗?

如何将多维C数组传递给函数?

我正在学习大学课程中的C和指针,我认为除了多维数组和指针之间的相似性之外,我对这个概念有很好的把握。 我认为,因为所有数组(甚至是多维数组)都存储在连续的内存中,你可以安全地将它转换为int* (假设给定的数组是一个int[] 。)但是,我的教授说明星中的星数。定义取决于数组中的维数。 因此, int[]将成为int* , int[][]将成为int**等。 所以我写了一个小程序来测试这个: void foo(int** ptr) { } void bar(int* ptr) { } int main() { int arr[3][4]; foo(arr); bar(arr); } 令我惊讶的是,编译器在两个函数调用上发出警告。 main.c:22:9: warning: incompatible pointer types passing ‘int [3][4]’ to parameter of type ‘int **’ [-Wincompatible-pointer-types] foo(arr); ^~~ main.c:8:16: note: passing argument to parameter ‘ptr’ here void foo(int** […]

“EXC_BAD_ACCESS:无法恢复以前选择的帧”错误,数组大小?

我有一个算法来创建Eratosthenes的筛子并从中拉出素数。 它允许您输入筛子的最大值,算法为您提供低于该值的质数并将它们存储在c样式数组中。 问题:一切正常,值高达500.000,但是当我输入一个大值 – 运行时 – 它在xcode中给出了以下错误信息: Program received signal: “EXC_BAD_ACCESS”. warning: Unable to restore previously selected frame. Data Formatters temporarily unavailable, will re-try after a ‘continue’. (Not safe to call dlopen at this time.) 我的第一个想法是我没有使用足够大的变量,但是当我使用’unsigned long long int’时,这应该不是问题。 此外,调试器将我指向我的代码中的一个点,其中数组中的一个点被赋值。 因此我想知道arrays有最大限制吗? 如果是:我应该使用NSArray吗? 如果不是,那么基于此信息导致此错误的原因是什么? 编辑:这是代码的样子(它不完整,因为它在发布的最后一行失败)。 我正在使用垃圾收集。 /*————————–SET UP————————–*/ unsigned long long int upperLimit = 550000; // […]

结构与未知大小的结构数组

我一整天都试图绕过这个…… 基本上,我有一个名为State的结构,它有一个名称,另一个名为StateMachine,带有名称,状态数组和添加的状态总数: #include #include typedef struct State { const char * name; } State; typedef struct StateMachine { const char * name; int total_states; State ** states; } StateMachine; StateMachine * create_state_machine(const char* name) { StateMachine * temp; temp = malloc(sizeof(struct StateMachine)); if (temp == NULL) { exit(127); } temp->name = name; temp->total_states = 0; […]

处理链表数组

我的方法: 每个元素的固定长度(假设为20)数组是指向链表的第一个节点的指针。 所以我有20个不同的链表。 这是结构: struct node{ char data[16]; struct node *next; }; 我对该数组的声明 struct node *nodesArr[20]; 现在要将一个新节点添加到其中一个链表,我这样做: struct node *temp; temp = nodesArr[i]; // i is declared and its less than 20 addNode(temp,word); // word is declared (char *word) and has a value (“hello”) addNode函数: void addNode(struct node *q, char *d){ if(q == NULL) q […]