[英]implement a pointer function to traverse the elements in the array
I want to traverse the array element with pointer function by C.我想用 C 的指针 function 遍历数组元素。 However, there's some limitations about doing the project.
但是,做这个项目有一些限制。
Here is the array:这是数组:
ary[5] = {1,2,3,4,5}
I have to create three pointer-int-funciton like as follow:我必须创建三个指针整数函数,如下所示:
int* begin();
int* end();
int* next();
void printArray();
So, when I implement the funciton, I doing the way like this:所以,当我实现这个功能时,我会这样做:
//define the begin function
int* begin(){
int *index = &ary[0];
return index;
}
//define the next function
int* next(){
int *count = ary;
count ++;
return count;
}
//define the end function
int* end(){
int *index = ary;
while (*index < &ary[5])
{
index ++;
}
}
printArray function:打印阵列 function:
void printArray(){
for (begin(); next() <= end(); next())
{
printf("%d ",*next());
}
}
However, the next() function could not work, because using the upper code will enter an Infinite loop.但是,next() function 不能工作,因为使用上面的代码会进入一个无限循环。 I would like to code like could++.
我想像 can++ 一样编写代码。 I hope someone can figure the problem with me.
我希望有人能解决我的问题。
int ary[5] = {1, 2, 3, 4, 5}
, you missed the type.int ary[5] = {1, 2, 3, 4, 5}
,您错过了类型。next()
, you get a copy of ary's address to count
, count++
only increment the copied pointer, not the ary
itself, so you will print infinite 2
on the screen.next()
时,都会得到 ary 的地址副本到count
, count++
只会增加复制的指针,而不是ary
本身,因此您将在屏幕上打印无限2
。ary
because it's an array not a pointer, or you will get error.ary
,因为它是数组而不是指针,否则会出错。 A simple way to print an int array like this:一种打印 int 数组的简单方法,如下所示:
int main(){
int ary[5] = {1, 2, 3, 4, 5};
int *ptr = ary;
while(ptr <= &ary[4]){
printf("%d ", *ptr);
ptr++;
}
return 0;
}
Modified code of your functions:修改后的函数代码:
int ary[5] = {1,2,3,4,5};
int *ptr;
int* begin(); //set ptr to the address of ary, and return it
int* end(); //the memory space address of the last element of the array
void next(); //increment the ptr
void printArray(); //print out the values in the array
int* begin(){
ptr = &ary[0];
return ptr;
}
void next(){
ptr++;
}
int* end(){
//return &ary[4];
return &ary[sizeof(ary)/sizeof(ary[0])-1];
}
void printArray(){
for (begin(); ptr <= end(); next())
{
printf("%d ",*ptr);
}
}
int main(){
printArray();
return 0;
}
Here is my code as below:这是我的代码如下:
int* begin();
int* end();
int* next();
void printArray();
int ary[SIZE] = {},
data;
static int *pointer;
The main function as below:主要的function如下:
//Using the for-loop to implement the program
for (int i = 0; i < SIZE; i ++)
{
//input the number to the array
printf("Enter the number: ");
scanf("%d", &data);
if (data < 0)
{
break;
}else{
ary[i] = data;
}
}
//define the begin function
int* begin(){
pointer = &ary[0];
return pointer;
}
//define the next function
int* next(){
pointer ++;
return pointer;
}
//define the end function
int* end(){
return &ary[sizeof(ary)/sizeof(ary[0])-1];
}
//define the printArray function
void printArray(){
for (begin(); pointer <= end(); next())
{
printf("%d ",*pointer);
}
}
If I enter the 3 number, for example 1 2 3, the results will not get the end of the number 3, at the same time, it will print out 1 2 3 0 0. I know that I have to remove the last two element(that doesn't input to the ary).如果我输入 3 的数字,例如 1 2 3,结果不会得到数字 3 的结尾,同时会打印出 1 2 3 0 0。我知道我要去掉最后两个元素(不输入到 ary)。 So, I feel a little confused.
所以,我感到有些困惑。 How can I solve it?
我该如何解决?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.