根据C11标准,
表单的预处理指令
#include"q-char-sequence"换行符
导致由"分隔符"之间的指定序列标识的源文件的全部内容替换该指令.
所以,如果我有一个头文件test.h包含:
#endif
Run Code Online (Sandbox Code Playgroud)
一个源文件test.c包含:
#if 1
#include "test.h"
Run Code Online (Sandbox Code Playgroud)
是否应该根据标准通过更换现有内容来通过预处理阶段test.h?
但我不能这样做clang,其中说:
In file included from test.c:2:
./test.h:1:2: error: #endif without #if
#endif
^
test.c:1:2: error: unterminated conditional directive
#if 1
^
2 errors generated.
Run Code Online (Sandbox Code Playgroud)
那么标准指定的行为是什么?
我刚刚开始学习 C,一个问题困扰了我一段时间。如果我写
int i = -1;
unsigned int j = 2;
unsigned int k = -2;
Run Code Online (Sandbox Code Playgroud)
整数文字-1and 2and的类型是什么-2,它如何转换为存储在signed intand 中unsigned int?
有符号整数是什么意思,是变量还是整数文字的属性?比如-2有符号整数和2无符号整数?
我有一个嵌入在另一个VC中的视图控制器.我想从嵌入式VC中的主VC获取变量的值.我能怎么做 ?
我试过"prepareForSegue",但它似乎没有触发嵌入式视图控制器.
我试图在测试项目中隔离问题:
主VC代码:
class MyViewController: UIViewController {
@IBOutlet weak var label1: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
label1.text = "Hello"
}
}
Run Code Online (Sandbox Code Playgroud)
嵌入式VC代码:
class EmbeddedVC: UIViewController {
@IBOutlet weak var label2: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
//label2.text = how to get value of label1 ?
}
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助 :)
我试图理解memcpy()C库中定义的函数<string.h>
句法: void *memcpy(void*dst,const void*src,size_t n);
我知道这个函数用于将指针指向的内存的内容复制到指针指向src的位置,dst并返回指针指向的地址dst.
我无法理解以下重要声明memcpy():
memcpy(),内存地址不应重叠,如果重叠,memcpy()则未定义.另一个查询是:传递给函数的第三个参数的size_t n值是否总是一个整数值?
int array[5];
Run Code Online (Sandbox Code Playgroud)
表达如
array[3] gets converted to *(array+3)
Run Code Online (Sandbox Code Playgroud)
或者在
void fun ( int *array[] );
*array[] gets converted to int **array
Run Code Online (Sandbox Code Playgroud)
我想知道数组声明是什么
int array[5];
Run Code Online (Sandbox Code Playgroud)
转换成?是吗
int *(array+5)
Run Code Online (Sandbox Code Playgroud)
如果是的话,这甚至意味着什么?一个人如何解释和/或阅读它?
我写了这个memcpy函数,但我仍然需要禁用规则11.5和11.8.是否有完整的MISRA:2012兼容解决方案?
#pragma cstat_suppress="MISRAC2012-Rule-21.6" // Uses of stdio.h were found.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
extern int main(void);
static int_least8_t _memcpy(void *dst, const void *src, const size_t length)
{
#pragma cstat_disable="MISRAC2012-Rule-11.5" // A conversion from a pointer to void into a pointer to object was found.
int_least8_t* destination = (int_least8_t*)dst;
#pragma cstat_disable="MISRAC2012-Rule-11.8" // A cast that removes a const or volatile qualification was found.
const int_least8_t* source = (int_least8_t*)src;
#pragma cstat_restore="MISRAC2012-Rule-11.5","MISRAC2012-Rule-11.8"
for (size_t i = 0; …Run Code Online (Sandbox Code Playgroud) Misra 2004有以下规则:
Rule 16.1: Functions shall not be defined with variable numbers of arguments
Run Code Online (Sandbox Code Playgroud)
因此,类似的功能printf不能与规则16.1一起使用.
uint32_t debug_print(char *format, ...)
{
int int_ret_val=0;
uint32_t ret_val = ERR_NO_ERROR;
va_list arguments;
va_start(arguments, format);
ret_val = vprintf(format, arguments);
va_end(arguments);
return ret_val;
}
Run Code Online (Sandbox Code Playgroud)
我寻找替代方案,但没有找到任何.
用于记录字符串格式化消息(" %d,%f,..")的所有c系列命令是否都使用变量列表?
以下是#include来自Google的C++风格指南的部分:
如果您依赖bar.h中的符号,请不要指望您包含foo.h这个(当前)包含bar.h:自己包含bar.h,除非foo.h明确表明其意图为您提供bar.h的符号.
但是,相关头中存在的任何包含不需要再次包含在相关的cc中(即,foo.cc可以依赖于foo.h的包含).
当我读到这篇文章时,这些句子似乎是矛盾的.他们对我说:
foo.cc需要的东西bar.h,它必须包括bar.h.foo.cc需要填充bar.h,foo.cc包含foo.h和foo.h包含bar.h,则foo.cc不需要包含bar.h.为什么这些句子不相矛盾?
我正在编写一些固件,需要使用C代码而不使用数据部分。假设人们远离全局变量,这是非常简单的。还是我想。
我写了一些功能类似于以下代码的内容:
void func()
{
int feature_set[][2] = {
{feature0, 1},
{feature1, 0},
{feature2, 0}
};
//Use 'feature_set' for some hardware init
}
Run Code Online (Sandbox Code Playgroud)
在我的特定用例中,feature_set是指我需要用于初始化的一些配置数据。因为我是在堆栈上创建此数据集,所以我期望在使用之前先在堆栈上构造它。我意识到这会创建更多的指令,但是在这种情况下我可以选择这样做。
但是,看完反汇编后,我意识到它实际上是在做这样的事情:
mov ecx, <size>
lea edi, <stack addr>
lea esi, <somewhere in .data>
rep movs
Run Code Online (Sandbox Code Playgroud)
很显然,编译器试图通过在结构中创建const版本.data并在需要时将其复制到堆栈中来优化此操作。
问题:有没有办法防止这种情况?有没有办法告诉编译器不要将数据段用于此操作?更改优化级别可能会起作用,但是我确实希望进行优化……只是不专门针对这种构造。
我有一个程序应该在 MCU(例如 STM32)上定期运行。例如,它应该每 1 毫秒运行一次。如果我编写一个 1ms ISR 并在其中调用我的完整程序,假设它不会超过 1ms,这是一个好方法吗?我可能会遇到什么问题吗?会很精确吗?
c ×7
c++ ×2
embedded ×2
misra ×2
arrays ×1
c11 ×1
constants ×1
gcc ×1
header-files ×1
isr ×1
memcpy ×1
optimization ×1
overlap ×1
printf ×1
qualifiers ×1
signed ×1
stm32 ×1
storyboard ×1
swift ×1