小编Ant*_*ala的帖子

C整数在C ++代码中使用时会失去其恒定状态

我有一个正在处理的C / C ++混合项目。我有一个全局常量C代码,希望能够从我的C ++脚本和C脚本访问。在这种特殊情况下,我试图使用具有可变尺寸的C ++数组,这些尺寸由C全局常数代码中定义的常数整数组成。但是,当我尝试使用或声明此数组时,出现以下错误:数组维数不是整数常量(尽管我在C代码中将它们定义为整数常量)。

常数

const int x = 5;
Run Code Online (Sandbox Code Playgroud)

常数h

#ifdef __cplusplus
extern "C" {
#endif

extern const int x;

#ifdef __cplusplus
}
#endif
Run Code Online (Sandbox Code Playgroud)

my_cpp.h

#include "constants.h"

Run Code Online (Sandbox Code Playgroud)

my_cpp.cpp

#include "my_cpp.h"

double A[x];
Run Code Online (Sandbox Code Playgroud)

所以在这里,我会得到一个错误,指出x不是整数常量。我哪里做错了?

c++

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

奇怪的printf行为?

[cprg]$ cat test.c
#include  <stdio.h>
#include <stdlib.h>

int main(int argc,char *argv[])
{
        int i=10;
        printf("i=%d\ni++=%d\n++i=%d\n",i,i++,++i);
        return 0;
}
[cprg]$ make
gcc -g -Wall -o test test.c
test.c: In function ‘main’:
test.c:7: warning: operation on ‘i’ may be undefined
test.c:7: warning: operation on ‘i’ may be undefined
[cprg]$ ./test
i=12
i++=11
++i=12
Run Code Online (Sandbox Code Playgroud)

我不知道为什么会发生这件事.请问有谁可以详细解释我这里发生了什么?

c

0
推荐指数
1
解决办法
460
查看次数

声明无效

由于某种原因,我收到一个错误:声明对此行没有影响.

for (j = idx; j < iter; j + increment) {
    printf("from loop idx = %i", (int)idx);
    punc(ctxt, j);
}
Run Code Online (Sandbox Code Playgroud)

c

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

C99 中指向数组开始之前的元素

我有一个整数数组:

int* counters = (int *) calloc(N, sizeof(int));
Run Code Online (Sandbox Code Playgroud)

必须使用基于一个的索引进行索引,例如第一个元素的索引为 1,第二个元素的索引为 2,等等。由于性能非常重要,我决定使用一个技巧:

int* oneIndexedCounters = counters - 1;
Run Code Online (Sandbox Code Playgroud)

这允许我使用基于 1 的索引,而无需从索引中减去 1:

// A[i] - contains one based indexes
for (int i = 0; i < K; i++) {
    oneIndexedCounters[A[i]] += B[i]; // many times in code
    // some other operations on oneIndexedCounters
}
Run Code Online (Sandbox Code Playgroud)

插入:

for (int i = 0; i < K; i++) {
    counters[A[i]-1] += B[i];
    // ...
}
Run Code Online (Sandbox Code Playgroud)

counters数组是由我的函数返回的,因此我无法在数组开头分配虚拟元素。

当您不取消引用该指针时,指向数组之前的一个元素是否有效(例如,当数组位于内存页边界上时)?或者是否有其他解决方案不那么棘手并且具有良好的性能?

c arrays pointers c99

0
推荐指数
1
解决办法
267
查看次数

Python3 exec,为什么返回None?

当这段文字下面的代码,并返回结果 None 为什么?

with open('exx.py', 'rb') as file:
ff = compile(file.read(), 'exx.py', 'exec')
snip_run = exec(ff, locals())
if 'result' in locals():
    print(snip_run, result)
else:
    print(snip_run)
Run Code Online (Sandbox Code Playgroud)

结果:

777777
None
Run Code Online (Sandbox Code Playgroud)

模块代码 exx.py:

print('777777')
Run Code Online (Sandbox Code Playgroud)

python return exec python-3.x

0
推荐指数
1
解决办法
2281
查看次数

在 Python 中使用串口,​​无需安装外部包

我有以下代码:

import serial
import time

ser = serial.Serial('com4',115200,timeout=1)
while 1:
    time.sleep(10)
    print ser.readline()
Run Code Online (Sandbox Code Playgroud)

我不想使用串行模块并允许在我的代码中只使用标准的 python 模块。

任何快速建议我该怎么做?

python pyserial

0
推荐指数
1
解决办法
1618
查看次数

如何将 volatile 变量传递给 c 中的函数?

我想将以下内容传递给 c 中的函数:

#define GPIO_PORTF_DATA_R (*((volatile unsigned long *)0x400253FC))
Run Code Online (Sandbox Code Playgroud)

以下似乎工作得很好:

void ZeroRegister(unsigned long * _REG) 
{ 
    #define vReg (*((volatile unsigned long *)_REG))
    vReg = 0; 
}
Run Code Online (Sandbox Code Playgroud)

有更好的方法来编码吗?或者我的代码有缺陷?除了它生成的编译器警告之外。

c volatile parameter-passing

0
推荐指数
1
解决办法
2360
查看次数

Python:UnicodeDecodeError:'utf8'编解码器无法解码字节0x91

我正在解析CSV如下:

with open(args.csv, 'rU') as csvfile:
        try:
            reader = csv.DictReader(csvfile, dialect=csv.QUOTE_NONE)
            for row in reader:
            ...
Run Code Online (Sandbox Code Playgroud)

args.csv我的文件名称在哪里.我文件中的一行是一个顶部有两个点的e.遇到这个时我的脚本会中断.

我得到以下堆栈跟踪:

File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/__init__.py", line 244, in dumps
    return _default_encoder.encode(obj)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 207, in encode
    chunks = self.iterencode(o, _one_shot=True)
  File "/usr/local/Cellar/python/2.7.11/Frameworks/Python.framework/Versions/2.7/lib/python2.7/json/encoder.py", line 270, in iterencode
    return _iterencode(o, 0)
Run Code Online (Sandbox Code Playgroud)

并出现以下错误:

UnicodeDecodeError: 'utf8' codec can't decode byte 0x91 in position 5: invalid start byte
Run Code Online (Sandbox Code Playgroud)

FWIW,我正在运行Python 2.7并且升级不是一个选项(出于几个原因).

我很失落如何解决这个问题所以非常感谢任何帮助.

谢谢!

python csv character-encoding python-2.x

0
推荐指数
1
解决办法
2612
查看次数

为什么解引用指向string(char数组)的指针会返回整个字符串而不是第一个字符?

由于指向数组的指针指向数组的第一个元素(具有相同的地址),我不明白为什么会发生这种情况:

#include <stdio.h>

int main(void) {    
    char (*t)[] = {"test text"};
    printf("%s\n", *t + 1); // prints "est text"
}
Run Code Online (Sandbox Code Playgroud)

另外,为什么打印下面的代码2呢?

#include <stdio.h>

int main(void) {    
    char (*t)[] = {1, 2, 3, 4, 5};
    printf("%d\n", *t + 1); // prints "2"
}
Run Code Online (Sandbox Code Playgroud)

c arrays string pointers pointer-arithmetic

0
推荐指数
1
解决办法
4414
查看次数

如何通过对相关对象进行过滤来获取具有不同项目的SQL Alchemy对象

我正在尝试对相关对象进行以下过滤的不同项目的SQL Alchemy查询,其等效于以下查询:

SELECT distinct items.item_id, items.item_name
FROM items
INNER JOIN categories as cat on items.category_id = cat.category_id
INNER JOIN stores on cat.store_id = stores.store_id
WHERE store.store_id = 123    
Run Code Online (Sandbox Code Playgroud)

我已经创建了以下包含外键的模型,但是当我在下面运行查询时,它不能正确过滤。

items_query = (db.session.query(Store, Item)
               .filter(Store.store_id == 123)
               ).all()



#SQL Alchemy Models 
class Store(db.Model):
    __tablename__ = 'stores'
    store_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    store_name = db.Column(db.String(100), unique=True, nullable=False)

    def __repr__(self):
        return '<Store>'+str(self.store_name)

class Category(db.Model):
    __tablename__ = 'categories'
    category_id = db.Column(db.Integer, primary_key=True, autoincrement=True)
    category_name = db.Column(db.String(100), unique=True, nullable=False)
    store_id = db.Column(db.Integer, db.ForeignKey('stores.store_id'))
    store = …
Run Code Online (Sandbox Code Playgroud)

python sqlalchemy distinct flask-sqlalchemy

0
推荐指数
1
解决办法
8900
查看次数