很难说出星号与指针的关系.
以下是一些令我困惑的代码示例.
typedef struct X { int i_; double d_; } X;
X x;
X* p = &x;
p->d_ = 3.14159; // dereference and access data member x.d_
(*p).d_ *= -1; // another equivalent notation for accessing x.d_
Run Code Online (Sandbox Code Playgroud)
在第五行,我们有(*p).d_ *= -1;.为什么有两个星号?他们的立场意味着什么?
int x = 2;
int* p_x = &x; // put the address of the x variable into the pointer p_x
*p_x = 4; // change the memory at the address in p_x to be 4
assert(x == …Run Code Online (Sandbox Code Playgroud) 我正在使用Objective-C制作国际象棋游戏.在这个游戏中我需要使用BOOL*而不是BOOL因为我需要一个指向布尔变量的指针.当我尝试使用BOOL*时,它会在我尝试执行此操作时向我发出警告:
BOOL *isWhiteTurn;
isWhiteTurn = YES;
Run Code Online (Sandbox Code Playgroud)
该警告是:
不兼容的整数到指针转换从'BOOL'(又名'signed char')分配给'BOOL*'(又名'signed char*')
我有一些理解左移操作符的问题.我知道如果向左移位x位数,那么将数字乘以2几位乘以移位的位数.
但考虑一下这个程序:
int a=4,*pA=&a,r3;
printf("%d\n",pA);
printf("%d\n",(*pA<<2));
r3=pA-(*pA<<2);
printf("%d",r3);
Run Code Online (Sandbox Code Playgroud)
它分别打印出pA和(*pA << 2),如果要减去这两个,它将不等于表达式中写入的r3,关闭4次,即int的大小.但是你需要考虑int的大小,因为你有pA和(*pA << 2),并且它们的减法不等于它的预期.
任何帮助表示赞赏......
为了记录,我对打印出指针的实际值不感兴趣,但实际上它的地址被移位了2位.我不明白正在发生的过程.
我正在使用 c 学习递归,并且有一个关于数组的问题,例如:
void main(){
char str[100];
--snip--
if(*str == 'c')
count++;
--snip--
}
Run Code Online (Sandbox Code Playgroud)
这里的 *str 是从指向字符串str 的第一个字符的指针中检索到的值。
我的问题是什么是指针取消引用,我可以执行以下操作:
--snip--
str+=1;
--snip--
Run Code Online (Sandbox Code Playgroud)
获取指向该字符串中另一个字符的下一个位置的新指针?
当你在C中声明静态变量时,
说你写了这样一个程序:
int *c;
void foo(){
c = (int *) malloc (10*sizeof(int));
c = &c[3];
*c = *&c[3];
}
Run Code Online (Sandbox Code Playgroud)
有什么意义*c?我的意思是我理解这*意味着它指的是什么,但*c做了什么?
而且,为什么需要转换(int *)返回值malloc()?
我对C很新,有时我会遇到奇怪的符号,特别是与指针有关.
一个很短的例子:
....
real *ptr;
real delta_force;
for(i<particles;...)
{
...some calculations
ptr=&FORCE(i,...); //FORCE is a macro returning the current force on a particle
*(++ptr) += delta_force;
...
}
...
Run Code Online (Sandbox Code Playgroud)
我怎么解释*(++ptr)?