C-从一个函数中获取正确的指针并通过另一个函数打印
我无法将我的C-从一个函数中获取正确的指针并通过另一个函数打印,c,pointers,C,Pointers,我无法将我的指针指向正确的“框”,使其通过第二个函数使用链表打印编号。我不会复制我的全部代码,因为这会有点作弊,我的目的是学习。因此,我将写一个简单的例子,我想做什么 typedef struct{ int number; struct node *next; } mystruct; void main() { char numb[1000]; scanf("%s",numb); mystruct *head=malloc(sizeof(mystr
指针
指向正确的“框”,使其通过第二个函数
使用链表
打印编号
。我不会复制我的全部代码,因为这会有点作弊,我的目的是学习。因此,我将写一个简单的例子,我想做什么
typedef struct{
int number;
struct node *next;
} mystruct;
void main()
{
char numb[1000];
scanf("%s",numb);
mystruct *head=malloc(sizeof(mystruct));
CreateList(numb, &head);
PrintList(head);
}
CreateList(char x[1000],mystruct **head)
{
int i;
int digits=strlen(x)
for (i=0;i<digits;i++)
{
// creating the linked list. meaning each digit of number going into a "box"
}
}
typedef结构{
整数;
结构节点*下一步;
}我的结构;
void main()
{
字符数[1000];
scanf(“%s”,麻木);
mystruct*head=malloc(sizeof(mystruct));
CreateList(麻木和头部);
印刷品清单(标题);
}
CreateList(字符x[1000],mystruct**head)
{
int i;
整数位数=strlen(x)
对于(i=0;i#包括
#包括
类型定义结构节点{
整数;
结构节点*下一步;
}我的结构;
void CreateList(char x[1000],mystruct**head);
作废打印列表(mystruct*head);
无效自由列表(mystruct*head);
内部主(空){
字符数[1000];
scanf(“%999s”,麻木);
我的结构*头;
CreateList(麻木和头部);
印刷品清单(标题);
自由名单(标题);
返回0;
}
void CreateList(char x[1000],mystruct**head){
mystruct*currNode,*newNode;
*head=NULL;
int i;
对于(i=0;x[i];i++){//x[i]!='\0'
if(NULL==(newNode=malloc(sizeof(*newNode))){
perror(“malloc错误”);
退出(退出失败);
}
如果(i==0){
*head=currNode=newNode;
}
newNode->number=x[i];//x[i]-“0”
newNode->next=NULL;
currNode=currNode->next=newNode;
}
}
无效打印列表(mystruct*head){
while(head){
printf(“%c”,头部->数字);//%d
头部=头部->下一步;
}
putchar('\n');
}
无效自由列表(mystruct*head){
while(head){
mystruct*temp=头部;
头部=头部->下一步;
免费(临时);
}
}
我不知道如何“返回”指针地址,甚至不知道在哪里给它一个地址。这有点让人困惑。这可能是因为我还是一个初学者,但这应该是可以理解的。不确定我的错误是什么“所以我将写一个简单的示例,说明我想做什么…”你没有。@alk我意识到…基本上这就是我的意思。用户给出一个数字。例如12345。通过使用指针,数字的每个数字都成为列表中的一个元素。然后我想在屏幕上打印数字,使用指针打印列表中已创建的每个元素。我无法解释这一点。
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int number;
struct node *next;
} mystruct;
void CreateList(char x[1000], mystruct **head);
void PrintList(mystruct *head);
void FreeList(mystruct *head);
int main(void){
char numb[1000];
scanf("%999s", numb);
mystruct *head;
CreateList(numb, &head);
PrintList(head);
FreeList(head);
return 0;
}
void CreateList(char x[1000], mystruct **head){
mystruct *currNode, *newNode;
*head = NULL;
int i;
for (i=0;x[i];i++){//x[i] != '\0'
if(NULL == (newNode = malloc(sizeof(*newNode)))){
perror("error of malloc");
exit(EXIT_FAILURE);
}
if(i==0){
*head = currNode = newNode;
}
newNode->number = x[i];//x[i] - '0'
newNode->next = NULL;
currNode = currNode->next = newNode;
}
}
void PrintList(mystruct *head){
while(head){
printf("%c", head->number);//%d
head = head->next;
}
putchar('\n');
}
void FreeList(mystruct *head){
while(head){
mystruct *temp = head;
head = head->next;
free(temp);
}
}