Tag: 数组

strcpy()和字符串数组

我需要将用户的输入存储到字符串数组中。 #include #include #include char *history[10] = {0}; int main (void) { char input[256]; input = “input”; strcpy(history[0], input); return (EXIT_SUCCESS); } 在终端上运行它我得到一个分段错误,在NetBeans中我得到main.c:11:错误:赋值中不兼容的类型。 我还尝试将所有历史记录移动到最新输入到第一个位置(历史[0])。 history[9] = history[8]; history[8] = history[7]; history[7] = history[6]; history[6] = history[5]; history[5] = history[4]; history[4] = history[3]; history[3] = history[2]; history[2] = history[1]; history[1] = history[0]; history[0] = input; 但这导致这样的输出。 […]

我需要在C中创建一个全局数组,其大小由用户输入

基本上我需要在C中创建一个数组的全局变量。 数组将为[n] [22] [n + 1],其中n为3,4,5或6,由用户选择。 有没有办法做到这一点,或者我应该只做数组[6] [22] [7],并且处理它的函数只使用最多n的部分(如果这有意义)? 我之前必须要做一个计算机科学课,但不记得究竟该怎么做。

argv指向指针数组的指针

我很困惑以下段落如何与其后面的代码匹配: 由于argv是指向指针数组的指针 ,因此我们可以操作指针而不是索引数组。 下一个变体基于递增argv,它是指向char的指针,而argc倒计时: #include /* echo command-line arguments; 2nd version */ main(int argc, char *argv[]) { while (–argc > 0) printf(“%s%s”, *++argv, (argc > 1) ? ” ” : “”); printf(“\n”); return 0; } 不是char *argv[]只是一个指针数组? 指向数组的指针不会写为char *(*argv[])或类似的东西吗? 作为旁注,通常我发现混合数组和指针的声明相当混乱,这是正常的吗?

C相同结构大小不同

我的问题与这个问题有关: c在struct中定义具有不同大小的数组 但是,我不想使用动态分配(嵌入式目标)。 问题回顾: 在C中 ,我希望有两个相同结构的版本,每个版本的静态数组大小不同。 两个结构都将通过指针参数由相同的函数使用。 typedef struct { short isLarge; //set 0 at initialization short array[SIZE_A]; //more arrays } doc_t; typedef struct { short isLarge; //set 1 at initialization short array[SIZE_B]; //more arrays } doc_large_t; void function( doc_t* document ) { if ( document->isLarge ) { //change document into doc_large_t* [1] } //common […]

在Cython中返回一个结构数组

我试图在Cython中返回一个结构数组。 // .pyx from libc.stdint cimport uint8_t cdef extern from “”: cdef struct apriltag_detection: int id double c[2] double p[4][2] ctypedef apriltag_detection apriltag_detection_t cdef extern from “tag36h11_detector/tag36h11_detector.h”: apriltag_detection_t* scan_frame(int width, int height, uint8_t* data); cdef class Detection: # how do I “link” this to the struct defined above? def __cinit__(self): pass def __dealloc__(self): pass def […]

JNI Pass Char * 2Darrays到JAVA代码

我想通过C代码中的JNI层传递以下指针数组 char *result[MAXTEST][MAXRESPONSE] = { { “12”, “12”, “” }, { “8”, “3”, “” }, { “29”, “70”, “” }, { “5”, “2”, “” }, { “42”, “42”, “” } }; 在java代码中,我写了以下声明 public static native String[][] getResult(); 我很困惑如何将该数组通过JNI层传递给Java代码??? 以下是JNI层描述 JNIEXPORT jobjectArray JNICALL Java_com_example_CheckResult_getResult (JNIEnv *env, jclass thiz) { Confused over here ???? }

在C中为结构指针数组成员分配地址

一些指针算术有相当大的麻烦。 我想我得到了概念(指针变量指向内存地址,正常变量指向数据)但我相信我的问题是语法( *, &, (*), *(),等) 我想要做的是构建一个自定义结构的动态数组(即指向堆结构的指针数组),我的界面提供了两个方法,“ad_to_obj_array”(它将对象添加,而数组可以为null为空)和“obj_array_dustbin”(它只是处理数组,也处理内容,堆objs)。 前者呈现如下。 对象的细节并不重要(并且结构已经重命名)但是我对一般问题的解决方案如下,如果您能发现错误,我将不胜感激。 编译器抱怨无效的左值,我尝试将RHS上的指针中的地址分配给堆结构指针数组中的指针值: #define NUM_ELEM(x) (sizeof (x) / sizeof (*(x))) obj* add_to_obj_array(obj* new_obj, obj* array) { int number_of_elements = 0; if (array != NULL) { number_of_elements = NUM_ELEM(array); } obj* new_array = NULL; /* note: I am expecting sizeof(new_obj) to return the size of an obj* to go […]

如何动态地将字符添加到数组中? 没有预定义的arrays?

如果我想将char添加到char数组,我必须这样做: #include int main() { int i; char characters[7] = “0000000”; for (i = 0; i 2) { break; } } for (i = 0; i < 7; i++) { printf("%c\n", characters[i]); } return 0; } 为了防止打印任何奇怪的字符,我必须初始化数组,但它不灵活。 如何动态地将字符添加到char数组? 就像你在Python中一样: characters = [] characters.append(1) …

使用数组创建单链接列表时出现警告

#include typedef struct { int data; struct node *next; }node; void print(node *head) { node *tmp = head; while (tmp) { printf (“%d “, tmp->data); tmp = tmp->next; } } int main() { node arr[5] = { {1, &arr[1]}, {2, &arr[2]}, {3, &arr[3]}, {4, &arr[4]}, {5, NULL} }; print(arr); return 0; } 为什么在使用gcc -Wall编译时会收到这些警告? (即使没有-Wall,gcc会产生相同的警告) […]

传递一个常数矩阵

提到这个问题,特别是litb接受的答案 ,我想知道为什么gcc抱怨这个: void func(const int (*ip)[3]) { printf(“Value: %d\n”, ip[1][1]); } int main() { int i[3][3] = { {0, 1, 2} , {3, 4, 5}, {6, 7, 8} }; func(i); return 0; } 如果我消除了const ,编译器会保持静止。 我有什么误会吗? 我想确保func不修改我的数组。 编辑:如果我为我的矩阵定义数据类型,会发生同样的事情: typedef int Array[3][3]; void func(const Array *p) { printf(“Value: %d\n”, (*p)[1][1]); } int main() { Array a […]