Tag: 指针

被称为对象而不是函数c

早上好 – 这些都是我的面条烹饪了一点(同样,谷歌关于我注意到的c编程语言很难…) void prepareFrames(PDouble prep){ int gap = prep->gap; printf(“Gap is %d\n”, gap); prep->intFrames = (PFrame*)malloc(gap*sizeof(PFrame)); PFrame copyFrame = prep->first; int i; for(i=0; i intFrames[i] = (PFrame)malloc(sizeof(Frame)); copyFrame(prep->intFrames[i], prep->first, i); //LINE 189 } } void copyFrame(PFrame new, PFrame copy, int num){ new->sequence = copy->sequence; new->evaluatedFrame = copy->evaluatedFrame + num; new->numBoxes = copy->numBoxes; new->boxes = […]

使用指针从csv文件复制一行

我正在编写一个程序,该程序应该在CSV文件中搜索名称,并复制记录(与名称在同一行上的所有信息)。 例如,如果CSV文件包含: Bob, 13, 12345612 Eli, 12, 21398743 我会输入”Bob”来获取第一行,并将其复制到一个名为”record”的数组中。 到目前为止,我的代码如下: #include #include void FindRecord(char *a, char *b, char c[]); void main(void){ char arrayName[100]; char arrayNewname[100]; char *name = arrayName; char *newname = arrayNewname; char record[1000]; printf(“Please input a name in the phonebook: “); scanf(“%s”, name); printf(“Please input a replacement name: “); scanf(“%s”, newname); FindRecord(“phonebook.csv”,name,record); } […]

你能帮我理解一下指针吗?

我知道以前曾经问过这个问题,但其他一些问题没有触及的原因是为什么 请允许我解释一下。 我刚刚完成了一个输出整数和指针的教程,向您展示如何做到这一点。 int anInteger = 50; int *anIntPointer = &anInteger; 因此,要设置指针,我会正常分配变量值,然后将该变量赋值给指针。 我理解这一点,但正如我已经说过的,这就是原因 。 如果我想返回值50我可以只使用NSLog anInteger那么为什么我需要一个指针。 为什么我需要NSLog *anIntPointer如果我可以只使用NSLog anInteger完全相同的东西? 好吧,我知道这是非常微不足道的,并且可能有完美的情况使用指针,但到目前为止,没有我读过或看过的教程会给我一个完美的情况。 他们都处理如何处理 请帮我找到原因 。

为嵌套结构指针分配内存

我正在使用C代码生成器创建具有以下结构的头文件: typdef struct Place { struct Forest { int trees; } *Forest; } Place ; 并在c ++项目中使用它们。 当我尝试访问Place.Forest-> trees时,我得到一个段错误,因为Place.Forest是一个悬空指针。 我无法正确malloc它因为Place.Forest = malloc(sizeof(Place.Forest)); 只会返回指针的大小。 我不能使用Place.Forest=malloc(sizeof(struct Forest)); 因为我从C ++访问Place并且作用域使我无法看到Forest。 如何在不更改Place或un-nesting Forest的情况下为Forest分配内存? 由于自动生成大量代码,因此修改结构是不切实际的。

没有链接列表的内存分配

说我有这个结构: struct Book { int book_id; struct Book *book_ptr; }; /* navigation pointers */ struct Book *front; struct Book *rear; 我想在我的记忆中添加一Book ,所以我有这个function: void add_book() { struct Book *temp; temp = (struct Book*)malloc(sizeof(struct Book)); temp->book_id = t; t++; temp->book_ptr = NULL; if (rear == NULL) { front = temp; rear = temp; } else { rear->book_ptr […]

swig-php包装器使用指针,c代码是一个数组

我正在使用SWIG生成一个调用’c’共享库的PHP扩展。 除了下面的情况,我能够让大多数事情发挥作用…… 在我的’c’代码中,我声明了一个函数(请注意结构和函数名称已被更改以保护无辜者): int getAllThePortInfo(EthernetPort *ports); 在这种情况下,参数端口实际上是EthernetPort结构的数组。 在另一个’c’程序中,我可以这样称呼它…… EthernetPort ports[4]; int rval = getAllThePortInfo(ports); 这很好用。 然后我运行SWIG,生成我的共享库,并且所有构建都很好。 我得到的PHP代码我可以调用… $ports = new_ethernetport(); $rval = getAllThePortInfo($ports); 这会导致PHP抛出以下错误: php:free():无效指针:0x099cb610 所以,我试着做一些像…… $ports = array(new_ethernetport(), new_ethernetport(), new_ethernetport(), new_ethernetport()); $rval = getAllThePortInfo($ports); 但后来PHP抱怨… PHP致命错误:在getAllThePortInfo的参数1中输入错误。 预计SWIGTYPE_p_EthernetPort 我认为正在发生的是PHP(和SWIG)没有区分指针和数组,而在包装器中,它正在考虑“指向单个结构的指针”,实际上,它是一个结构数组。 PHP中有什么我可以做的吗? 分配一块内存,我可以用作存储多个结构的空间? SWIG我能做些什么来让我的包装更好地理解我的意图吗? 我真的很感激任何建议。 谢谢。

2个暗淡的数组和双指针

可能重复: 创建指向二维数组的指针 当我调用函数func4()和func5()时,我得到followinfg错误: func4()错误:无法将参数’1’的’short int(*)[3]’转换为’short int **’为’int func4(short int **)’| func5()错误:无法将参数’1’的’short int(*)[3]’转换为’short int **’为’int func5(short int **)’| 如何更正调用函数func4()和func5()时的错误? 这是我的代码: #include int func1(short mat[][3]); int func2(short (*mat)[3]); int func3(short *mat); int func4(short **mat); int func5(short *mat[3]); int main() { short mat[3][3],i,j; for(i = 0 ; i < 3 ; i++) for(j = 0 ; j < […]

在这种情况下,了解悬空指针行为

我有一个指针,默认情况下它携带NULL然后它等待一些事件并在事件发生时获取一个值,稍后我释放指针在其他地方,但即使在释放指针后我没有使它为NULL所以它仍然保持引用相同的内存位置 ,我知道下一个malloc调用可能会将该内存块分配给其他内存请求! pointer_type *p = NULL; while (process_get_wakeup(//some logic//)) { while ((qelem = (void*)process_dequeue(//some logic//)) != NULL) { p = (pointer_type *)qelem; } . . //goes into a loop of calls where free(p) is also done! . . //Printing value of p as %p gives this : 0xFF00000000 编辑: 我已经知道它不是我们应该如何做到的 ,我不能指望保留与现在可能用于其他东西的值相同的值,但我想知道的是为什么只有p的特定值我看到了! 这个值: 0xFF00000000是否有任何特殊含义?

将指针传递给函数值不改变

#include void foo(int*); int main() { int i = 97, *p = &i; foo(p); printf(“%d “, *p); getch(); } void foo(int *p) { int j = 2; p = &j; printf(“%d “, *p); } 输出是2 97为什么不是2 2? 指针p现在保存j的地址,为什么要打印97?

无法递增解除引用指针的值

我似乎有一个简单程序的问题,该程序应该计算文件中的各种字符类型。 即使文件根本不为空,它也始终打印零。 我认为它与指针有关,可能是错的。 我还想知道在这种情况下是否需要初始化变量? // fun.h void count_char(FILE *f, unsigned *newl, unsigned *let, unsigned *num, unsigned *spec_char); // main.c #include #include “fun.h” int main() { unsigned newline = 0, number = 0, letter = 0, special_character = 0; char path[256]; FILE *f_read; printf(“Insert a file path: “); gets(path); f_read = fopen(path, “r”); if(f_read == NULL) […]