我有一个新手的问题.我正在阅读"Erlang编程"一书,并在每一章之后进行练习.
在我有一个db.erl
模块的情况下,在其中我已经为几个练习放置了几个函数.经过几天的练习后,我已经厌倦了在erl
shell中重复"compile-> execute exercise-function"动作.假设我正在研究模块中的new()
功能db.erl
; 每次修改这个函数后,我都被迫做下一个erl
:
c(db.erl).
db:new().
Run Code Online (Sandbox Code Playgroud)
一次又一次.有时我忘记重新加载我的模块,结果令人困惑.我可以使用:编译/加载OS shell
erl -compile file.erl; erl -make
,但是我找不到从特定模块运行特定功能的方法.有什么建议?
我试图哈希一个unsigned long
值,但哈希函数需要一个unsigned char *
,如下面的实现中所示:
unsigned long djb2(unsigned char *key, int n)
{
unsigned long hash = 5381;
int i = 0;
while (i < n-8) {
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash = hash * 33 + key[i++];
hash …
Run Code Online (Sandbox Code Playgroud) 这是一个现有代码,它找到与父路径相对应的相对路径.它已经在平台上正常工作,除了SUSE 12和GCC 4.7. 我已经提到了一个错误的输出,并在评论中预期. 我想知道为什么会这样? 这段代码有什么问题? 子字符串和父字符串都以NUL字符结尾.我看到的唯一另一件事是源和目标从同一个内存位置传递,换句话说我们正在尝试更新相同内存位置的值.这是真正的问题吗?
//child = /dev/shm/4/tmp/backup/datadir/performance_schema/events_stages_summary_by_account_by_event_name.frm
//parent = /dev/shm/4/tmp/backup
char* get_relative_path(char *child, const char *parent)
{
char* start= child;
static char dot[] = "." ;
....
....
/* Check if child = parent + "/" + .... */
if (strstr(child, parent) == child)
{
int parent_len= strlen(parent);
/* parent path may or may not have the "/" suffix. check for both. */
if (parent[parent_len-1] == FN_LIBCHAR ||
child[parent_len] == FN_LIBCHAR)
{
child+= parent_len;
while (*child && …
Run Code Online (Sandbox Code Playgroud) c_val
如果每个单元存储为设置位,则该程序应该确定变量值中存储了多少单元.我的问题是:为什么作者写这个陈述if (c % 2 == 1) count++;
后转向c
右边c = c >> 1;
?
#include <stdio.h>
#include <cstdlib>
int main(){
unsigned char c_val;
printf("char value = ");
scanf("%c", &c_val);
int count = 0;
unsigned char c = c_val;
while(c){
if (c % 2 == 1) count++;
c = c >> 1;
}
printf("%d bits are set", count);
system("pause");
}
Run Code Online (Sandbox Code Playgroud) 我试图在我的程序中创建一个链表,我无法使用malloc()为结构指针分配内存.如何在GCC中为变量分配内存?示例程序如下.如何让它在gcc中运行?
#include<stdio.h>
#include <alloc.h>
struct node
{
int data;
struct node * link;
};
void insert (struct node *p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(p==NULL)
{
p=temp;
}
else{
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(p,7);
insert(p,9);
disp(p);
}
Run Code Online (Sandbox Code Playgroud)
我遇到的错误是:
Line 18: error: alloc.h: No such file or directory
In function 'insert':
Line 13: warning: incompatible …
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
struct mystruct
{
char cc;
float abc;
};
union sample
{
int a;
float b;
char c;
double d;
struct mystruct s1;
};
int main()
{
union sample u1;
int k;
u1.s1.abc=5.5;
u1.s1.cc='a';
printf("\n%c %f\n",u1.s1.cc,u1.s1.abc);
k=sizeof(union sample);
printf("%d\n\n",k);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
运算符的大小正在返回8
我仍然能够访问结构元素,一次不止一个,并且sizeof
运算符仍然返回我假设的原始数据类型的最大大小.为什么会这样?实际分配的大小是8
?并且sizeof
返回错误的值?或者实际分配的大小是8
?那么结构是如何适应的?如果我们使用分配一个联合数组malloc
并sizeof
在这种情况下分配足够的空间吗?请详细说明.
谁能告诉我,为什么data.i
和data.f
被损坏?这段代码来自的网站试图解释它,但使用了错误的语法和许多错别字,所以我想知道这里是否有人可以帮助我.
#include <stdio.h>
#include <string.h>
union Data
{
int i;
float f;
char str[20];
};
int main( )
{
union Data data;
data.i = 10;
data.f = 220.5;
strcpy( data.str, "C Programming");
printf( "data.i : %d\n", data.i);
printf( "data.f : %f\n", data.f);
printf( "data.str : %s\n", data.str);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译并执行上述代码时,会产生以下结果:
data.i:1917853763
data.f:4122360580327794860452759994368.000000
data.str:C编程
我一直在使用Codecademy进入python,现在正在使用LearnStreet进行审阅和练习.这让我退缩了 - 以下代码:
def run():
count = 1
while count <= 10:
print count
count += 1
return count
print run()
Run Code Online (Sandbox Code Playgroud)
返回从1到11的所有数字.我认为它应该返回从1到10的所有数字.为什么循环打印时count == 11
?while表示只有在计数不大于10时执行块.