使用calloc和数组并返回指针的麻烦

作为参考,这是我的任务的第二部分: int* generateFibonacci(int size); 此函数将输入一个称为size的整数作为输入。 size变量中包含的值将表示要放入数组的Fibonacci序列中的数量。 该函数将使用calloc创建此大小的数组,然后使用Fibonacci序列中的size编号填充数组,从1和1 。 当数组完成时,函数将返回指向它的指针。 当我在第8行中收到错误warning: assignment makes and integer from pointer without a cast时,我遇到了麻烦:在warning: assignment makes and integer from pointer without a cast 。 我得到的另一个错误是在第19行“ warning: return makes pointer from integer without a cast ”。 所以我的问题是,我怎么想设置calloc来使用一个大小来自用户的数组,然后返回一个指向它的指针? #include #include int* generateFibonacci(int size) { int i, array[size]; array[size]=(int*)calloc(size, sizeof(int)); array[0]=0; array[1]=1; for(i […]

Linux C选择:管道回显到输入工作,但从键盘读取不?

我想了解http://beej.us/guide/bgnet/examples/select.c (包含在下面以供参考)。 我这样做: :~$ cat /etc/issue Ubuntu 10.04 LTS \n \l :~$ gcc –version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 :~$ wget http://beej.us/guide/bgnet/examples/select.c :~$ gcc select.c -o select :~$ echo “ff” | ./select A key was pressed! :~$ ./select TYPINGTYTimed out. :~$ cat /etc/issue Ubuntu 10.04 LTS \n \l :~$ gcc –version gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3 :~$ […]

如何调用使用JNI从C返回String的Java方法?

有没有办法调用java方法,它在C中返回一个String ? 对于Integer它的工作原理如下: JNIEXPORT jint JNICALL Java_Client_getAgeC(JNIEnv *env, jobject callingObject, jobject employeeObject) { jclass employeeClass = (*env)->GetObjectClass(env, employeeObject); jmethodID midGetAge = (*env)->GetMethodID(env, employeeClass, “getAge”, “()I”); int age = (*env)->CallIntMethod(env, employeeObject, midGetAge); return age; } 我已经搜索了很长时间但是没有什么能用于String 。 最后,我想得到一个char* 提前致谢!

_Complex long int

TL;博士; 问: _Complex long int中int含义是什么? 为什么合法? 更长的版本 我将一些32位代码移植到64位安全。 在一个地方,我注意到它使用: static __complex__ long int i32X[256]; 转储搜索并替换为int32_t更改“long int”给了我一个编译错误。 complex是一个较旧的GNUism,由标准_Complex取代。这是一个重现问题的简短代码片段: #include #include typedef _Complex long int WhyAmILegal; typedef _Complex int32_t Snafu; typedef int32_t _Complex Snafu2; 在gcc下编译给出: complextest.c:6:26: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘Snafu’ typedef _Complex int32_t Snafu; ^~~~~ complextest.c:8:17: error: two or more data […]

如何从C中的文本文件中读取数字块

我有一个文件numbers.dat包含列格式的大约300个数字(浮点,负数)。 目标是首先使用300个数字填写numbers.dat,然后每次将100个数字提取到另一个文件中,例如n1.dat。 第二个文件n2.dat将包含来自numbers.dat的下一个100个数字,依此类推3个从number.dat获得的文件子集。 我无法理解如何考虑最后读取的第100个数字的位置,以便在preios提取的数字之后读取和获取下一个块的文件。 尝试Gunner提供的解决方案: FILE *fp = fopen(“numbers.dat”,”r”); FILE *outFile1,*outFile2,*outFile3; int index=100; char anum[100]; while( fscanf(fp,”%s”,anum) == 1 ) { if(index==100) { // select proper output file based on index. fprintf(outFile1,”%s”,anum); index++; } if(index >101) { fprintf(outFile2,”%s”,anum); index++; } } 问题是只写入一个数据。 什么应该是正确的过程?

对solaris的ppoll

这段代码在Linux中编译但不在Solaris中编译,因为显然ppoll()是特定于Linux的(我在Solaris中使用GCC得到了一个未定义的符号错误)。 有任何帮助转换吗? 我不认为只使用poll()是一个好主意,但话说回来,我没有编写这段代码。 (我是用C编写命令行shell的;第一次尝试使用ncurses / C ) #include #include #include #include #include #include #include #include /** VT100 command to clear the screen. Use puts(VT100_CLEAR_SCREEN) to clear * the screen. */ #define VT100_CLEAR_SCREEN “\033[2J” /** VT100 command to reset the cursor to the top left hand corner of the * screen. */ #define VT100_CURSOR_TO_ORIGIN “\033[H” struct […]

无法在内联汇编中声明.data

你好,我有一个问题,我似乎无法声明.data。 我用我的代码尝试了各种不同的东西,它似乎总是打破这一部分 asm( “.data\n” “.file: .ascii \”/dev/mem\\000\”\n” ); 我也可以删除.data之后的任何信息,它仍会在’.data’处中断。 这是在带有内联汇编的树莓派2上完成的。如果其余的代码是相关的,请告诉我,但我很确定它不是。 谢谢你的帮助! 编辑:道歉我忘了包含错误消息 “错误:’此处’方法名称’的.size表达式不会计算为常量

作为进程子进程,如何知道哪个文件描述符是父进程

我正在尝试编写一个程序,该程序分叉并等待他的孩子完成,然后孩子在输入上做一些工作然后以与父母一样的方式分叉等等。 现在,我知道分支复制到子文件描述符数组,我应该关闭与父文件关联的那些,但我无法弄清楚哪些是父文件。 我需要给孩子这是父母的pid吗? 我一直试图在一小时的大部分时间里绕过它,我想我有一种心灵障碍,因为我无法得出结论。 TL; DR:作为子进程,我如何知道哪些文件描述符属于我的父进程?

从stdin发送任意字节到fgets

我正在使用fgets(user_input,input_len,stdin)提示用户提供的字符串。 我怎样才能将hex \ x04表示的字节发送给程序?

如何增加数组的限制?

// C Program to find average of numbers given by user #include #include #include void main() { double sum = 0; int ii = 0,c; char buf[256], *token; printf(“Enter the numbers to average on a single line, separated by space and press enter when done\n”); fgets(buf, 255, stdin); token = strtok(buf, ” “); while (token […]