#2
rjsp2023-04-06 14:28
|
程序代码:
struct Sarg
{
int count;
char* ptr;
};
int add(Sarg& a)
{
int sum = 0;
int* fp = (int*)a.ptr;
for(int i=0;i<a.count;i++)
{
sum += fp[i];
}
return sum;
};
int main()
{
Sarg a;
a.count = 5;
a.ptr = (char*)new int[5]{ 1,2,3,4,5 };
std::cout << add(a);
system("pause");
}
{
int count;
char* ptr;
};
int add(Sarg& a)
{
int sum = 0;
int* fp = (int*)a.ptr;
for(int i=0;i<a.count;i++)
{
sum += fp[i];
}
return sum;
};
int main()
{
Sarg a;
a.count = 5;
a.ptr = (char*)new int[5]{ 1,2,3,4,5 };
std::cout << add(a);
system("pause");
}
请问怎么把这段程序修改成可以传入字符串呢?就是往add里面添加参数时,可以让他传入字符串或者float等值