C prog: printing values from array of structures

Click For Summary
SUMMARY

The discussion focuses on a C programming function designed to print values from an array of structures, specifically handling names and social security numbers. The function, print_names(struct pers_info arr[]), successfully prints names but requires modification to display only the first initial of the middle name. Participants suggest using printf("%c", arr[0].pers_name.middle[0]); for this purpose. Additionally, there is a need to adapt the function to handle both passing the entire structure array and individual elements, with emphasis on understanding pass-by-value and pass-by-reference concepts in C.

PREREQUISITES
  • Understanding of C programming syntax and structure
  • Familiarity with structures in C, specifically struct pers_info
  • Knowledge of function parameters and return types in C
  • Concepts of pass-by-value and pass-by-reference in C
NEXT STEPS
  • Learn about C structure manipulation and memory management
  • Explore the differences between pass-by-value and pass-by-reference in C
  • Investigate how to implement loops for array processing in C
  • Study string handling in C, including character arrays and formatting output
USEFUL FOR

C programmers, software developers, and students learning about data structures and function parameter passing in C.

  • #31
Your function should look like this:

PHP:
void print_name(struct pers_info record) {
    ...
}

And the code calling it should look like:

PHP:
for (int n=0;n<5;n++)
   print_name(arr[n]);

Passing a struct by value is no different than passing any other kind of variable by value.

- Warren
 
Computer science news on Phys.org
  • #32
print_names(struct pers_info arr[n]);

You don't need to include the type when passing an argument (except for type casts, which is not what's required here).
 
  • #33
chroot said:
Your function should look like this:

PHP:
void print_name(struct pers_info record) {
    ...
}
record? Does that mean one of the individual structures I made? thanks. Record isn't a keyword is it?
 
  • #34
record is a keyword in Pascal (I think), not in C though.

In chroot's example, it's just the name of the argument.
 
  • #35
When you create a function, you can call the variables it receives by any names you'd like. Even if the calling function referred to a hunk of data as arr[n], the called function can choose to call that incoming hunk of data anything it wants. In this case, I chose the word 'record' to refer to the variable being passed into the print_name function. It's not a reserved word in C, and means, well, nothing -- you can call it aStudent if you want, or theThingy, too.

- Warren
 
  • #36
HOLY COW! IT'S WORKING! Here's the function.

void print_names(struct pers_info record)
{
printf("%s -- %s \n", record.pers_name.last, record.ssn);
}
 
  • #37
Thank You! :biggrin: :biggrin: :biggrin:

This the output:

Adams -- 123456123
Burke -- 623001200
Cooper -- 423456999
Dodd -- 327856909
Eggwhite -- 563456929
Press any key to continue
 

Similar threads

  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 29 ·
Replies
29
Views
3K
  • · Replies 18 ·
Replies
18
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 21 ·
Replies
21
Views
3K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 1 ·
Replies
1
Views
2K