如何设置一个数字的整数指针,并使其表现为非指针整数?

use*_*862 2 c pointers

这是当前无效的代码.我需要j作为指针.

Substring(const char a[],int x,int y,char b[])
{
    int *j=0;
    for(x;x<=y&&a[x]!='\0';x++)
    { b[*j]=a[x];
         *j++;}
    b[*j] ='\0';
    return (b);

}
Run Code Online (Sandbox Code Playgroud)

以下代码运行良好,唯一的问题是j不是指针.

Substring(const char a[],int x,int y,char b[])
    {
        int j=0;
        for(;x<=y&&a[x]!='\0';x++)
        { b[j]=a[x];
             j++;}
        b[j] ='\0';
        return (b);

    }
Run Code Online (Sandbox Code Playgroud)

我希望第一个代码表现得像第二个,如何做到这一点的任何想法?代码编译并执行,但它停止工作.调试没有帮助.我不能使用多于1个变量-j.

Ker*_* SB 7

像这样:

int real_j = 0;
int * j = &real_j;

// ...

++(*j);
Run Code Online (Sandbox Code Playgroud)

这当然完全没有意义.

  • @KerrekSB哈哈POINTLESS !! 好双关语 (2认同)

Ani*_*nge 6

不好了

int *j = 0;j指向0(或NULL).

方法1:

int jx = 0;
int *j = &jx;
Run Code Online (Sandbox Code Playgroud)

方法2:

    int *j = malloc(sizeof(int));
    *j = 0;
....
 ///don't forget to free(j);
Run Code Online (Sandbox Code Playgroud)