这是当前无效的代码.我需要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.
像这样:
int real_j = 0;
int * j = &real_j;
// ...
++(*j);
Run Code Online (Sandbox Code Playgroud)
这当然完全没有意义.
不好了
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)