Tag: 数组

在一个结构中的数组并填充他

目前我有一个研究项目,我和一个朋友真的不知道如何解决问题的一部分。 所以,这是一个C项目。 我有2个结构: struct Lib { char letter; int capacity; int size; char** words[200000]; }; typedef struct Lib Library; struct Program { char* loadedFileName[50]; FILE* f; Library* dictionary; int totalwords; }; typedef struct Program Program; 而这个function: void fillDicoFromFile(Program* startup){ rewind(startup->f); while(!feof(startup->f) && !ferror(startup->f)){ char* word = malloc(sizeof(char) * 30); fscanf(startup->f, “%s”, word); int indexLib = […]

C空字符导致程序行为问题

我遇到的问题是这个程序是菜单驱动的。 当我输入字母时,“i”被输入到input数组中,其大小为MAX_LENGTH+1 。 通过GDB,我发现input数组的第0个索引输入“i”,其余空格输入NULL_CHAR字符。 无论如何,当我按下“i”作为插入菜单时,我向一个字段打招呼,告诉我输入一个值。 我输入任何整数并按inter。 它没有受到“命令?:”字段的欢迎,而是给我一个输入条目的机会,它立即告诉我输入无效并告诉我再次输入命令。 这就是我的意思: Commands are I (insert), D (delete), S (search by name), V (search by value), P (print), Q (quit). Command?: i 45 Command?: Invalid command. Commands are I (insert), D (delete), S (search by name), V (search by value), P (print), Q (quit). Command?: 我发现发生这种情况的原因是因为当再次调用safegets函数时,由于某种原因,函数safegets中的局部变量c的值为NULL_CHAR ,可能是因为输入char数组中的所有其他值将所有其他条目作为NULL_CHAR 。 我不明白为什么c会自动分配NULL_CHAR的值,因为在while循环中,因为有一个语句c […]

将char *前缀添加到C中的现有char *的最佳方法

是)我有的: char * a = “world”; char * b = “Hello”; 我需要的是: char * a = “Hello World”; 我需要先添加b。 这样做有什么function吗?

如何在c中从引用中为二维数组赋值?

int max(int x,int y){ if(x>=y) return x; else return y; } int calci(int i,int j,int n,int *c){ int k,y,m; k=j; y = (n)/((2^i)*(2^j)); for(y=i;y>=0;y–){ for(k=j;k>=0;k–){ *(*(c+2)+k) = max(y,calci(i+1,k,n,c)+calci(i+2,k,n,c)+calci(i,k+1,n,c)); } for(y=0;y<=j+2;y++){ *(*(c+1)+y)=*(*(c+2)+y); *(*(c+0)+y)=*(*(c+1)+y); } } return *(*(c+2)+0); } int main(){ int i,j,n,z,x,y; printf("Enter the amount\n"); scanf("%d",&n); x=log(n)/log(2); y=log(n)/log(3); int arr[3][y+3]; for(i=0;i<=2;i++) for(j=0;j<=y;j++) arr[i][j+2]=0; z = calci(x,y,n,arr); […]

调用函数时char数组和char *数组之间的区别?

我想知道为什么这个代码与char tab[100]工作正常,但如果我使用char *tab不起作用? fgets函数将char*数组作为参数对吗? #include #include #include Int Palindrome(char* str, int i, int j); int main() { char tab[100]; printf(“Enter your string : \n”); fgets(tab, 100, stdin); int j = strlen(tab); printf(“%d\n”, Palindrome(tab, 0, j – 2)); return 0; } int Palindrome(char* str, int i, int j) { if (i >= j) { printf(“My word […]

以下C代码有什么问题?

可能重复: 对C宏扩展和整数运算感到困惑 一个谜语(在C中) 以下C程序的预期输出是打印数组中的元素。 但是当实际运行时,它不会这样做。 #include #define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0])) int array[] = {23,34,12,17,204,99,16}; int main() { int d; for(d=-1;d <= (TOTAL_ELEMENTS-2);d++) printf("%d\n",array[d+1]); return 0; }

指向本地指针数组的全局指针

我在一个函数中有一个指向数组的指针数组。 我需要在另一个函数中访问该数组。 指针数组 char *ptr[size + 1]; 我做了一个全局指针 char *p; 我指向ptr(与ptrfunction相同) p = *ptr; 然后我试图从其他function访问ptr void otherFunction(void){ for(int n = 0; n < 6; n++) { char* str = &p[n]; printf("%i %s\n", n, str); } } 什么时候 ptr[0] = “abcd” ptr[1] = “bcde” ptr[2] = “cdef” ptr[3] = “defg” ptr[4] = “efgh” ptr[5] = “fghi” […]

chararray的大小和长度不一样

我已经有了几年的Python,C#和Java经验,我刚开始学习C语言。 我在教程中学到了char anything[]总是一个指针。 (今天有人告诉我这是错的) – 我认为我的问题与此有关。 不过,我正在尝试获取char数组的长度: #include int get_string_length(char * string) { int length = 0; while(string[length] != ‘\0’) { char c = string[length]; length++; } return length; } int get_string_size(char * string) { return sizeof(string); } int main() { printf(“%d\n”, get_string_size(“hello world”)); // returns 8 printf(“%d\n”, get_string_length(“hello world”)); // returns 11 printf(“%d\n”, sizeof(“hello […]

在共享内存中保留固定大小的符号

我一直在研究一些需要我存储符号列表和相应计数的东西,它们被存储为字符串,int映射。 问题是输入大小约为32 MB但是当我尝试将其存储在内存中时,大小膨胀到1.4Gb,我经历了一些链接,发现std :: string占用了相当多的内存用于簿记和很可能导致膨胀,所以我进一步看了一下,发现可以使用boost :: array,其大小等于符号的最大大小,因为boost数组不做任何书籍保持它没有导致记忆臃肿。 但是,我希望将这个boost数组放在共享内存中,就像map<boost::array ,int>。我看了一下boost :: array文档,发现它不支持allocators ,目前我使用boost的分配器和段管理器如下: typedef bip::allocator CharAllocator; typedef bip::basic_string<char, std::char_traits, CharAllocator> SharedString; 我也遇到了这个链接,这似乎解释了很多: http : //jovislab.com/blog/?p = 89 有没有办法做到这一点,我有什么其他选择。 我不是在寻找完整的解决方案,而是提示。 谢谢,Deb!

为什么程序打印0?

我已经查看了代码并重新编写了几次,每次打印数组和均值时都得到0。 我正在使用代码块作为ide。 以下是statlib.c // Calculates the mean of the array double calculateMean(int totnum, double data[ ]) { double sum = 0.0; double average = 0.0; int i; // adds elements in the array one by one for(i = 0; i < totnum; i++ ) sum += data[i]; average = (sum/totnum); return average; }// end function […]