This question already has answers here:
What happens if I define a 0-size array in C/C++?
(8 answers)
Closed 5 years ago.
#include <stdio.h>
int main () {
struct Record {
int employeeNumber;
char employeeName;
float salary;
int yearsServiced;
} record[5];
struct record[0] = {46723, "Fattah", 4550.00, 8};
printf("TheEmployee number is %d", record[0].employeeNumber);
}
Why my program cannot run? please help. Thanks for advance.
struct record[0] declares an array of size 0. You intend to initialize the first element of the array and you confuse declaring and indexing:
struct Record myRecord[1] = {46723, "Fattah", 4550.00, 8};
This declares an array of size 1 and initializes the first element with the given values.
Firstly, by struct record[0] you are declaring an array of size 0, which you need to change to struct record[1].
And as far as your doubt regarding casting, you should read Struct initialization of the C/C++ programming language?
Lastly , I don't think it is the reason of your error but why are you trying to assign "Fattah" to a variable of type char.
You declared employeeName as char but you pass in a string. employeeName needs to be a pointer on char.
char *employeeName;
#include <stdio.h>
int main () {
struct Record {
int employeeNumber;
char *employeeName;
float salary;
int yearsServiced;
} record[5];
record[0].employeeNumber = 46723;
record[0].employeeName = "Fattah";
record[0].salary = 4550.00;
record[0].yearsServiced = 8;
printf("TheEmployee number is %d", record[0].employeeNumber);
}
Related
This question already has answers here:
Is there a function to copy an array in C/C++?
(12 answers)
Closed 4 years ago.
How can I add an array to a struct property in C? Here you have an example of what I want to accomplish:
#include <stdio.h>
#include <string.h>
typedef struct {
char age;
short number;
int grades[10];
char name[80];
char address[120];
} Student;
Student s;
s.age = 23;
s.number = 10;
strcpy(s.name, "Jon");
strcpy(s.address, "Doe");
int oldGrades[10] = {1, 8, 2, 2, 4, 9, 9, 8, 4, 1};
s.grades = oldGrades; // Error: expression must be a modifiable lvalue
How can I add the array oldGrades to the property grades of my recently created object?
You can manually copy the array contents using for loop.
for(int i =0;i<10;i++)
s.grades[i] = oldGrades[i];
Or you can use memcpy.
memcpy(s.grades, oldGrades, sizeof(oldGrades))
c doesn't provide facility s.grades = oldGrades to copy array by assignment.
This question already has answers here:
How do I determine the size of my array in C?
(24 answers)
Closed 4 years ago.
I'm trying to write a function in C that takes a struct array (of structs that hold employee information) as an argument and prints each member of each struct. For some reason my code isn't producing any output. Could anyone tell me where I am going wrong? Thanks.
Here it is:
#include <stdio.h>
#include <stdlib.h>
typedef struct Employee
{
int number;
char name[20];
char department[15];
double salary;
}Employee;
void employeePrint(Employee arr[]);
main()
{
Employee e1 = {101,"John Smith\0","Accounting\0",54926.25};
Employee e2 = {102,"Jane Q. Public\0","Retail\0",54926.24};
Employee e3 = {103,"George Washington\0","Tech\0",70417.76};
Employee empArr[3] = {e1,e2,e3};
employeePrint(empArr);
}
void employeePrint(Employee arr[])
{
int i;
for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i)
{
printf("Number: %-4d\n",arr[i].number);
printf("Name: %-6s\n",arr[i].name);
printf("Department: %s\n",arr[i].department);
printf("Salary: $%-2.2lf",arr[i].salary);
printf("----------------");
}
}
The problems are here: void employeePrint(Employee arr[]) and for(i = 0; i < sizeof(arr)/sizeof(arr[0]); ++i). When you pass an array to a function like that, it decomposes to a pointer. Pointers don't store the size of the original array anymore, so sizeof(arr) is giving you the length of a pointer, which is smaller than your struct, so the division rounds to 0. To fix the problem, pass the size as another parameter, or use some sort of sentinel to know when to stop looping.
replace:
void employeePrint(Employee arr[])
with:
void employeePrint(size_t len, Employee arr[len])
then pass the length of the array in the function call, it should produce what you are looking for.
This question already has answers here:
char array not assignable
(4 answers)
Why are arrays not assignable in C/C++? [duplicate]
Closed 5 years ago.
having not worked with C for a while I'm stuck with passing a single struct from an array of structs to a function by reference.
The code I have looks like this:
struct Sensor {
//ROM data
char romCRC[1];
char romSerial[6];
char romFamily[1];
};
const int maxSens = 10;
void Read_ROM(struct Sensor *sens){
char ROM[10];
for (k = 0; k<8; k++){
ROM[k] = read_byte();
sens->romFamily = ROM[0];
}
}
int main(){
struct Sensor Sensors[maxSens];
Read_ROM(&Sensors[0]);
}
What I expect it to do is:
Create an array of 10 structs of type Sensor
Pass the address of the first struct to the function Read_ROM
Set the member romFamily of the struct Sensor[0] to ROM[0]
read_byte is tested and working. It does return 1 char.
When I try to compile I get this error:
#138 expression must be a modifiable lvalue
With 138 being the line number of:
sens->romFamily = ROM[0];
What is wrong here?
Arrays are not assignable in C, although you can set individual elements.
In your case you need sens->romFamily[0] = ROM[0];
But do question why you need a single element array in the first place.
This question already has answers here:
Get list of C structure members
(6 answers)
Closed 7 years ago.
I have two variables called x and y in a structure called mystruct, when I have the program I want to be able to display the variable names with its value like this.
mystruct.x --> 3, mystruct.y --> 5
Is there a way to do this without just putting mystruct.x in the printf like a string in c?
you can't extract the name of the struct in a smart and clean way.
the possibility to call the type of a variable or a struct is called "Reflection" - the application ability to reflect upon itself (either in compile or runtime) and extract data like the type, typename, inner variables etc.
this is not supported at all in C. thinking about it , C doesn't care- a struct is basically a row in the memory (like any other variable in C, actually).
you can however , make a not-so-smart implementation that stores the typename equivilant to the memory address :
struct memory_address_to_type_name{
char type_name [20],
void* memory_address
} name_memory_address_map[50];
char* get_name (void* variable){
int i;
for (i=0;i<50;i++){
if (variable == name_memory_address_map[i].memory_address)){
return name_memory_address_map[i].type_name;
}
}
return "not found";
}
int push_name (void* variable , char* type_name){
int i;
for (i=0;i<50;i++){
if (strcmp(name_memory_address_map[i].type_name,"") == 0){
name_memory_address_map[i].memory_address = variable;
strcpy(name_memory_address_map[i].type_name,type_name);
}
return 1;
}
return 0;
}
}
int main (void){
myStruct x;
push_name (&x,"myStruct");
//other code
printf("%s --> %d",get_name(x),x.my_member);
}
of course , this is not a complete example. you do want to use a linked list instead of a ad-hoc bounded array , do much more protecting against overflows and out of arrays errors , etc. this is only the idea.
as a side note (and I might get downvoted for this), as a C++ developer, your problem could be much more easily solved in C++ by using a typeid(x).name() (if the implementation does return normal string , like VC++ implementation) , or reproduce the solution above with std::map and std::string . but this is a side note only.
do you mean something like this: http://ideone.com/3UeJHv:
#include <stdio.h>
#define PRINT_INT(X) printf(#X"-->%d\n",X)
#define PRINT_STUDENT(X) printf(#X".x-->%d " #X".y-->%d\n" ,X.x ,X.y)
struct student
{
int x;
int y;
};
int main()
{
int c = 10;
PRINT_INT(c);
struct student stud1 = {200 , 300};
struct student stud2 = {400 , 500};
PRINT_STUDENT(stud1);
PRINT_STUDENT(stud2);
return 0;
}
output:
c-->10 stud1.x-->200 stud1.y-->300 stud2.x-->400 stud2.y-->500
This question already has answers here:
How can I correctly assign a new string value?
(4 answers)
Closed 7 years ago.
Here is my code which I would expect it to compile, but it does not:
#include <stdio.h>
#include <stdlib.h>
typedef struct turtle {
char name[20];
int age;
} turtle;
int main(){
turtle koray = {"koray",25};
turtle halim;
halim.name = "halim"; // This line will cause in compile error.
halim.age = 25;
printf("%s\n",koray.name);
printf("%s\n",halim.name);
}
What am I doing wrong?
This complies successfully, but prints garbage:
*(halim.name) = "halim";
by garbage I mean:
koray
p
You can figure out by reading the error message.
error: incompatible types in assignment of ‘const char [6]’ to ‘char [20]’
"halim" is a const char [6] and name is a char [20], and they cannot be assigned directly.
Use strcpy() instead.
strcpy(halim.name,"halim");
In C array cannot be assigned. (They can be initialised on definition however).
To fill a C-"string" use strcpy():
strcpy(halim.name, "halim");