[英]Trying to figure out why my struct pointer is storing data inaccurately
I am trying to improve my C skills so I apologize if my question is long. 我正在努力提高自己的C语言技能,因此,如果我的问题很长,我深表歉意。 I am having a hard time understanding as to why my struct pointer holds the wrong value in my program, I tried to debug it but I am still relatively new to C and was hoping one of you could tell me what I'm doing wrong here and how I could improve my code and what to focus on.
我很难理解为什么结构指针在程序中保持错误的值,我尝试对其进行调试,但是我对C还是比较陌生,希望你们中的一个可以告诉我我在这里做错了什么以及如何改进我的代码以及重点关注的内容。
I am making a program that stores user data on this struct and then prints it out. 我正在制作一个程序,将用户数据存储在此结构上,然后将其打印出来。
typedef struct table {
char *firstName;
char *lastName;
int id;
}USER;
This function below stores the first name 下面的函数存储名字
void firstName(int *counter, int *check, USER *pt) {
for (int i = *counter; i < *check; i++) {
pt[i].firstName = calloc (MAX_LENGTH, sizeof(pt));
printf("Enter First Name: ");
getchar();
fgets(pt[i].firstName, MAX_LENGTH, stdin);
}
}
This is just my bool function returning true or false 这只是我的布尔函数返回true或false
bool isTrue(char *decision) {
if(*decision == 'Y') {
return true;
}
else {
return false;
}
}
And this is my main 这是我的主要
int main(int argc, char **argv) {
USER *pt = calloc(1, sizeof(pt));
int counter = 0, check = 0;
char decision = '\0';
while (1) {
printf("Would you like to enter a user?(Y/N):");
fgets(&decision, 2, stdin);
strtok(&decision, "\n"); //remove the newline char
if (!isTrue(&decision)) {
break;
}
if (counter != 0) {
pt = realloc(pt, sizeof(pt) * 10); //the 10 is temporary
}
check = counter + 1; // make sure loop only runs once.
firstName(&counter, &check, pt);
++counter; // increment counter;
}
printStruct(pt, &counter);
return 0;
}
When I run it out sometimes it works fine and returns everything and sometimes it skips a value. 当我用完它时,有时它可以正常工作并返回所有内容,有时它会跳过一个值。 This is what I get.
这就是我得到的。 It skips the value at pointer index 1 and prints garbage instead.
它跳过指针索引1处的值,而是打印垃圾。
Would you like to enter a user?(Y/N):N
First name at array 0 is Ermir
First name at array 1 is P#1First name at array 2 is Kevin
First name at array 3 is Blaus
First name at array 4 is Adam
Also I was wondering why is it when I realloc here If i do I get a realloc error when I enter the second name. 我也想知道为什么当我在这里重新分配时,如果输入第二个名字,我得到一个重新分配错误。
if (counter != 0) {
pt = realloc(pt, sizeof(pt) * 10); //realloc(pt, sizeof(pt) * counter + 1) wont work
}
char decision = '\0';
...
fgets(&decision, 2, stdin);
You are only allocating 1 char but are at least reading 2 chars into it. 您仅分配1个字符,但至少要读2个字符。 Fix by allocating a sufficiently sized array for decision.
通过分配足够大小的数组进行决策来解决。
Unrelated but in firstName() pt[i].firstName = calloc (MAX_LENGTH, sizeof(pt));
无关,但在firstName()
pt[i].firstName = calloc (MAX_LENGTH, sizeof(pt));
should be pt[i].firstName = calloc (MAX_LENGTH, 1);
应该是
pt[i].firstName = calloc (MAX_LENGTH, 1);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.