C 创建链接列表时遇到问题
很抱歉标题含糊不清,我不太清楚如何解释发生了什么。我试图创建一个链表,链表的每个条目都包含两个字符串,一个整数数组可以容纳四个条目,一个浮点数组可以容纳四个条目。 下面是头文件中主结构的初始化-C 创建链接列表时遇到问题,c,arrays,linked-list,C,Arrays,Linked List,很抱歉标题含糊不清,我不太清楚如何解释发生了什么。我试图创建一个链表,链表的每个条目都包含两个字符串,一个整数数组可以容纳四个条目,一个浮点数组可以容纳四个条目。 下面是头文件中主结构的初始化- #ifndef FUNCTIONS_H #define FUNCTIONS_H #define MAX_LENGTH 20 struct stock { char ticker[MAX_LENGTH]; char comp[MAX_LENGTH]; int shares[4]; float pri
#ifndef FUNCTIONS_H
#define FUNCTIONS_H
#define MAX_LENGTH 20
struct stock {
char ticker[MAX_LENGTH];
char comp[MAX_LENGTH];
int shares[4];
float price[4];
struct stock *next;
};
#endif
这是我的主文件中的代码-
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "functions.h"
void main(void) {
int choice,shr[4], i;
char tic[MAX_LENGTH];
char nam[MAX_LENGTH];
float pri[4];
struct stock *head = NULL;// entry point for linked list
struct stock *current; //pointer currently being used
printf("Press 1 to enter a new stock\n");
printf("Press 2 to find the LIFO and FIFO dollar cost average for the number of shares sold\n");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("Enter the stock ticker:\n");
scanf("%s", &tic);
printf("Enter the name of the stock:\n");
scanf("%s", &nam);
for(i = 1; i<=4; i++) {
printf("Enter the number of shares:\n");
scanf("%d",&shr[i] );
printf("Enter the price of the stock:\n");
scanf("%f", &pri[i]);
}
if(head == NULL) { //check to see if first element has been filled
head = (struct stock *)malloc(sizeof(struct stock));
current = head;
}
else { //if the first element is full, move on to the next entry
current->next = (struct stock *)malloc(sizeof(struct stock));
current = current->next;
}
strcpy(current->ticker, tic);
strcpy(current->comp, nam);
memcpy(current->shares, shr, sizeof(current->shares));
memcpy(current->price, pri, sizeof(current->price));
current->next = NULL;
}
printf("%s\n", current->ticker);
printf("%s\n", current->comp);
for(i = 1; i <= 4; i++) {
printf("%d\n", current->shares[i]);
printf("%f\n", current->price[i]);
}
}
#包括
#包括
#包括
#包括“functions.h”
真空总管(真空){
int选择,shr[4],i;
字符tic[最大长度];
char nam[最大长度];
浮动优先级[4];
struct stock*head=NULL;//链表的入口点
struct stock*current;//当前正在使用的指针
printf(“按1键输入新股票\n”);
printf(“按2查找已售出股份数量的后进先出和先进先出美元成本平均值”);
scanf(“%d”,选择(&C);
开关(选择)
{
案例1:
printf(“输入股票代码:\n”);
扫描频率(“%s”和“tic”);
printf(“输入股票名称:\n”);
scanf(“%s”&nam);
对于(i=1;inxt=(结构股票*)malloc(sizeof(结构股票));
当前=当前->下一步;
}
strcpy(当前->股票代码,tic);
strcpy(当前->公司,不结盟运动);
memcpy(当前->股份,shr,大小(当前->股份));
memcpy(当前->价格,pri,sizeof(当前->价格));
当前->下一步=空;
}
printf(“%s\n”,当前->报价器);
printf(“%s\n”,当前->复合);
对于(i=1;i股[i]);
printf(“%f\n”,当前->价格[i]);
}
}
该计划的最终目标是有两个单独的股票条目,并能够根据每种股票的四种不同股票购买情况计算FIFO/LIFO美元平均成本。但目前,我只想能够将信息正确地输入到链接列表中
在循环向用户四次询问股票数量和价格之后,以前要求的字符串“nam”似乎消失了,因为如果我尝试访问它或稍后打印它,它将不会打印任何内容
我正在尝试使用memcpy函数将输入的数组复制到链表中的数组中。每当我在复制到链表后尝试打印数组时,它都无法正确打印。股票和价格数组的前三个条目打印正确,但第四个股票条目打印出一个巨大的数字,第四个float条目打印为零
谢谢您的帮助。
tic
和nam
已经是指针,所以请更改
scanf("%s", &tic);
scanf("%s", &nam);
到
另外,与您的问题没有直接关系,但是
head = (struct stock *)malloc(sizeof(struct stock));
current->next = (struct stock *)malloc(sizeof(struct stock));
最好写成
head = malloc(sizeof(*head));
current->next = malloc(sizeof(*(current->next)));
无需从malloc()
中强制转换返回值,并且通过使用sizeof(*head)
可以保证为sizeof
scanf(“%s”,&tic);…scanf(“%s”,&nam)
您不需要这里的&
。
head = malloc(sizeof(*head));
current->next = malloc(sizeof(*(current->next)));