Tag: 数组

‘C’分段故障与2darrays

谁能解释一下为什么这段代码不起作用? #include #include void findAndPrint(char *arr[], int year, int month, int day); int main() { char *dayTab[] = { {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} }; findAndPrint(dayTab, 3, 3, 3); getchar(); return 0; } void findAndPrint(char *arr[], […]

C – 合并合并排序的一部分

我是新来合并排序,我正在尝试创建一个。 我的合并排序不是排序我发送它的数组,我无法弄清楚为什么。 这里是所有代码http://pastebin.com/M4RUzhUa的链接 这是我的mergesort函数 void merge_sort(int array[], int low, int high) { int middle = (low + high) / 2; if(low < high) { merge_sort(array, low, middle); merge_sort(array, middle+1, high); merge(array, low, middle, high); } } 这是我的(更新的)合并function void merge(int array[], int low, int middle, int high) { int size,left,right,i, j; size = high – low […]

将char数组转换为字符串

我有一个char数组,我试图将其转换为指向字符串的char指针。 我相信这涉及获取指向char数组的第一个元素的指针,并在char数组的末尾添加一个空字符。 这样做的原因是我试图将它传递给Pebble智能手表的SimpleMenuItem ,其中.title需要获取一个char* ,指向一个字符串。 虽然我已经能够填充char数组并且(我认为)添加了null字符并获得了指针,但我无法在我的卵石上看到标题。 我不确定这是一个卵石问题还是我的C理解问题,但我觉得它可能是前者。 卵石代码(C): void in_received_handler(DictionaryIterator *received, void *context) { dataReceived = dict_read_first(received); APP_LOG(APP_LOG_LEVEL_DEBUG, “read first”); while (dataReceived != NULL){ if (dataReceived->key == 0x20) { //original is of the format “# random string”, ie “4 test name” char* original = dataReceived->value->cstring; APP_LOG(APP_LOG_LEVEL_DEBUG, original); char originalArray[strlen(original)]; //copy over to originalArray for (unsigned […]

将数组增加到一定数量

我有一个整数数组(代表一个4位数字),我需要递增,以便每个整数永远不会高于3.基本上,它需要打印每个4位数字,其中没有4或更高。 这是我期望与实际输出相比的输出: Expected: 0000 0001 0002 0003 0010 0011 0012 0013 0020 0021 0022 …. 3333 Received: 0000 1000 2000 3000 3100 3200 3300 3310 3320 3330 3331 3332 我知道我的算法搞砸了,但我不知道该怎么做: int i, c[4]; memset(c, 0, sizeof(c)); i = 0; while (1) { testprint(c); c[i]++; if (c[i] == 3) i++; if (i == 3) break; } […]

超出索引C / C ++

C / C ++中数组的典型声明是: 类型名称[elements]; 其中type是有效类型(例如int,float …),name是有效的标识符,elements字段(始终用方括号[]括起来),用数字表示数组的长度元素。 所以我声明一个int数组有2个元素 int a[2]; a[3] =4; 为什么这不会抛出exception?

传递矩阵作为参数

我是c的新手,我正在使用CUDA编译器。 我想声明五个矩阵,从[1] [1]到[5] [5]。 用随机值初始化它们。 打印这些值。 我已经展示了我写的全部代码。 我遇到了问题 传递矩阵作为参数 初始化矩阵(应该是常量) #include #include void printMatrix(int **a, int rows, int cols) { for(int i=0; i<rows; i++){ for (int j=0; j<cols; j++){ printf("%d\t", a[i][j] ); } printf("\n"); } } int main() { int N; for (int n=0; n<=5; n++) N = n; int a[N][N]; for(int i=0; i<N; i++) […]

将char *拆分为char * Array

我正在尝试将char *拆分为C中的char *数组。我习惯用Java / PHP OO编程。 我知道在这些语言中有几种简单的方法,但在C语言中……我完全迷失了。 我经常有几个小时的段错误x) 我正在使用TinyXML并从XML文件中获取信息。 这是我们找到数组的结构。 const int MAX_GATES = 64; typedef struct { char *name; char *firstname; char *date; char *id; char *gates[MAX_GATES]; } UserInfos; 这是我填充此结构的地方: UserInfos * infos = (UserInfos*)malloc(1024); infos->firstname = (char*)malloc(256); infos->name = (char*)malloc(128); infos->id = (char*)malloc(128); infos->date = (char*)malloc(128); sprintf(infos->firstname, “%s”, card->FirstChild(“firstname”)->FirstChild()->Value()); sprintf(infos->name, “%s”, card->FirstChild(“name”)->FirstChild()->Value()); sprintf(infos->date, […]

数组元素是C中字符串数组的变量

我正在尝试在C中初始化一个字符串数组。我想从一个变量设置数组的一个元素,但我得到一个编译器错误。 这有什么问题? char * const APP_NAME = “test_app”; char * const array_of_strings[4] = { APP_NAME, “-f”, “/path/to/file.txt”, NULL }; 错误是error: initializer element is not constant 。

在字符串数组中排序单词

我的程序旨在允许用户输入一个字符串,我的程序将输出每个字母和单词的出现次数。 我的程序还按字母顺序排序。 我的问题是:我输出所看到的单词(第一个未排序)及其出现的表格,在我的表格中我不想重复。 解决了 例如,如果单词“to”被看到两次,我只想让“to”这个单词在我的表中只出现一次,输出出现次数。 我怎样才能解决这个问题? 另外,为什么我不能简单地将string[i] == delim为应用于每个分隔符,而不是必须为每个分隔符手动分配它? 编辑:修复了我的输出错误。 但是,我如何设置string[i]的条件等于我的代码中的任何分隔符,而不仅仅是为空格键工作? 例如,在我的输出中,如果我输入“你,你”,它将输出“你,你”,而不仅仅是“你”。 如何编写它以删除逗号并将“你,你”比作一个单词。 任何帮助表示赞赏。 我的代码如下: #include #include #include const char delim[] = “, . – !*()&^%$#@ ? []{}\\ / \””; #define SIZE 1000 void occurrences(char s[], int count[]); void lower(char s[]); int main() { char string[SIZE], words[SIZE][SIZE], temp[SIZE]; int i = 0, j = 0, […]

C中的二维数组指针访问分段故障

我正在尝试用C语言编写Conway的生命游戏代码。我有一些包含我的宇宙的2D数组的问题。 我将向您展示一些导致我出现问题的代码。 实际上有一个我无法处理的问题。 #include #include #include “file2ppm.h” typedef enum { false, true } bool; void *allocateMemoryFor2DArray(int x, int y); //allocate memory for 2D array void initializeUniverseFromFile(char* path, int x, int y, int array[x][y]); //set values of cells from a file void initializeUniverseRandomly(int x, int y, int array[x][y]); //set random values int getNumberOfColumns (FILE *file); //get […]