Tag: 动态

创建数组的动态声明

我想要动态声明的make数组我想象像这样的somethnig。我想要make程序识别单词中的字符。 char i; scanf(“%c”,&i); char word[]=i; printf(“%c”,word[0]); 我也试过这样的事情 char i; scanf(%c,&i); char *word=i; printf(“%c”,word[0]); 我不知道如何使它工作

2d动态数组(在C中),每行具有特定的行号和不同的列大小

如何在每行中创建具有特定行号和不同列大小的2d动态数组(在C中)? 例如:这是一个数组(3 =行) |1 | 4 | 5 | |3 | |6 | 2 | 第1行 – 3列 第2行 – 1列 第3行 – 2列 我希望我的程序在运行时向用户询问每一行以给出cols的数量。 我如何制作这样的数组?

C读取字符串并动态接受括号

我要读一串括号来分析它。 如何读取要插入动态生成的数组中的字符串? 除了使用scanf的括号外,如何避免读取所有字符? [] {}() 谢谢。 编辑:我必须从键盘读取一系列括号,但我不知道长度。 所以我要创建一个动态生成的数组(这是一个要求),只包含括号的空格。 当我正在阅读时,我想只接受括号并避免使用所有其他字符,这是否可以与正则表达式的scanf一起使用?

如何在C中创建用户定义的struct数组

我希望用户在程序启动时定义数组的大小,我目前有: #define SIZE 10 typedef struct node{ int data; struct node *next; } node; struct ko { struct node *first; struct node *last; } ; struct ko array[SIZE]; 但是,我想删除#define SIZE ,让SIZE成为用户定义的值,所以在main函数中我有: int SIZE; printf(“enter array size”); scanf(“%d”, &SIZE); 我怎样才能获得数组的值? 编辑:现在我在.h文件中有以下内容: typedef struct node{ int data; struct node *next; } node; struct ko { struct node […]

在C编程中将元素插入动态char数组

在C编程中尝试将元素添加到动态char数组时遇到了一些问题。 这是预期的输出: How many characters do you want to input: 5 Input the string:datas The string is: datas Do you want to 1-insert or 2-remove or 3-quit?: 1 What is the character you want to insert: a Resulting string: adata 我已经在main函数中执行了那些用户输入部分,这里是main中的代码,我在其中输入字符串输入,大小并将它们传递给insert(): printf(“How many characters do you want to input: “); scanf(“%d”, &n); str = malloc(n […]

我有一个分段错误,但我没有找到oO?

它应该与合并排序。 合并和排序合并有两个function。 一些未知的函数(从文件和打印数组中读取数组)在输入文件中完全起作用。 Valgrind向我展示了失败是在array2的分配以及它在void合并的第3个while循环中的读写时。 void merge(int* array, int start, int middle, int end) { int size = end – start + 1; int *array2 = malloc(size*sizeof(array2)); int k = start; int m = middle + 1; int i = 0; int j = 0; while ( k <= middle && m <= end ) { […]

一些读取.csv文件的代码崩溃了

我尝试创建一个代码来读取csv文件并通过获取行和列来更改一个值。 在第一次我读取文件以检查那里有多少行和cols,而不是我创建一个动态2D数组 – 每行是文件中的行。 实际上使文件在2D数组中。 而且我将更改所选行和col的值,并将整个数组写回文件。 有人知道它为什么会坠毁吗? 它在第一行崩溃了 – bigArr [i] [j] =(char)的CH; function: int changeValue(int line, int col, char str[],const char* path) { FILE* csvFile = fopen(path, “r”); char arr[VERY_BIG_MEMORY]; int l = 0, c = 1; int i = 0,j=0; int ch = 0; if (!csvFile) { printf(“Cant read the file\nPlease open a […]

我使用malloc()将多维变量数组传递给C中的函数时遇到问题

这段代码应该在main中创建一个数组然后打印它,但每次运行它时我只得到一个全0的数组 #include #include void print(float **A, int w, int h){ int i, j; A = (float*) malloc(w*h*sizeof(float)); for (i=0;i<h;i++){ A[i] = (float*) malloc(w*sizeof(float)); } for (i=0; i<h; i++){ for(j=0; j<w; j++){ printf("%f ", A[i][j]); } printf("\n"); } } int main(void) { int i; int x_dimension=3; int y_dimension=2; float arr [3][2]={}; arr[0][0]=16.2; print(arr,x_dimension,y_dimension); return 0; }

动态分配2d数组

我正在尝试创建一个二维数组,特别是有向图的邻接矩阵。 我以前从未尝试过这种动态内存分配,而且我遇到了麻烦。 这是代码: int n, i; printf(“Number of nodes is: “); scanf(“%d”, &n); int ** array = malloc(n * sizeof(int*)); for(i = 0; i < n; i++) array[i] = malloc(n * sizeof(int)); printf("Number of edges is: "); scanf("%d", &m); int x, y; for(i=0;i<m;i++) { scanf("%d %d", &x, &y); array[x][y]=1; } 一旦我完成所有边缘,程序停止工作并抛出通常的“exe已经停止工作”。 问题出在哪里? 编辑:houssam发现了我的错误。 第一个“for”应该从1变为n。 当我输入1 […]

动态分配2D数组而不使用任何循环?

我们可以动态分配2D数组而不使用任何for循环或while循环吗? 我在c c ++中有任何直接命令或function吗?