Tag: c

限制用户停止Windows服务

我参与编写Windows服务。 在我开始写作之前,我想知道 在LOGIN USERS方面要考虑哪些要点,特别是如何限制其他用户“停止服务”? 在编写服务时我应该考虑哪些通用点? 一旦用户尝试停止服务,如何显示警告提示? 注意:我正在尝试用C编写服务。 谢谢。

如何通过在c中提升内存地址来存储结果

例1: #include #include void saveData(void* data) // only allow to use void* data { // …After some calculation: int inputData[5] = {1,2,3,4,5}; memcpy((int*)data, inputData, sizeof(inputData)); } int main() { int myNum[256]; saveData((void*)myNum); printf(“\n%d%d%d%d%d\n”,myNum[0],myNum[1],myNum[2],myNum[3],myNum[4]); } 我在main中有一个int数组,并将数组传递给函数saveData, 在函数savaData中,我将inputData [5]保存到myNum [256]; 它工作正常。 例2: 如果我有一个struct数组而不是int数组怎么办: #include #include struct Analysis{ int a; int b[10]; }; void saveData(void* data, size_t size) […]

string和int的串联导致C中的分段错误

我不确定我做错了什么。 我正在尝试将hostname与pid连接以创建id 。 char *generate_id(void) { int ret; char id[1048]; char hostname[1024]; pid_t pid = getpid(); //hostname[1023] = ‘\0’; if ((ret = gethostname(hostname,1024) < 0)) { perror("gethostname"); exit(EXIT_FAILURE); } sprintf(id, "%s%d", pid); printf("hostname is %s\n", hostname); printf("The process id is %d\n", pid); printf("The unique id is %s", id); return id; } 编辑: 阅读一些答案后更新了代码: char *generate_id(void) […]

为什么我的trie泄露数据?

我正在尝试使用trie制作拼写检查器,但是我试图添加到我的trie中的单词似乎没有被放入。泄漏在哪里? 我花了几个小时使用调试器并单步执行代码…… 主function: /** * Implements a spell-checker. */ #include #include #include #include #include “dictionary.h” #undef calculate #undef getrusage // default dictionary #define DICTIONARY “dictionaries/large” // prototype double calculate(const struct rusage *b, const struct rusage *a); int main(int argc, char *argv[]) { // check for correct number of args if (argc != 2 && argc […]

在c-compile期间使用luarocks为lua安装yaml的问题

我有lua,我使用了luarocks。 编辑:在Windows系统上 我从v202-3这里下载了luasocket-2.0.2-3.win32-x86.rock文件,成功安装了luasocket。 它是一个旧版本,但它的工作原理。 但是现在我需要安装yaml所以我可以使用yaml文件。 现在我被迫使用* .src.rock文件或.rockspec文件进行安装。 我试过了: luarocks install yaml ,它给出: D:\user\workspace\_lua>luarocks install yaml Warning: Failed searching manifest: Failed fetching manifest for https://luarocks.org – Failed downloading https://luarocks.org/manifest – C:\Users\user\AppData\Local/LuaRocks/Cache/https___luarocks.org/manifest Warning: Failed searching manifest: Failed fetching manifest for https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/ – Failed downloading https://raw.githubusercontent.com/rocks-moonscript-org/moonrocks-mirror/master/manifest – C:\Users\user\AppData\Local/LuaRocks/Cache/https___raw.githubusercontent.com_rocks-moonscript-org_moonrocks-mirror_master_/manifest Warning: Failed searching manifest: Failed fetching manifest for http://luafr.org/moonrocks/ – […]

实现碰撞检测

我写了一个程序,模拟一个50米的建筑物被抛出的球。 我通过在球撞击地面时反转y方向的速度(y <0),保持水平速度相同,并将两个速度乘以某个最小值,从而使球最终变为休息。 #include #include #include int main() { FILE *fp; FILE *fr; float ax = 0, ay = 0, x = 0, y = 0, vx = 0, vy = 0; float time = 0, deltaTime = .001; float min = -.00000000001; int numBounces = 0; fr = fopen(“input_data.txt”, “rt”); fp = fopen( “output_data.txt”, […]

警告:从不兼容的指针类型分配

#include #include #include int main() { double x,y; printf(“Enter a double: “); scanf(“%lf”, &x); printf(“Enter another double:”); scanf(“%lf”, &y); int *ptr_one; int *ptr_two; ptr_one = &x; ptr_two = &x; int *ptr_three; ptr_three = &y; printf(“The Address of ptr_one: %p \n”, ptr_one); printf(“The Address of ptr_two: %p \n”, ptr_two); printf(“The Address of ptr_three: %p”, ptr_three); return […]

转换为int时浮点数为何在C下面舍入?

在一次采访中,有人问我对以下代码有何看法: #include int main() { float f = 10.7; int a; a = f; printf (“%d\n”, a); } 我回答了: 当您将float更改为没有float转换的int ,编译器将发出警告。 由于您没有使用强制转换,因此int将具有垃圾值。 然后,他们允许我在在线编译器上运行程序。 我不堪重负。 我的假设都是错误的。 编译器没有发出任何警告,并且int的值为10.即使我改变了, float值为10.9或10.3,答案也是一样的。 即使投了一个cast并没有改变结果。 有人能告诉我为什么会发生这种情况,结果会在什么情况下有所不同。 注意:在编译时,面试官告诉我不添加gcc标志。 编辑:现在我已经明白,浮动不会四舍五入,答案将是10.但有人可以解释我为什么这样设计? 转换为int时为什么浮动,在下面四舍五入? 有具体原因吗?

在c 中交换任何类型的两个变量

c中是否有任何逻辑可以交换任何类型的两个变量。 即int,float,字符序列。 我可以想到将每种类型的变量存储为characte序列并将其交换为普通字符串的逻辑,但我不是它的好主意。

如何检查字符串是否在文件中

我是C的新手,我正在试图弄清楚如何在一个包含多个数字和单词的文件中搜索特定字符串及其后面的数字。 我正在使用的输入文件如下所示: TREES 2 BENCHES 5 ROCKS 10 PLANTS 8 我知道如何使函数读取字符串,我知道如何比较两个字符串但我不知道如何将它们放在一起或如何设置数组以读取整个文件。 我一直在使用strcmp ,但我只是不知道如何初始化我想要寻找的这个词。