C prog: printing values from array of structures

Click For Summary

Discussion Overview

The discussion revolves around a C programming problem related to printing values from an array of structures, specifically focusing on how to handle and display names, including first and middle initials. Participants explore various methods for modifying a print function to accommodate different requirements, including passing structures by value versus by reference.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Debate/contested
  • Homework-related
  • Mathematical reasoning

Main Points Raised

  • One participant shares a function that prints names from an array of structures and seeks help to print only the first initial of the middle name.
  • Another participant suggests using the first character of the middle name string to achieve the desired output.
  • A different approach is proposed to limit the number of characters printed from the middle name string using a format specifier.
  • One participant discusses modifying the print function to handle cases where a middle name is present or absent, indicating uncertainty about the elegance of their approach.
  • Another participant expresses confusion about modifying the function to pass individual structure values instead of the entire array, questioning if this means passing individual elements.
  • There is a suggestion to create a loop to call the print function for each element in the array, raising questions about how to set up the function accordingly.
  • One participant explains the concept of pass-by-value in C, emphasizing that passing a structure by value means passing a copy rather than a reference.
  • Another participant reflects on the implications of passing by value versus passing by reference, noting the potential confusion regarding the teacher's instructions.
  • A participant shares a simpler example to understand passing pointers and requests guidance on how to implement pass-by-value in that context.

Areas of Agreement / Disagreement

Participants express various viewpoints on how to implement the print function and handle structure passing, indicating that there is no consensus on the best approach. Some participants agree on the need to clarify the difference between passing by value and by reference, while others remain uncertain about the specific requirements of the assignment.

Contextual Notes

There are limitations in understanding the assignment's requirements, particularly regarding the distinction between passing the entire array versus individual elements. Participants also express uncertainty about the implications of using pointers and structures in C programming.

  • #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
4K
  • · Replies 18 ·
Replies
18
Views
3K
  • · 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