Tag: c

C Winsock编程:输入垃圾

我正在编写一个非常简单的套接字服务器,遵循此示例代码 。 服务器启动并运行,我可以通过telnet连接到它(实际上使用Putty)。 每当收到新连接时,我会生成一个新线程,代码如下: DWORD WINAPI receive_cmds(LPVOID lpParam) { char outbuffer[100]; outbuffer[0] = ‘\0’; char inbuffer[100]; inbuffer[0] = ‘\0’; int res; SOCKET current_client = (SOCKET)lpParam; while (1) { res = recv(current_client, inbuffer, sizeof inbuffer – 1, 0); if (res > 0) { inbuffer[res] = ‘\0’; printf(“Got ‘%s’\n”, inbuffer); } Sleep(10); if (res == 0) { […]

使用libgit2的“git log -1 fullpath / myfile”

我想用libgit2实现git log -1 fullpath/myfile 。 我是libgit2的新手。 我是在正确的轨道上吗? 这是我到目前为止: git_repository_head(&refToHead, repo); headOID = git_reference_oid(refToHead); git_commit_lookup(&headCommit, repo, headOID); headTreeOID = git_commit_tree_oid(headCommit); git_tree_lookup(&tree, repo, headTreeOID); git_tree_entry_byname(tree, “repopath/myfile”); 不幸的是, git_tree_entry_byname似乎不适用于repo的子目录中的文件。 任何的想法? 谢谢你,拉尔斯

C指针与Objective-C指针

我来自Objective-C背景,我正在尝试扩展我在C中的知识。然而,有一件事使我困惑,这就是C和Obj-C中指针之间的区别。 正如您在下面的示例中所看到的,两种语言之间的行为似乎有点不同,我想知道您是否可以帮助解释原因? C代码工作正常: void myFunction() { int x, *pointerX; pointerX = &x; *pointerX = 5; // Prints: “x is 5” printf(“x is: %i”, x); } Obj-C代码失败: – (void)myMethod { NSString *string = @”Caramel coffee”, *stringPointer; stringPointer = &string; // Warning: Assignemnt from incompatible pointer type *stringPointer = @”Chocolate milkshake”; // Exception: Incompatible types in assignment […]

getchar()每隔一次只调用一次

使用Ch标准解释器时, getchar()仅运行每隔一行。 C:/> char a = getchar(); C:/> char b = getchar(); b C:/> char c = getchar(); C:/> char d = getchar(); d 我在使用scanf(“%c”, &a)时遇到了同样的问题; 在Vim中,语句被跳过。 printf(“\nType of Something\nA for SomethingA\nB for SomethingB ” “\nC for SomethingC\n\nSelect (A,B,C) > “); char letter = getchar(); // This statement gets skipped return 0;

如何在MinGW中获取用户名?

要使用MinGW从Windows获取用户名,我应该使用unistd.h中的getlogin()函数还是Windows函数GetUserName? 谢谢。

例2.1,K&R:LONG_MIN和LONG_MAX的符号常量值是否错误?

这是我用来查找LONG的符号常量值的代码 #include //These header files contains the symbolic constants #include //for different datatypes #include int main(void){ printf(“\tMininum numeric value for long type: %ld\n”, LONG_MIN); printf(“\tMaximum numeric value for long type: %ld\n”, LONG_MAX); printf(“\tMaximum numeric value for unsigned long type: %lu\n”, (unsigned)ULONG_MAX); return 0; } 我得到的输出是: Mininum numeric value for long type: -9223372036854775808 Maximum numeric value […]

推Lua表

我在C中创建了一个Lua表,但我不确定如何将该表推到堆栈的顶部,这样我就可以将它传递给Lua函数了。 有谁知道如何做到这一点? 这是我目前的代码: lua_createtable(state, libraries.size(), 0); int table_index = lua_gettop(state); for (int i = 0; i < libraries.size(); i++) { lua_pushstring(state, libraries[i].c_str()); lua_rawseti(state, table_index, i + 1); } lua_settable(state, -3); [ Push other things ] [ Call function ]

使用C中的递归函数测试回文结构

我尝试编写用于测试字符串的程序,如果它是一个回文或不是,但我总是得到输出,因为它不是一个。 我的代码出了什么问题? #include #include int is_palindrome(int start, int end, char *str) { if (str[start] != str[end]) return 0; else if (start == end) return 1; else return is_palindrome(++start, –end, str); return 0; } int main() { char str[20]; int length,start=0,end=length-1; int result; printf(“Enter the String.\n”); fgets( str, sizeof( str ), stdin ); length = strlen(str); […]

用于生成下一位以在灰色代码中翻转的C代码

我需要一个函数返回一个数字,基本上告诉我在移动到格雷码的第n个元素时哪个位是要翻转的。 如果它是标准(reflection)格雷码或其他一些最小位切换方法并不重要。 我可以做到,但似乎不必要的笨拙。 目前我有这个: #include int main() { int i; for (i=1; i>1); n2 = n^(n>>1); d = n1^n2; j = 0; while (d >>= 1) j++; return j; } main()中的循环仅用于演示函数的输出。 有没有更好的办法? 编辑:只看输出,显然可以更简单地做到这一点。 我添加了第二个函数gray2,它可以更简单地完成相同的操作。 这会是这样做的吗? 这不是生产代码,而是业余爱好者。 #include int main() { int i; for (i=1; i>1); n2 = n^(n>>1); d = n1^n2; j = 0; while […]

执行while循环,选择C作为char

在下面给出的代码中,如果我按下’y’一次它会重新开始,但是它不会要求下一个重复(或按’y’)。有人可以帮助为什么这个代码在一个循环后被终止? main() { char choice; do { printf(“Press y to continue the loop : “); scanf(“%c”,&choice); }while(choice==’y’); }