![](/img/trans.png)
[英]Embedded C: Can I pass members of a static struct to function in another file, IF a pointer to the member is passed by a fn in the same file
[英]How can I copy value in a struct that is passed by reference to the function and member A is also a pointer?
我正在学习指针,但我遇到了一个问题,因为我无法调试程序,因为它给了我地址而不是实际的数组。
我正在尝试创建 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
};
现在要在正确的索引中插入一个数字,我编写了一个名为Insert的 function 。 我从 main 调用它。
它看起来像这样
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++;
}
}
}
那么,我怎样才能从右到左复制值来为新数字腾出位置。
如何复制通过引用传递给 function 且成员 A 也是指针的结构中的值?
我只想执行这个操作
Array.A[i++] = Array.A[i];
使用Array->A[i+1] = Array->A[i]
。 或者您可以使用memmove()
进行无循环移位
memmove(&(Array->A[i+1]), &(Array->A[i]), sizeof(Array->A[i]) * (Array->len - index));
正确的是
struct xntype
{
int *b;
} xn;
*(xn.b + 1) = 15;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.