小编Jon*_*hon的帖子

如果c中的短语法

我知道如果可以用c语言写短语请告诉我如何

   if arraeck(a, n) ?    printf("YES")    printf("NO");
Run Code Online (Sandbox Code Playgroud)

这样的一些事情?...在​​一行......什么是正确的语法?

c ternary-operator conditional-operator

7
推荐指数
3
解决办法
5万
查看次数

具有初始值的类构造

我是C++的新手,以及课程的全部理念 - 我还在读一本书来尝试和学习.我正在阅读的这本书说,当我构建一个类时,我可以通过这样做来指定默认值:

class foo {
public:
   foo(char c, int i);
private:
   char exampleChar;
   int exampleInt;
};

foo::foo(char c, int i):
exampleChar(c),
exampleInt(i)
{}
Run Code Online (Sandbox Code Playgroud)

这段代码(对我来说)看起来非常混乱,并且不遵循我在其他语言中习惯的规则.我的问题是,做上述内容与此之间的区别是什么(下面,我个人认为看起来更清洁)?

foo::foo(char c, int i) {
   exampleChar = c;
   exampleInt = i;
}
Run Code Online (Sandbox Code Playgroud)

我正在考虑的一些事情是:如果大规模地进行性能/效率问题 - 还是完全一样?

c++

6
推荐指数
3
解决办法
5107
查看次数

用C(-Wall噩梦)正确记录UNIX时间

可能重复:
什么原始数据类型是time_t?

基本上,我想要做的就是产生当前的UNIX时间(从时间(NULL)得到的结果)并将其打印到我打开的文件中.

我尝试过以下代码:

fprintf(f, "%i", time(NULL));
Run Code Online (Sandbox Code Playgroud)

但我得到这些恼人的编译器警告:

src/database-createtbl.c:140: warning: int format, time_t arg (arg 3)
Run Code Online (Sandbox Code Playgroud)

我正在尝试使用-Wall编译 - 这真的不应该是一个问题,但它让我疯了.

c

3
推荐指数
1
解决办法
409
查看次数

查询效率(多选)

我有两个表 - 一个叫customer_records,另一个叫customer_actions.

customer_records 具有以下架构:

CustomerID (auto increment, primary key)
CustomerName
...etc...
Run Code Online (Sandbox Code Playgroud)

customer_actions 具有以下架构:

ActionID (auto increment, primary key)
CustomerID (relates to customer_records)
ActionType
ActionTime (UNIX time stamp that the entry was made)
Note (TEXT type)
Run Code Online (Sandbox Code Playgroud)

每次用户对客户记录执行操作时,都会输入一个条目customer_actions,并且用户有机会输入一个注释.ActionType可以是少数几个值之一(如'设计更新'或'添加案例信息' - 只能是选项列表中的一个).

我希望能够做的是显示customer_records最后ActionType一个特定值的记录列表.

到目前为止,我已经搜索了网/ SO并想出了这个怪物:

SELECT * FROM (
    SELECT * FROM (
        SELECT * FROM `customer_actions` ORDER BY `EntryID` DESC
    ) list1 GROUP BY `CustomerID`
) list2 WHERE `ActionType`='whatever' LIMIT …
Run Code Online (Sandbox Code Playgroud)

mysql sql greatest-n-per-group

3
推荐指数
1
解决办法
132
查看次数

内部线程自由导致分段错误

对此非常困惑.我刚刚开始学习指针,现在决定更深入一些并开始尝试线程.我要做的是将指针传递给一个线程,所以(在mallocing 之后data)我调用:

pthread_create(&ptThread, &ptAttr, newClient, (void *) data);
Run Code Online (Sandbox Code Playgroud)

newClient函数内部,我做我需要做的事情,并且我决定(因为已经分配了一个指针)来释放它:

void *newClient(void *v) {
    // ...stuff happens here...

    free(v);
}
Run Code Online (Sandbox Code Playgroud)

在该free(v);部分,我得到一个分段错误.所以我使用了valgrind,我得到这个说这是一个无效的免费:

==1214== Invalid free() / delete / delete[]
==1214==    at 0x4023B6A: free (vg_replace_malloc.c:366)
==1214==    by 0x804F622: newClient (xxxxxx2.c:44)
==1214==    by 0x4032954: start_thread (pthread_create.c:300)
==1214==    by 0x4112E7D: clone (clone.S:130)
==1214==  Address 0x4 is not stack'd, malloc'd or (recently) free'd
Run Code Online (Sandbox Code Playgroud)

为了增加火力,当我退出我的程序时,valgrind告诉我,我有一个内存泄漏,因为v不是free'd:

==1214== 8 bytes in 2 blocks are definitely lost in …
Run Code Online (Sandbox Code Playgroud)

c pointers pthreads

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

调用sprintf的编译错误:"expected'char*',但参数类型为'char'"

我正在尝试用C编写一个微芯片.现在我正在努力更新LCD屏幕上的一条线,但它无法正常工作.任何人都可以对此有所了解吗?

float slope = 0.0626;
char *mystring;
int8_t    LCDline1[20];

void myfunction(){
    sprintf(*mystring, "%ld", (long)(slope * 10000));
    memcpy(LCDline1, *mystring, strlen(*mystring)+1);
}
Run Code Online (Sandbox Code Playgroud)

当我运行编译代码时,我得到以下三个错误.

calibrate.c:60:5:警告:传递'sprintf'的参数1使得整数指针没有强制转换.注意:预期'char*'但参数类型为'char'

calibrate.c:61:5:警告:传递'strlen'的参数1使得整数指针没有强制转换.注意:预期'const char*'但参数类型为'char'

calibrate.c:61:5:警告:传递'memcpy'的参数2使得整数指针没有强制转换.注意:预期'const void*'但参数类型为'char'

我不确定我做错了什么,我使用以下定义作为我的起点

void*memcpy(void*str1,const void*str2,size_t n)

size_t strlen(const char*str)

char*p ="String";

c pointers

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