Array of structure passed as function in C [duplicate] - c

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().

Related

the value of a pointer passed in a function doesn't change in the main procedure [duplicate]

This question already has answers here:
Dynamic memory access only works inside function
(1 answer)
Changing address contained by pointer using function
(5 answers)
Closed 14 days ago.
The value pointed by p does change inside the function but in the main procedure, it pintts out the previous value. How can I fix that?
#include <stdio.h>
#include <stdlib.h>
void changer(char *p, char user1[], char user2[]);
int main()
{
char *p=malloc(10*sizeof(char));
char user1[10]="rania";
char user2[10]="souad";
p=user1;
changer(p,user1,user2);
printf("%s\n",p);
return 0;
}
void changer(char *p, char user1[], char user2[])
{
if (p==user1){
p=user2;
}
else
p=user1;
}

Function to print the members of each struct in an array of structs in C [duplicate]

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.

C - Passing single struct from array to function by reference [duplicate]

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.

C programming structure [duplicate]

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);
}

Passing 2-D array to function in c [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Passing multidimensional arrays as function arguments in C
In C,
if I want a function to receive a 2-D array, can I use * notation for the function parameter
int (int my2dary[][10]); //This is what I do not want.
Yes, you pass a pointer to an array of int
int func(int (*my2dary)[10]);
and you call it
int a[5][10];
func(a);
Although, func doesn't know how many elements are in my2dary, so you must give a number too
int func(int n, int (*my2dary)[10]);
and call
int a[5][10];
func(5, a);
See How to interpret complex C/C++ declarations or The ``Clockwise/Spiral Rule''.
If your problem is that you don’t know the size of the array at compile time, you may want:
int func(int *array, int size)
{
int n,m;
...
array[m*size+n]; /* = array[m][n] if in the caller: int array[x][size]; */
}
Optionally (and very probably you need) you can pass a second size argument (x) to be able to test array boundary

Resources