作为家庭作业的一部分,我试图从stdin读取一个字符,并根据它行事:
char choice;
while (1) {
printf("please enter [y]es, [n]o or [m]aybe: ");
scanf("%c", choice);
fflush(stdin);
// validate input
if (choice == 'y' || choice == 'n' || choice == 'm') {
break;
} else {
printf("Please enter only \'y\', \'n\' or \'m\'!\n");
}
}
// do something with the input
if (choice == 'y') {
printf("you selected yes!\n");
}
Run Code Online (Sandbox Code Playgroud)
由于某种原因,scanf捕获了char和line-feed之后,因此它继续对输入执行某些操作,然后还打印"请仅输入'y','n'或'm'!" 线.如果我在stdin上输入几个字符,它将为所有字符打印该行,同时也为第一个字符正确执行.所以,例如:
$ ./run
please enter [y]es, [n]o or [m]aybe: y<return>
you selected yes!
Please enter only 'y', 'n' or …Run Code Online (Sandbox Code Playgroud) 我试图在命令提示符下打印%的进度.但它不能正常工作.
我想将进度打印为::状态10%已完成,当20%将完成时,它将显示状态20%已完成在同一位置不在新行中.请你帮助我好吗.
码::
$count++;
$per=($count/$total)*100;
print "\nStatus: $per Completed.\r";
sleep 1;
Run Code Online (Sandbox Code Playgroud) 我从一本高级 Linux 编程书中得到了这段代码。当我尝试在 Linux 64 位环境下执行代码时which_prime,pthread_join()函数调用后变量的值会损坏(更改为 0)。
在此示例中,为什么which_prime在运行 pthread_join 后会损坏的值?
一般来说,即使我们调用其他函数,我们是否可以在 main 中安全地使用传递给 pthread_create 函数的第 4 个参数pthread_join()?
#include <pthread.h>
#include <stdio.h>
/* Compute successive prime numbers (very inefficiently). Return the
Nth prime number, where N is the value pointed to by *ARG. */
void* compute_prime (void* arg)
{
int candidate = 2;
int n = *((int*) arg);
while (1) {
int factor;
int is_prime = 1;
/* Test primality by successive division. …Run Code Online (Sandbox Code Playgroud) 我有以下代码片段来理解指针到特定长度的字符数组的工作,使用以下示例代码.
#include <stdio.h>
int main(){
char sports[5][15] = {
"cricket",
"football",
"hockey",
"basketball"
};
char (*sptr)[15] = sports;
if ( sptr+1 == sptr[1]){
printf("oh no! what is this");
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何sptr+1和sptr[1]可以相等?因为第一个意味着增加地址,它sptr由一个存储,第二个意味着获取存储在其中的地址的值sptr + 1.
边界错误是指程序读取或写入的内存超出分配的内存块的实际尺寸.边界写入错误的示例是将第11个条目写入大小为10的数组中
我有一个必须在my_free()函数实现中检测边界写错误的测试
int main()
{
const char* string1 = "Hello, this is a string! I exist to demonstrate a common error.";
char* copy_of_string1 = (char*) my_malloc(strlen(string1));
// Whoops! Forgot to allocate space for the '\0' that ends the string.
strcpy(copy_of_string1, string1); // == boundary write error
my_free(copy_of_string1);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何捕获用户在分配的块之后直接写入一个或多个零字节的情况?
我的C程序中有以下结构
struct person {
char key[50];
char color[20];
int age;
};
Run Code Online (Sandbox Code Playgroud)
我想制作一个这个结构的深层副本.我有深度复制功能设置但是我对如何深度复制字符串有点困惑.我听说有人使用strcpy和其他人使用strdup.
在我的程序中我想要的是,如果原始人被释放,深度复制的人的密钥和颜色不会受到影响.设置后,键和颜色不能改变.为了我的目的,我应该使用strcpy或strdup功能吗?
我需要执行以下操作
我已经用onmousedown、onmouseenter和很好地实现了这一点,并在我处理最后一步的文档中onmouseleave添加了。onmouseup
问题是,我需要它在移动设备上工作。我已经成功地用,div组合实现了拖动,而且很简单。现在我尝试实现上述行为,但我发现,touchstarttouchmove
touchenter或touchleave事件,并且touchmove显然只有在目标已经发生touchstart事件时才会触发。令人沮丧的是,没有无缝的方法可以做到这一点,我确实知道触摸设备与鼠标不同,但我认为应该对待一些常见的行为没有太大区别,并且只区别对待每个设备的特殊事物有。
如果有办法做到这一点,我真的会很感激,因为我非常厌倦阅读文档和尝试看似解决问题但实际上没有解决的事情。
请注意,这个网格不是静态的,也不是直接使用 HTML 创建的,它是从文件中提取并使用不同的程序生成的,它只是一个<table>.
我正在编写一个程序,我需要在其中迭代从用户给定的 cidr(例如75.24.64.0/24)派生的地址列表。
我查看了一些代码,但这似乎过于复杂。
最后我决定使用一个看起来像这样的结构:
struct ip_iterator {
unsigned int netmask;
int bitcount;
long long num_total;
long long num_left;
int current_ip[4];
};
Run Code Online (Sandbox Code Playgroud)
然后我可以定义ip_iterator_init ip_iterator_next, 和ip_iterator_is_finished函数。但是,我被困在如何从 cidr 获得第一个 IP 的问题上。不久前我学习了网络数学,但自从我获得认证以来,我一直在使用在线计算器。
我的编译器(DSP SHARC)非常挑剔。当我用备注进行构建时,出现此错误:
[cc1123] foo.c:1511 {D} remark: comparison of unsigned type
with signed type
while (taille > 0)
Run Code Online (Sandbox Code Playgroud)
两种解决方案:
编译器是正确的,我应该写
size_t taille;
...
while(taille > (size_t)0)
Run Code Online (Sandbox Code Playgroud)编译器很愚蠢,我应该忽略这一点
可能涉及ISO或MISRA标准的另一种解决方案
我该怎么办?
编辑
其实我最好写这样的例子
while(taille) {...}
Run Code Online (Sandbox Code Playgroud)
但是,这与我最初的问题无关
我知道我们可以使用clock_gettime(CLOCK_MONOTONIC).
我尝试问的问题是,如果我需要从纪元开始的纳秒时间,那将是一个巨大的数字。
例如:
13438461673这样13438461673 * 1000000000我如何将它放入 64 位整数中?
c ×8
linux ×2
cidr ×1
deep-copy ×1
ip-address ×1
javascript ×1
mouseevent ×1
perl ×1
perl-module ×1
pointers ×1
scanf ×1
struct ×1
touch-event ×1