我有兴趣打印结构字段.
Typedef struct
{
UINT32 thread_id;
BOOL is_valid;
}T_THREAD;
Run Code Online (Sandbox Code Playgroud)
是否有一种方法用"C"语言来打印结构的内容,如
ex:print (T_THREAD)和输出应该是这样的
Contents of a structure T_THREAD are
thread_id
is_valid
Run Code Online (Sandbox Code Playgroud) 假使,假设
int array[16];
Run Code Online (Sandbox Code Playgroud)
有标准转换称为数组到指针转换,因此array会隐式转换为类型int*,但为什么&array等于array?
例如,
int array[16];
void *p = array;
void *q = &array;
printf("%p\n", p);
printf("%p\n", q);
Run Code Online (Sandbox Code Playgroud)
这将给出相同的地址和没有编译错误.
为什么?
我想在控制台应用程序中完成以下事项:
1,程序将执行任务1,如果用户输入q,程序将退出;这是我的代码:
#include <stdio.h>
#include <time.h>
char buff[64];
char command;
while(command != 'q')
{
begin:
printf(">> ");
scanf("%s", buff);
command = buff[0];
switch (command)
{
case '1':
// task 1 code will be added here;
break;
case '2':
// task 2 code will be added here;
break;
case 'q':
printf("quit the loop.\n");
break;
}
// wait for 10 seconds;
Sleep(10000);
// default task code will be added here;
if(command != 'q')
{
goto begin; …Run Code Online (Sandbox Code Playgroud) 我有两个字符串.让我们说'
str1="One Two Three";
Run Code Online (Sandbox Code Playgroud)
和
str2="two";
Run Code Online (Sandbox Code Playgroud)
我想知道是否有任何函数检查第一个字符串中第二个字符串的匹配,并返回指向第一个匹配项的指针,类似于strstr(),但不处理相同的字母,大写或小写,作为两个不同的角色.
对于我的例子,函数应该str2在第一个字符串中找到匹配,尽管是大写"T"的"Two".
我在Linux内核源代码(2.6.32)中遇到了以下代码.
do_wait_for_common(struct completion *x, long timeout, int state)
{
if (!x->done) {
/* some code here */
}
x->done--;
return timeout ?: 1; <--- What it returns?
}
Run Code Online (Sandbox Code Playgroud)
为了理解这种行为,我手动尝试了以下代码
#include <stdio.h>
int f(int x)
{
return x?:1;
}
int main()
{
printf("f %d\n", f(0));
printf("f %d\n", f(1));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
并得到以下输出
f 1
f 1
Run Code Online (Sandbox Code Playgroud)
当我改变它
int f(int x)
{
return x?:2;
}
Run Code Online (Sandbox Code Playgroud)
我正进入(状态
f 2
f 1
Run Code Online (Sandbox Code Playgroud)
我只是想知道标准中是否提到了这种行为(如果没有提到则返回1).
当我尝试包含一个.h文件,其中包含内联函数的定义
__attribute__( ( always_inline ) ) __STATIC_INLINE uint32_t __SLA (int32_t o1, int32_t o2, int32_t o3)
Run Code Online (Sandbox Code Playgroud)
它给出了"警告:always_inline函数可能不是无法解决的[-Wattributes]"你可以帮助我,我正努力解决它.
前段时间没有站在这样的线路之后:
if (arg)
invk(test);
else if (test)
{
alot();
stuff();
}
Run Code Online (Sandbox Code Playgroud)
我决定在1920x1200次中为自己提供更好的可读性,而不是省略{}.
所以我写了一个工具来重新格式化我现有的代码.
后来我注意到该工具中的一个错误
if (x)
{
...
}
else if(y)
{
...
}
else if(z)
{
...
}
Run Code Online (Sandbox Code Playgroud)
已被改变(不明显地改变行为)成:
if (x)
{
...
}
else
{
if(y)
{
...
}
else
{
if(z)
{
...
}
}
}
Run Code Online (Sandbox Code Playgroud)
这让我意识到(无意)这实际上是else if通过C的语法和语义规则做的事情.
那么,有没有甚至像一个陈述else if()现有或只是语义的滥用的结果在这个有用的,但(让我们称之为它,以便用于此目的)混淆起源措辞,打破任何格式化规则,只是作为人类可读?
我曾strncat多次使用过,但现在只需检查标准中的正式定义:
#include <string.h>
char *strncat(char * restrict s1,
const char * restrict s2,
size_t n);
Run Code Online (Sandbox Code Playgroud)
strncat函数从s2指向的数组到s1指向的字符串的末尾附加不超过n个字符(空字符及其后面的字符未附加).s2的初始字符将覆盖s1末尾的空字符.终止空字符始终附加到结果.
通常我会想到s1并s2简单地指向char.但正如所看到的,该标准对它们的要求不同:
s1s2s1和之间的唯一区别s2是const限定符 - 这就是为什么一个被称为数组而另一个被称为字符串?
而且,在脚注中:
因此,可以在s1指向的数组中结束的最大字符数是strlen(s1)+ n + 1.
所以这里他们的引用方式s1不同:指向的数组(不是字符串)s1
有没有的调用这些不同方式的任何含义s1和s2?
#include <stdio.h>
int main(){
unsigned char a[4] = {1, 2, 3, 4};
int b = *(int *)&a[0];
printf("%d\n", b);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我只是不明白,为什么结果b是0x4030201.
有人可以帮帮我吗?
在libuv源代码中,我找到了这段代码:
/* The if statement lets the compiler compile it to a conditional store.
* Avoids dirtying a cache line.
*/
if (loop->stop_flag != 0)
loop->stop_flag = 0;
Run Code Online (Sandbox Code Playgroud)
有人能解释一下吗?
什么是缓存行?
另外,我猜条件存储是一些汇编指令,它检查一些东西,如果成功,写一些值.对?
这种结构什么时候有意义?我想并不总是,因为否则编译器会一直使用条件存储,对吧?