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.
Related
This question already has answers here:
Can't assign string to pointer inside struct
(1 answer)
Assign string to element in structure in C
(5 answers)
Closed 12 months ago.
I have following structure in C and trying to assign a string into title field of menuButtons structure. But I getting an error:
Error C2440 "=": unable to convert from "const char [6]" to "char *"
struct menuButtons {
int xTopLeft;
int yTopLeft;
int xBottomRight;
int yBottomRight;
char *title;
} easy, medium, hard;
struct menuButtons getEasyInfo() {
easy.xTopLeft = 190;
easy.yTopLeft = 200;
easy.xBottomRight = 450;
easy.yBottomRight = 280;
easy.title = "EASY MODE"; --> error
return easy;
}
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);
}
This question already has answers here:
Passing an array of structs in C
(9 answers)
Closed 6 years ago.
I want to know how I can send my array of structure to a function.
typedef struct {
char fname[20];
char lname[20];
int cnumber[12];
} contact;
contact record[40];
int main()
{
// I have all the data in the record array as I am reading it from the
// file and want to pass the record array to the function PRINT and access it.
print();
}
How can it be send in the function and print all the values using function call?
You can send your array of structures to a function like this:
void print(contact record[], int n) {
Then print the contents in this function and send it back to main() as:
print(record, n);
Note: the length of the array, n, should be kept track of somewhere in your program, then passed to print().
This question already has answers here:
How do function pointers in C work?
(12 answers)
Closed 6 years ago.
I came across the following code:
int H3I_hook(int (*progress_fn)(int*), int *id)
{
...
}
I don't understand the purpose of (int*) at the end of the first argument?
Demystifying:
int (*progress_fn)(int*)
it can be interpreted like below:
int (*progress_fn)(int*)
^ ^ ^
| | |___________ pointer to integer as argument
| |
| pointer to any function that has V and takes ^
|
|__________________________return type an integer
int (*progress_fn)(int*) is function pointer decleration, and (int *) is the list of parameters the function accepts.
So, this:
int (*progress_fn)(int*)
is a pointer to a function that will return an int and will receive one parameter, of type int*.
So you have to understand that progess_fn is the actual parameter. All its relevant components define how the function's prototype is actually.
For more, read How do function pointers in C work?
Given this declarartion:
int progress_callback(int* a);
// ^ this is the (int*) you asked about
You can call H3I_hook like this:
int id = something;
int x = H3I_hook(progress_callback, &id);
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");