[英]How can I copy value in a struct that is passed by reference to the function and member A is also a pointer?
I am learning the pointer and I stuck on an issue as I cannot debug the program because it gives me address rather than the actual array.我正在学习指针,但我遇到了一个问题,因为我无法调试程序,因为它给了我地址而不是实际的数组。
I am trying to create an Abstract datatype of Array我正在尝试创建 Array 的抽象数据类型
struct ARRAY_ADT
{
int *A; // For storing the pointer to base address of the array
int size; //For storing the size of the array
int len; //For storing the length of the array
};
Now to insert a number in it's correct index I had written a function called Insert .现在要在正确的索引中插入一个数字,我编写了一个名为Insert的 function 。 I am calling it from main.
我从 main 调用它。
It's look like this它看起来像这样
void insert(struct ARRAY_ADT *Array)
{
int num;
int index;
int i = Array -> len - 1;
printf("Enter the value to be inserted: ");
scanf("%d", &num);
printf("Enter the value to be index: ");
scanf("%d", &index);
printf("Array length:%d \n", Array ->len);
if(OutofRange(Array -> size, Array -> len))
{
printf("Array is full:");
}
else
{
if (OutofRange(Array -> size, index))
{
printf("Index is not valid");
}
else
{
//int *temp = NULL;
while(i >= index)
{
Array->(A+i+1) = Array->(A+i); // Problem
i--;
}
Array -> A[index] = num;
Array -> len++;
}
}
}
So, how can I copy value from right to left to make place for the new number.那么,我怎样才能从右到左复制值来为新数字腾出位置。
How can I copy value in a struct that is passed by reference to the function and member A is also a pointer?如何复制通过引用传递给 function 且成员 A 也是指针的结构中的值?
I just want to perform this operation我只想执行这个操作
Array.A[i++] = Array.A[i];
Use Array->A[i+1] = Array->A[i]
.使用
Array->A[i+1] = Array->A[i]
。 Or you can shift without a loop by using memmove()
或者您可以使用
memmove()
进行无循环移位
memmove(&(Array->A[i+1]), &(Array->A[i]), sizeof(Array->A[i]) * (Array->len - index));
right is正确的是
struct xntype
{
int *b;
} xn;
*(xn.b + 1) = 15;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.