I am trying to declare a struct in the header file. But it keeps on showing that too many initializer values in the main.c file. I tried using a pointer and not doing so, but it still shows that I am wrong.
Here is the main file:
#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"} }
};
Header file:
struct Phone
{
char description[4];
char phoneNumber[10];
};
struct Patient
{
int no[4];
char name[15];
struct Phone phn;
};
Thanks in advance.
CodePudding user response:
I did some minor fixes and it removed some issues
#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;
}
The reason why the code wasn't running
As the comments pointed out, int no
should be a regular int as its meant to be a single number(1024 for example) instead of a collection of 4 signed Integer numbers ({1024, -1023, 1032, 288763})
As such i think you are confused about pointers, arrays and char arrays, so heres a very explanation that i hope explains it
Arrays
A array is a list/collection of a certain data type you define(be it chars, short, ints, structs, etc...) , if you do int varName[4] you will store 4 Integer Numbers back to back with no gap in memory. Although varName is not a pointer, in reality you can easily "convert" it into a pointer by just messing with varName without specifying a position in the array (basically varName will return a pointer to varName[0] while varName 1 returns a pointer to position 1 and so on) as you learn more of the in's and out's of C or assembly you will realize why this is but its out of scope of the question
Pointers
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(although phoneNumber is still 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