我有一个映射到Swift的C函数定义为:
func swe_set_eph_path(path: UnsafeMutablePointer<Int8>) -> Void
Run Code Online (Sandbox Code Playgroud)
我试图传递一个路径到该功能,并尝试过:
var path = [Int8](count: 1024, repeatedValue: 0);
for i in 0...NSBundle.mainBundle().bundlePath.lengthOfBytesUsingEncoding(NSUTF16StringEncoding)-1
{
var range = i..<i+1
path[i] = String.toInt(NSBundle.mainBundle().bundlePath[range])
}
println("\(path)")
swe_set_ephe_path(&path)
Run Code Online (Sandbox Code Playgroud)
但在路径[i]行我得到错误:
'subscript'不可用:不能使用Int范围下标String
swe_set_ephe_path(NSBundle.mainBundle().bundlePath)
Run Code Online (Sandbox Code Playgroud)
也不
swe_set_ephe_path(&NSBundle.mainBundle().bundlePath)
Run Code Online (Sandbox Code Playgroud)
也不工作
除了没有工作,我觉得必须有一个更好,更少复杂的方式来做到这一点.使用CString的StackOverflow以前的答案似乎不再起作用了.有什么建议?
我对Objective C不熟悉
我正在使用私有框架,需要能够从我的Swift代码中更改其中一个属性.
该属性在Objective C中以这种方式声明:
@property (nonatomic, assign) BOOL *isSSNField;
Run Code Online (Sandbox Code Playgroud)
在swift中我试图以这种方式改变属性的值:
myClass.isSSNField = true
Run Code Online (Sandbox Code Playgroud)
我收到了这个错误
Cannot assign a value of type 'Bool' to a value of type 'UnsafeMutablePointer<ObjcBool>'
Run Code Online (Sandbox Code Playgroud)
我不知道从哪里开始,或者为什么我得到这个错误
以下是Rust编程语言中的Deref示例,除了我添加了另一个断言.
为什么assert_eq和deref平等'a'?为什么我需要*手动调用一次deref?
use std::ops::Deref;
struct DerefExample<T> {
value: T,
}
impl<T> Deref for DerefExample<T> {
type Target = T;
fn deref(&self) -> &T {
&self.value
}
}
fn main() {
let x = DerefExample { value: 'a' };
assert_eq!('a', *x.deref()); // this is true
// assert_eq!('a', x.deref()); // this is a compile error
assert_eq!('a', *x); // this is also true
println!("ok");
}
Run Code Online (Sandbox Code Playgroud)
如果我取消注释该行,我会收到此错误:
error[E0308]: mismatched …Run Code Online (Sandbox Code Playgroud) 我对Go中结构的方法感到困惑.我一直在他们的教程中跟随:
func (p *Page) save() error {
filename := p.Title + ".txt"
return ioutil.WriteFile(filename, p.Body, 0600)
}
Run Code Online (Sandbox Code Playgroud)
根据我的理解,p是指针,你需要在检索属性之前取消引用指针,例如:
filename := (*p).Title + ".txt"
Run Code Online (Sandbox Code Playgroud)
对我来说唯一有意义的方法是点如果像->C++一样.我错过了什么?
我试图指定一个指向lambda函数的指针指向另一个lambda函数.代码将说明一切:
#include <iostream>
int main(int argc, char *argv[]) {
auto l1 = []() { std::cout << "Lambda 1!" << std::endl; };
auto l2 = [] { std::cout << "Lambda 2!" << std::endl; };
auto l1p = &l1;
l1p = &l2; // Why can't I do this assignment?
return 0;
}
Run Code Online (Sandbox Code Playgroud)
由于两个lambda函数的返回类型和参数是相同的,为什么我不能做这个赋值?
以下两种铸件有什么区别吗?
int a=10;
int *p=&a;
(void)p; //does not give any warning or error
Run Code Online (Sandbox Code Playgroud)
要么
(void *)p; //error: statement with no effect [-Werror=unused-value]
Run Code Online (Sandbox Code Playgroud)
什么时候遵守 gcc -Wall -Werror --std=c99 -pedantic
刚看到这个答案. (显然我误解了一些事情)
在普通的c/c ++程序中,我们将main函数编写为
int main(int c, char **argv)
Run Code Online (Sandbox Code Playgroud)
要么
int main(int c, char *argv[])
Run Code Online (Sandbox Code Playgroud)
这里argv表示一个指针数组,但我们甚至用**表示双指针(指向指针).
例如:
char p,*q,**r;
q=&p;
r=&q;
Run Code Online (Sandbox Code Playgroud)
这里r是双指针而不是指针数组.
任何人都可以解释这个区别吗?
我不清楚这些之间的区别是什么2.我的教授写道**数组与*array []相同,我们得到了一个例子,他使用了**数组(所以在类之后我尝试用*array交换它] ]它没有用),谁能告诉我这些2是否与他写的一样?无论如何,这个类是关于动态内存分配的
@一旦我改变了双指针,这一行开始抛出错误
lines = malloc(sizeof(char*));
Run Code Online (Sandbox Code Playgroud)
以及其他一些内存重新分配的地方
@ 2地狱耶,这是整个代码
对于那些评论吼叫,因为他的陈述是,因为[]内部没有任何内容
**array = *array[]
Run Code Online (Sandbox Code Playgroud)
大新闻
对于给您带来的任何不便,我感到非常抱歉,在写这篇文章的时候我太累了,这里是整个代码,没有编辑
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char **lines; // global text buffer, organized as an array of lines
// --------------------------------------------------------------------------------
// initialize global buffer
void initialize()
{
lines = malloc(sizeof(char*));
lines[0] = NULL;
}
// --------------------------------------------------------------------------------
// return number of lines in buffer
int countLines()
{
int count = 0;
while(lines[count++]) ;
return count-1;
}
// --------------------------------------------------------------------------------
// print one line
void printLine(int line) …Run Code Online (Sandbox Code Playgroud) 当我编译下面的代码时gcc -Wall -pedantic -ansi -std=c89,它编译成功,而不会在指针赋值时给出错误.请注意,我转换int (*)[4]为int (*)[].
int arr[4];
int (*p_arr)[] = &arr;
Run Code Online (Sandbox Code Playgroud)
假设有一些理由允许这个(不兼容?)赋值,当我尝试使用它时,编译器会给出不完整的类型错误error: invalid application of ‘sizeof’ to incomplete type ‘int[]’.
(void) sizeof(*p_arr);
Run Code Online (Sandbox Code Playgroud)
这个错误让我想到允许先前指针赋值的用途是什么p_arr = &arr?这个作业是否按照标准允许?
我使用了不完整的struct/union类型(通常用于前向声明)并且也遇到了错误incomplete array element type.但这incomplete array type对我来说是新的.是否可以在C标准中使用并具有用例?
我想要一个带有指针成员变量的类.该指针应指向可以堆栈分配或堆分配的对象.但是,此指针不应具有任何所有权.换句话说,当指针超出范围时,根本不应该调用delete.我认为原始指针可以解决问题...但是,我不确定是否有比原始指针更好的C++ 11方法?
例:
class foo{
public:
bar* pntr
};
int main(){
bar a;
foo b;
b.pntr=&a;
}
Run Code Online (Sandbox Code Playgroud)