[英]Assigning values to initialized pointers
When we declare a pointer, we usually initialize it to a variable (memory mapping) and then assign a value to it. 声明指针时,通常将其初始化为变量(内存映射),然后为其分配值。
#include <stdio.h>
int i=0;
int cir_shift(int *x,int *y);
int main()
{
int a,b,c,*temp,*d;
d=&c;
scanf("%d %d %d",&a,&b,&c);
temp=(d+3);
*temp=c;
cir_shift(d,temp);
printf("%d %d %d",a,b,c);
}
int cir_shift(int *x,int *y)
{
*x=*(x+1);
i++;
if(i==3)
{
return 0;
}
else
{
x++;
cir_shift(x,y);
}
}
Here, when I want to print the value of temp (*temp), it keeps crashing. 在这里,当我要打印temp(* temp)的值时,它一直崩溃。 I did initialize 'temp' to a memory address, but still it crashes. 我确实将'temp'初始化为一个内存地址,但是仍然崩溃。 And, when I want to print the memory address stored at temp, it prints the value at temp (*temp). 并且,当我要打印以temp存储的内存地址时,它将以temp(* temp)的值打印。 What seems to be the problem and what may the solution be? 问题似乎在哪里,解决方案可能是什么? Thank you. 谢谢。
temp=(d+3);
*temp=c;
d + 3
means 3
int
after where d
is pointing to. d + 3
表示d
指向的位置之后的3
int
。 It is an invalid address as you didn't allocate anything there. 这是无效的地址,因为您未在其中分配任何内容。 So then trying to write to *temp
unleashes the infinite revenge of your implementation. 因此,然后尝试写入*temp
释放实现的无限报仇。
You should explain what you are trying to achieve with temp=(d+3);
您应该解释您尝试使用temp=(d+3);
? ?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.