我在这里要完成的是一个带有链表的字典.有一个节点指针数组.我试图使用malloc初始化每个数组指针.当我删除for循环时,它工作正常.
#include <stdbool.h>
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
#include "dictionary.h"
unsigned int count = 0;
unsigned int collisions = 0;
unsigned long index = 0;
#define HASHTABLE_SIZE 1999099
// Initialize struct for linked list.
typedef struct node{
char word[46];
struct node *next;
} node;
// Initialize an array of node pointers.
node *hashtable[HASHTABLE_SIZE];
for(unsigned long i = 0; i < HASHTABLE_SIZE; i++)
// Error here reads expected "=",";","asm" or __attribute__ before "<"
{
hashtable[i] = (node *)malloc(sizeof(node));
}
Run Code Online (Sandbox Code Playgroud) 嗨,我遇到了这种情况。我正在使用malloc给我一个包含10个指针的数组。当我在gdb中看到测试指针时,其中一个(第三个)指向0x0。有时,使用apple [2]-> string =“ hello”时,代码会出现段错误。为什么malloc这样做?在此先感谢您的帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int
main(void)
{
typedef struct test
{
char *string;
int data;
} test;
test *apple[10]; // Declare an array of 10 test pointers. This line results in one of the pointers having a null value.
apple[0] = malloc(sizeof(test));
apple[0]->string = "hello";
printf("The string is %s\n",apple[0]->string);
printf("Size of apple[0]->data is %d\n",sizeof(apple[0]->data));
printf("Size of tester is %d\n",sizeof(test));
free(apple[0]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我想看看指针数组如何工作。我不打算使用所有10个指针。那我只需要分配我需要的东西吗?巧合的是,第三个指针是0x0?
我对此反应感到困惑.任何人都可以帮我解决这个问题,并指出我犯了错误的地方?键盘输出是" memory clobbered before allocated block
"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *s = (char *)malloc(10 * sizeof(char));
s = "heel";
printf("%s\n",s);
printf("%c\n",s[2]);
printf("%p\n",s);
printf("%d\n",s);
free(s);
return 0;
}
Run Code Online (Sandbox Code Playgroud) 如果这是转发,我深表歉意。我在问题中找不到类似的。
我正在尝试将 emacs 设置为我的 python IDE,其中包含 IPython。基本上遵循这个http://www.jesshamrick.com/2012/09/18/emacs-as-a-python-ide/
我的 emacs shell 中的 python 版本是 2.7.5,而终端中的 python 版本是 2.7.6。然而,在 emacs shell 中输入 ipython 显示 ipython 尚未安装,但我能够将 ipython 安装到终端。
我想知道如何将 ipython 插件安装到 .emacs 文件中。当你说安装时,是否意味着复制文件并将其放在那里?如果是的话,我已经这样做了。然而,当我尝试运行 python 程序时,它显示“正在搜索程序:没有这样的文件或目录,ipython。提前感谢您的帮助。