如何通过引用将结构数组传递给函数?
How can I pass an array of structures to a function by reference?
#include <stdio.h>
void changeValues(struct ITEM *item[]);
struct ITEM
{
int number;
};
int main(void)
{
struct ITEM items[10];
for (int i = 0; i < 10; i++)
{
items[i].number = i;//initialize
printf("BEFORE:: %d\n", items[i].number);
}
changeValues(items);
for (int i = 0; i < 10; i++)
{
items[i].number = i;
printf("AFTER:: %d\n", items[i].number);
}
return 0;
}
void changeValues(struct ITEM *item[])
{
for (int i = 0; i < 10; i++)
item[i] -> number += 5;
}
我正在尝试将结构数组传递给函数。我需要通过引用而不是值来更改函数内结构成员的值。出于某种奇怪的原因,当我在调用函数后打印结果时,值与调用函数之前的值保持不变。
在 C 中你不能通过引用传递(像 C++)。只能按值传递,也可以按指针传递。
在这种情况下,您似乎想将结构数组传递给函数changeValues
。这就是您在 main
中所做的。但是,changeValues
的原型和实现实际上是尝试将指针数组传递给 struct ITEM
.
一个可能的解决方法是将指向结构 ITEM
的指针数组更改为结构数组。
void changeValues(struct ITEM item[])
{
for (int i = 0; i < 10; i++)
{
item[i].number += 5;
}
}
编辑:您的代码中实际上还有另外两个错误:
1) struct ITEM
的定义需要在changeValues
原型之前:
struct ITEM
{
int number;
};
void changeValues(struct ITEM item[]);
2) 在你的 main()
中你实际上重置了 changeValues
中的所有值 - 基本上你使该函数中所做的一切都无效:
for (int i = 0; i < 10; i++)
{
items[i].number = i; // <-- Remove this line as you are resetting the value again here
printf("AFTER:: %d\n", items[i].number);
}
struct ITEM
{
int number;
};
void changeValues(struct ITEM item[]);
int main(void)
{
struct ITEM items[10];
for (int i = 0; i < 10; i++)
{
items[i].number = i;//initialize
printf("BEFORE:: %d\n", items[i].number);
}
changeValues(items);
for (int i = 0; i < 10; i++)
{
// items[i].number = i; // <-- Remove this line as you are resetting the value again here
printf("AFTER:: %d\n", items[i].number);
}
return 0;
}
void changeValues(struct ITEM items[])
{
for (int i = 0; i < 10; i++)
{
items[i].number += 5;
}
}
您可以'pass as reference'通过传递指针地址。
这是一个例子:
int main(void)
{
char *pA; /* a pointer to type character */
char *pB; /* another pointer to type character */
puts(strA); /* show string A */
pA = strA; /* point pA at string A */
puts(pA); /* show what pA is pointing to */
pB = strB; /* point pB at string B */
putchar('\n'); /* move down one line on the screen */
while(*pA != '[=10=]') /* line A (see text) */
{
*pB++ = *pA++; /* line B (see text) */
}
*pB = '[=10=]'; /* line C (see text) */
puts(strB); /* show strB on screen */
return 0;
}
#include <stdio.h>
void changeValues(struct ITEM *item[]);
struct ITEM
{
int number;
};
int main(void)
{
struct ITEM items[10];
for (int i = 0; i < 10; i++)
{
items[i].number = i;//initialize
printf("BEFORE:: %d\n", items[i].number);
}
changeValues(items);
for (int i = 0; i < 10; i++)
{
items[i].number = i;
printf("AFTER:: %d\n", items[i].number);
}
return 0;
}
void changeValues(struct ITEM *item[])
{
for (int i = 0; i < 10; i++)
item[i] -> number += 5;
}
我正在尝试将结构数组传递给函数。我需要通过引用而不是值来更改函数内结构成员的值。出于某种奇怪的原因,当我在调用函数后打印结果时,值与调用函数之前的值保持不变。
在 C 中你不能通过引用传递(像 C++)。只能按值传递,也可以按指针传递。
在这种情况下,您似乎想将结构数组传递给函数changeValues
。这就是您在 main
中所做的。但是,changeValues
的原型和实现实际上是尝试将指针数组传递给 struct ITEM
.
一个可能的解决方法是将指向结构 ITEM
的指针数组更改为结构数组。
void changeValues(struct ITEM item[])
{
for (int i = 0; i < 10; i++)
{
item[i].number += 5;
}
}
编辑:您的代码中实际上还有另外两个错误:
1) struct ITEM
的定义需要在changeValues
原型之前:
struct ITEM
{
int number;
};
void changeValues(struct ITEM item[]);
2) 在你的 main()
中你实际上重置了 changeValues
中的所有值 - 基本上你使该函数中所做的一切都无效:
for (int i = 0; i < 10; i++)
{
items[i].number = i; // <-- Remove this line as you are resetting the value again here
printf("AFTER:: %d\n", items[i].number);
}
struct ITEM
{
int number;
};
void changeValues(struct ITEM item[]);
int main(void)
{
struct ITEM items[10];
for (int i = 0; i < 10; i++)
{
items[i].number = i;//initialize
printf("BEFORE:: %d\n", items[i].number);
}
changeValues(items);
for (int i = 0; i < 10; i++)
{
// items[i].number = i; // <-- Remove this line as you are resetting the value again here
printf("AFTER:: %d\n", items[i].number);
}
return 0;
}
void changeValues(struct ITEM items[])
{
for (int i = 0; i < 10; i++)
{
items[i].number += 5;
}
}
您可以'pass as reference'通过传递指针地址。
这是一个例子:
int main(void)
{
char *pA; /* a pointer to type character */
char *pB; /* another pointer to type character */
puts(strA); /* show string A */
pA = strA; /* point pA at string A */
puts(pA); /* show what pA is pointing to */
pB = strB; /* point pB at string B */
putchar('\n'); /* move down one line on the screen */
while(*pA != '[=10=]') /* line A (see text) */
{
*pB++ = *pA++; /* line B (see text) */
}
*pB = '[=10=]'; /* line C (see text) */
puts(strB); /* show strB on screen */
return 0;
}