小编hun*_*986的帖子

使用指针时C中的分段错误

我不知道这里发生了什么:

#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)

错误发生printffor循环中的语句之后,当我*从两侧移除它时,但答案是错误的.

这是使用C中的指针的插入排序.

c pointers

2
推荐指数
1
解决办法
687
查看次数

在c ++/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 ++中通过引用传递?

c c++ pass-by-reference

0
推荐指数
1
解决办法
443
查看次数

标签 统计

c ×2

c++ ×1

pass-by-reference ×1

pointers ×1