- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
所以我尝试在结构中创建一个简单的二维数组,并读取文件中某些坐标的值。
此时我已经有了所有的内部函数,当我试图在“get_coords”函数上获取坐标时,我遇到了一个段错误。如果我在“int main”上编写所有完全相同的代码而不是使用函数,它就可以工作。
代码:
#include <stdio.h>
#include <stdlib.h>
// structure
typedef struct coordinates{
double **a;
} coord;
// counts the nmbr of points
int count(){
int i;
int a1, a2, a3;
FILE *fp;
fp = fopen("abc.txt", "r+");
while (fscanf(fp, "%d %d %d", &a1, &a2, &a3) != EOF){
i++;
if (feof(fp)){
break;
}
}
fclose(fp);
return(i);
}
// creates new structure with the right size of memory allocated
coord *newVector(size_t s){
coord *v;
int j;
v = malloc(sizeof(coord));
v->a = malloc(sizeof(double*)*3);
for (j=0; j<3; j++){
v->a[j] = malloc(sizeof(double)*s);
}
}
void get_coords(coord *points){
int i=0;
FILE *fp;
fp = fopen("abc.txt", "r+");
while (fscanf(fp, "%le %le %le", &points->a[i][0], &points->a[i][1], &points->a[i][2]) != EOF){
i++;
}
fclose(fp);
}
int main(){
int i = 0, j=0;
coord *points;
i = count();
points = newVector(i);
get_coords(points);
for (i=0; i<3; i++){
printf("%lf %lf %lf\n", points->a[i][0], points->a[i][1], points->a[i][2]);
}
}
abc.txt:
1 2 3
4 5 6
7 8 9
感谢大家的帮助。
杯子,迪伦。
PS:这只是我想要的原型(prototype)。
最佳答案
在 count()
中,您正在递增一个未初始化 变量,调用未定义行为 (UB)。
改变这个:
int count() {
int i;
为此:
int count() {
int i = 0;
newVector()
不返回动态分配的内存。
改变这个:
coord *newVector(size_t s) {
...
}
为此:
coord *newVector(size_t s) {
...
return v;
}
解决这些问题后,您应该会看到以下输出:
1.000000 2.000000 3.000000
4.000000 5.000000 6.000000
7.000000 8.000000 9.000000
不是问题,但我会使用 %lf
作为 fscanf()
中的格式说明符,而不是 %le
。
而且,在count()
中,这个return(i);
和这个return i;
是一样的。括号是多余的。
TODO:释放动态分配的内存(我假设您跳过了提供 MCVE 的那部分)。
专业提示:下次编译时启用警告标志。在这种情况下,他们已经为您找到了问题所在。例如,在 GCC 中,我会得到这个:
gsamaras@myPc:~$ gcc -Wall main.c
main.c: In function ‘main’:
main.c:51:28: warning: unused variable ‘j’ [-Wunused-variable]
int i = 0, j=0;
^
main.c: In function ‘newVector’:
main.c:37:13: warning: control reaches end of non-void function [-Wreturn-type]
}
^
最后一个警告是我回答中的第二点。
关于核心转储试图在结构中创建 newVector(array 2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53851445/
我是 C++ 的新手,我在使用这段代码时遇到了问题: string output_date(int day, int month, int year){ string date; if
所以我这样做了 tar cvzf test.zip FP 为了创建目录 FP 的 zip 但是,它会列出 zip 中的目录 FP/ FP/php/ FP/php/pdf/ FP/php/docs/ F
我正在尝试在 Swift、Xcode 7.3(所以是 Swift 2.2)中创建一个通用类,但我似乎无法让它通过编译器: protocol Struct1Protocol { } struct Str
我的测试用例是这样的: class FooTest extends PHPUnit_Framework_TestCase { /** @covers MyClass::bar */ f
我正在尝试将brew install wine作为使electron-builder工作的一步。但是我所能得到的只是以下响应: ==> Installing dependencies for wine
我这样做: string[,] string1 = {{"one", "0"},{"Two", "5"},{"Three","1"}}; int b = 0; for(int i = 0; i <=
我正在尝试使用 SetWindowsHookEx 键盘 Hook Notepad.exe。 如您所见,工作线程正在将其 ASCII 代码(即 wParam)发送到指定的服务器。 UINT WINAPI
我正在尝试将 ListView 实现到我的 Fragment 中,但无论我尝试什么,我都会得到一个 NullPointerException。我检查对象是否为 null 并记录是否为 null,看起来
我尝试在一行中对齐两个 div。使用 float left 属性,一切顺利。但是当我在 div 中使用图像时,它开始产生问题。 所以这是我的示例代码:- Some headi
我目前正在使用此代码来获取图像的灰度图像表示并以 (512, 370, 1) 的格式表示它大批。 img_instance = cv2.imread(df.iloc[i][x_col]) / 255.
总结 我正在创建一个简单的应用程序,它允许用户选择一个包含顶级窗口的进程。用户首先键入 native DLL(而非托管 DLL)的路径。然后用户键入将在 Hook 过程中调用的方法的名称。该方法不得返
我是一名优秀的程序员,十分优秀!