我不知道这里发生了什么:
#include<stdio.h>
int main()
{
int i, j, *k, x,array[]={5,3,4,1,8,9,2,7,6,0};
int *ptr=array;
for(j=1;j<10;j++) {
printf("---------iteration %d--------------\n",j);
*k=*(ptr+j); // the segmentation error is occurring here at this line
printf("key=%d\n",*k);
i=j-1;
while( i>=0 && *k < *(ptr+i)) {
*(ptr+i+1)=*(ptr+i);
i--;
}
*(ptr+i+1) = *k;
printf("%d\n",*(ptr+i+1));
for( x=0;x<10;x++)
printf("%d,",*(ptr+x));
printf("\n");
}
for( i=0;i<10;i++)
printf("%d,",*ptr++);
printf("\n");
}
Run Code Online (Sandbox Code Playgroud)
错误发生printf
在for
循环中的语句之后,当我*
从两侧移除它时,但答案是错误的.
这是使用C中的指针的插入排序.
#include<stdio.h>
void sq(int &b) {
b=b+12;
}
void main() {
int a=5;
sq(a);
printf("%d",a);
}
Run Code Online (Sandbox Code Playgroud)
在上面的c程序中,它不起作用,但在c ++中也是如此
#include<iostream>
void sq(int &b) {
b=b+12;
}
int main() {
int a=5;
sq(a);
std::cout<<a;
}
Run Code Online (Sandbox Code Playgroud)
在c ++中传递变量的方式有区别吗?在c ++中工作的whydoes?上面的代码在c ++中通过引用传递?