我正在嘗試在頭檔案中宣告一個結構。但它繼續顯示 main.c 檔案中的初始值設定項太多。我嘗試使用指標但不這樣做,但它仍然表明我錯了。
這是主檔案:
#define MAX_PETS 7
struct Patient pets[MAX_PETS] = {
{1024, "Shaggy Yanson", {"CELL","3048005191"} }, //too many initializer values for phone no.
{1032, "Puglsey Yanson", {"CELL","3048005191"} },
{1040, "Beans Maulin", {"HOME","3649155831"} },
{1048, "Banjo Codi", {"TBA",{'\0'}} },
{1056, "Rover Davidov", {"WORK","7934346809"} }
};
頭檔案:
struct Phone
{
char description[4];
char phoneNumber[10];
};
struct Patient
{
int no[4];
char name[15];
struct Phone phn;
};
提前致謝。
uj5u.com熱心網友回復:
我做了一些小修復,它消除了一些問題
#include <stdlib.h>
#include <stdio.h>
#define MAX_PETS 7
struct Phone
{
char description[5];
char phoneNumber[11];
};
struct Patient
{
int no;
char name[15];
struct Phone phn;
};
struct Patient pets[MAX_PETS] = {
{1024, "Shaggy Yanson", {"CELL","3048005191"} }, //too many initializer values for phone no.
{1032, "Puglsey Yanson", {"CELL","3048005191"} },
{1040, "Beans Maulin", {"HOME","3649155831"} },
{1048, "Banjo Codi", {"TBA",{'\0'}} },
{1056, "Rover Davidov", {"WORK","7934346809"} }
};
int main(int argc, char **argv){
pets;
printf("%s", pets->name); //this shouldn't be done in a professional context as it can confuse but results in printing the first name in the pets array
return 0;
}
代碼沒有運行的原因
正如評論所指出的,int no應該是一個常規的 int,因為它意味著一個數字(例如 1024)而不是 4 個有符號整數的集合({1024、-1023、1032、288763})
因此,我認為您對指標、陣列和字符陣列感到困惑,所以這里有一個非常好的解釋,我希望能解釋一下
陣列
陣列是您定義的某種資料型別的串列/集合(無論是字符、短整數、整數、結構等),如果您執行 int varName[4],您將背靠背存盤 4 個整數,沒有記憶中的差距。盡管 varName 不是指標,但實際上您可以輕松地將其“轉換”為指標,只需弄亂 varName 而無需指定陣列中的位置(基本上 varName 將回傳指向 varName[0] 的指標,而 varName 1 回傳指標到位置 1 等等)當你了解更多 C 或程式集的進出時,你會意識到為什么這是但它超出了問題的范圍
指標
A pointer is just a variable that stores a virtual address of another variable. Basically when you return the value it will tell you where is the value that its pointing is located
Char
A char is just a byte(so it goes from 127 to -128) long integer Number (while an int tipically has 4 bytes to represent it), its primary use is to represent characters for example \0 will be a string terminator which will result in the char having the number 0 assigned to it but if you tell it to hold 65 as a value and print it as a character, it will say its an A this just depends how do you want the variable to be represented
Char arrays and strings
A char array is a bunch of chars back to back that typically are used to represent a string of text and the way the compiler/computer knows the end is putting a string terminator on the end of it this is a \0 or a char with the value of 0 , this is why you must be careful when allocating space to char array or pointers that are supposed to be used as strings as you must allow an extra space for a terminator
You don't always have to terminate the string because depending on the context the compiler will do it for you, for example when i print pets->name the reason why it correctly stops where its supposed to is because the compiler sticks a terminator on the end of "Shaggy Yanson"
However the compiler is very limited in its ability to understand the context, so when you said the description was a array with 4 positions and filled them to the end(assuming phoneNumber array is now a null terminated string therefore having a dimension of 11), since arrays are stored back to back doing:
printf("%s", pets[0].phn.description); // prints the description of the phone inside the first pet
will result in CELL3048005191 which is not what we wanted, as the first terminator is on phone so always give an extra character above the maximum expected number of characters in a array so that you or the compiler can place that final string terminator at the end
unless you don't wanna use it as a string
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455497.html
上一篇:從結構陣列中洗掉專案
