这个概念似乎给我带来了麻烦.为什么NSError对象需要将其指针传递给正在修改对象的方法?例如,不仅仅是传递对错误的引用做同样的事情吗?
NSError *anError;
[myObjc doStuff:withAnotherObj error:error];
Run Code Online (Sandbox Code Playgroud)
然后在doStuff中:
- (void)doStuff:(id)withAnotherObjc error:(NSError *)error
{
// something went bad!
[error doSomethingToTheObject];
}
Run Code Online (Sandbox Code Playgroud)
为什么上述工作不像大多数其他对象消息传递模式一样有效?为什么必须使用错误:(NSError**)错误?
是char*,int*,long*甚至是long long*同样大小的(在给定的平台)?
很久以前我曾经在C学校上学.我记得我真的讨厌C的东西:未分配的指针不指向NULL.
我问过包括教师在内的很多人为什么他们会将未分配指针的默认行为指向NULL,因为它似乎更难以预测.
答案应该是表现,但我从未买过.我认为,如果C默认为NULL,那么编程历史中的许多错误都可以避免.
这里有一些C代码指出(双关语)我在说什么:
#include <stdio.h>
void main() {
int * randomA;
int * randomB;
int * nullA = NULL;
int * nullB = NULL;
printf("randomA: %p, randomB: %p, nullA: %p, nullB: %p\n\n",
randomA, randomB, nullA, nullB);
}
Run Code Online (Sandbox Code Playgroud)
编译警告(很高兴看到C编译器比我在学校时更好)和输出:
randomA:0xb779eff4,randomB:0x804844b,nullA:(nil),nullB:(nil)
我环顾四周,但一直无法找到解决问题的问题.这是我的代码:
#include <stdlib.h>
struct my_struct {
int n;
char s[]
};
int main()
{
struct my_struct ms;
ms.s = malloc(sizeof(char*)*50);
}
Run Code Online (Sandbox Code Playgroud)
这里是错误gcc给我的:错误:无效使用灵活的数组成员
如果我在结构中声明s的声明,我可以编译它
char* s
Run Code Online (Sandbox Code Playgroud)
这可能是一个优秀的实现(指针算法比数组快,是吗?)但我想在ca声明中
char s[]
Run Code Online (Sandbox Code Playgroud)
是相同的
char* s
Run Code Online (Sandbox Code Playgroud) 将一些地图定义为:
var valueToSomeType = map[uint8]someType{...}
var nameToSomeType = map[string]someType{...}
Run Code Online (Sandbox Code Playgroud)
我想要一个指向地图地址的变量(不要复制所有变量).我尝试使用:
valueTo := &valueToSomeType
nameTo := &nameToSomeType
Run Code Online (Sandbox Code Playgroud)
但在使用时valueTo[number],它显示
内部编译器错误:var没有类型,init:new
怎么弄?
编辑
另一个问题显示错误.
我使用以下代码来从文件中读取数据,作为更大程序的一部分.
double data_read(FILE *stream,int code) {
char data[8];
switch(code) {
case 0x08:
return (unsigned char)fgetc(stream);
case 0x09:
return (signed char)fgetc(stream);
case 0x0b:
data[1] = fgetc(stream);
data[0] = fgetc(stream);
return *(short*)data;
case 0x0c:
for(int i=3;i>=0;i--)
data[i] = fgetc(stream);
return *(int*)data;
case 0x0d:
for(int i=3;i>=0;i--)
data[i] = fgetc(stream);
return *(float*)data;
case 0x0e:
for(int i=7;i>=0;i--)
data[i] = fgetc(stream);
return *(double*)data;
}
die("data read failed");
return 1;
}
Run Code Online (Sandbox Code Playgroud)
现在我被告知使用-O2,我得到以下gcc警告:
warning: dereferencing type-punned pointer will break strict-aliasing rules
谷歌我找到了两个正交的答案:
VS
我的理解是数组只是指向一系列值的常量指针,当你在C中声明一个数组时,你就是声明一个指针并为它所指向的序列分配空间.
但这让我感到困惑:以下代码:
char y[20];
char *z = y;
printf("y size is %lu\n", sizeof(y));
printf("y is %p\n", y);
printf("z size is %lu\n", sizeof(z));
printf("z is %p\n", z);
Run Code Online (Sandbox Code Playgroud)
使用Apple GCC编译时会得到以下结果:
y size is 20
y is 0x7fff5fbff930
z size is 8
z is 0x7fff5fbff930
Run Code Online (Sandbox Code Playgroud)
(我的机器是64位,指针长8个字节).
如果'y'是常量指针,为什么它的大小为20,就像它指向的值序列一样?变量名'y'是否在编译时被内存地址替换为适当的?那么数组是C中的某种语法糖,它在编译时只是转换为指针的东西吗?
我最近开始学习C,并且我在理解指针语法方面遇到了问题,例如当我写下以下行时:
int ** arr = NULL;
Run Code Online (Sandbox Code Playgroud)
我怎么知道:
arr是指向整数指针的指针
arr是指向整数指针数组的指针
arr是指向整数数组指针数组的指针
是不是都一样int **?
如果我有一个char ** s作为参数接收的函数,我想将它称为一个pointer字符串数组,这意味着指向一个指向数组的指针数组的指针chars,但它是否也指向指向char?的指针?
我在for循环中使用指针时遇到问题。在for循环初始化程序中,我取消引用int指针并将其值设置为“ 0”。当我在循环中使用该取消引用的指针时,出现了段错误,并且我不明白为什么。我正在使用Code :: Blocks和C GNU GCC编译器。
看着观察窗口,我可以看到在for循环过程中变量有一个随机数。
似乎已取消引用的指针在for循环期间丢失了作用域。
代码:
#include <stdio.h>
int main(void)
{
int val = 0;
int *p = NULL;
int answer = 0;
p = &val;
*p = 1; // This dereferences and sets to one successfully
for (int i=3, (*p)=0 ; i>=0; i--) // Here *p is a random number
{
printf("do stuff");
(*p) += 1; // Here it causes a segmentation fault
} …Run Code Online (Sandbox Code Playgroud) 我无法理解如何正确地确保nil在这种情况下不存在某些事情:
package main
type shower interface {
getWater() []shower
}
type display struct {
SubDisplay *display
}
func (d display) getWater() []shower {
return []shower{display{}, d.SubDisplay}
}
func main() {
// SubDisplay will be initialized with null
s := display{}
// water := []shower{nil}
water := s.getWater()
for _, x := range water {
if x == nil {
panic("everything ok, nil found")
}
//first iteration display{} is not nil and will
//therefore work, on the second iteration …Run Code Online (Sandbox Code Playgroud) pointers ×10
c ×8
arrays ×3
go ×2
cocoa ×1
declaration ×1
dictionary ×1
for-loop ×1
gcc ×1
interface ×1
null ×1
objective-c ×1
optimization ×1
struct ×1
syntax ×1
types ×1