小编efu*_*ddy的帖子

当 char 输入而不是 int 输入时,C 无限循环

我有一个 C 程序,它应该验证用户的输入是否是 1 到 8 之间的整数。如果输入整数,它就可以工作,但是当输入字符时,验证循环将永远重复。你能告诉我我做错了什么吗?

#include <stdio.h>

int main(void)
{
  int i;
  int input;
  domainEntry *myDomains = buildDomainDB();

  printf("You have the choice between the"
         " following top domains: 1-EDU, 2-COM"
         ", 3-ORG, 4-GOV, 5-MIL, 6-CN, 7-COM.CN, 8.CAN\n");
  printf("Which one do you want to pick?");
  scanf(" %d", &input);

  if(input < 1 || input > 9)
  {
    do  
    {   
      printf("Invalid input. Please try again.");
      printf("You have the choice between the"
             " following top domains: 1-EDU, 2-COM"
             ", 3-ORG, 4-GOV, 5-MIL, …
Run Code Online (Sandbox Code Playgroud)

c validation scanf infinite-loop

4
推荐指数
1
解决办法
1万
查看次数

如何将int作为"void*"传递给线程启动函数?

我最初为我的斐波纳契变量数组有一个全局变量,但发现这是不允许的.我需要进行基本的多线程处理并处理竞争条件,但我无法在pthread create中将int作为void参数提供.我试过使用一个没有运气的常量指针.由于一些奇怪的原因,void*超过了第一个布尔测试但不是else,如果:

  $ gcc -o fibonacci fibonacci.c
    fibonacci.c:22:16: warning: comparison between pointer and integer ('void *' and 'int')
      else if (arg == 1)
               ~~~ ^  ~
    1 warning generated.
Run Code Online (Sandbox Code Playgroud)

我的代码很乱,我真的很困惑,因为我已经重写了很多次.如果我将我的线程运行函数中的所有args转换为int,我会得到一个分段错误11,这是有道理的.所有通过地址传递i索引并取消引用它的尝试都失败了,因为它是一个void并且不能用作int.你能推荐别的吗?

#include<stdio.h> //for printf
#include<stdlib.h>  //for malloc
#include<pthread.h> //for threading

#define SIZE 25 //number of fibonaccis to be computed
int *fibResults;  //array to store fibonacci results

void *run(void *arg)  //executes and exits each thread
{
  if (arg == 0)
  {
    fibResults[(int)arg] = 0;
    printf("The fibonacci of %d= %d\n", (int)arg, fibResults[(int)arg]);    
    pthread_exit(0); …
Run Code Online (Sandbox Code Playgroud)

c int casting pthreads void

2
推荐指数
1
解决办法
616
查看次数

T SQL每天仅选择第一条记录

我创建了一个视图,其中包含SQL数据库中的特定表,并且可以成功地从日期范围内的所有表中选择所有样本。这是视图设计的一部分:

SELECT 'PLANT FLOW1' AS 'Tag', ts AS 'Timestamp', value AS 'Data'
FROM dbo.UASTP_150000_TL63
UNION
SELECT 'PLANT FLOW2' AS 'Tag', ts AS 'Timestamp', value AS 'Data'
FROM dbo.UASTP_150000_TL10
UNION
SELECT 'INFLUENT FLOW' AS 'Tag', ts AS 'Timestamp', value AS 'Data'
FROM dbo.UASTP_150000_TL1
UNION
SELECT 'EFFLUENT FLOW' AS 'Tag', ts AS 'Timestamp', value AS 'Data'
FROM dbo.UASTP_150000_TL2
UNION
Run Code Online (Sandbox Code Playgroud)

我只需要每天从每个表中选择第一读物。我找到了一些示例代码,并尝试将其合并,但是它不起作用。

错误的T-SQL代码:

SELECT TOP (100) percent [Tag]
      ,[Timestamp]
      ,[Data]
  FROM [enteliwebDB].[dbo].[WIMS_View]
  where Timestamp >= DATEADD(day, -30, getdate())
  and Timestamp <= getdate()
  and where …
Run Code Online (Sandbox Code Playgroud)

sql t-sql sql-server datetime

2
推荐指数
2
解决办法
348
查看次数

标签 统计

c ×2

casting ×1

datetime ×1

infinite-loop ×1

int ×1

pthreads ×1

scanf ×1

sql ×1

sql-server ×1

t-sql ×1

validation ×1

void ×1