gpt4 book ai didi

c++ - 将数组推送到 C++ 中的函数?

转载 作者:行者123 更新时间:2023-11-28 07:40:24 25 4
gpt4 key购买 nike

        {
int a[3];
mainClassStack.pushNumber(a[1,2,3]);
break;
}

void stack_class::pushNumber(int numberFunc)
{
if(head==NULL)
{
head = new stack_struct;
head->number = numberFunc;
head->next_number = NULL;
tailPointer=head;
}
else
{
pointerFunc = new stack_struct;
pointerFunc->number=numberFunc;
pointerFunc->next_number=NULL;
head->next_number=pointerFunc;
head=pointerFunc;
}
}


void stack_class::pushNumber(char charFunc)
{

int a=0;
a=charFunc;
if(head==NULL)
{
head = new stack_struct;
head->number = a;
head->next_number = NULL;
tailPointer=head;
}
else
{
pointerFunc = new stack_struct;
pointerFunc->number=a;
pointerFunc->next_number=NULL;
head->next_number=pointerFunc;
head=pointerFunc;
}

}


void stack_class::pushNumber(int arrayFunc[3])
{
if(head==NULL)
{
for(int i=0;i<3;i++)
{
head = new stack_struct;
head->number = arrayFunc[i];
head->next_number = NULL;
tailPointer=head;
}
}
else
{
for(int i=0;i<3;i++)
{
pointerFunc = new stack_struct;
pointerFunc->number=arrayFunc[i];
pointerFunc->next_number=NULL;
head->next_number=pointerFunc;
head=pointerFunc;
}
}
}

我正在重载函数并将数组推送到适当的函数中,该函数稍后会将数组中的值添加到动态链表中。到目前为止我已经写了这个,但是当我尝试打印链接列表时,它显示垃圾。我在这里做错了什么?

最佳答案

这对您来说更有意义吗?

void Foo(int arg[])
{
printf("%i\n", arg[0]);
printf("%i\n", arg[1]);
printf("%i\n", arg[2]);
}

int main()
{
int a[3] = {1,2,3};
Foo(a);
return 0;
}

输出是:

1
2
3

关于c++ - 将数组推送到 C++ 中的函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15940325/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com