C passing a Array of Structuers as an argument in a function
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
C passing a Array of Structuers as an argument in a function
When a function is called and it's arguement passed is an array of structers, is the actual name going to be a pointer to the first structer of the array? In otherwards, does it act just like when you pass an array name(array a) and automatically the array name is a like &a[0]?
A sparse example from my book:
#include "readline.h"
struct part {
int number;
char name[NAME_LEN+1];
int on_hand;
} inventory [MAX_PARTS];
main {
read_line(inventory[num_parts].name, NAME_LEN);
}
Your example passes one member of a structure that is one element of the array. References to arrays are the same whether they are arrays of scalars or structures. If you want to pass an array of structures (you can't really; you can only pass a pointer to an array of structures), you use the name of the array as the argument.
Code:
read_line(inventory);
/* The code in readline might look something like:
*/
readline( * struct part parts ){
/* 'i' indexes the array. Must be 0 .. MAX_PARTS-1 */
printf( "Part number %d is %s\n", parts[ i ].num, parts[ i ].name );
}
so if it one member of a structure that is one element of the array being passed, what is it a pointer to exactly? and i'm thrown off, because it is not using & operand and just the name of the array it would normally be array[0]- but since this is a member of a structure that is one element of the array being passed it can't be element 0.
Q: When a function is called and it's argument passed is an array of structures, is the actual name going to be a pointer to the first [element] of the array?
Yes.
Quote:
Q: In otherwords, does it act ... like &a[0]?
Yes, exactly.
Quote:
Q: So if it one member of a structure that is one element of the array being passed, what is it a pointer to exactly?
You said it correctly above: the pointer will be to "a[0]".
Remember - in C, the element #'s start at zero (not at "1").
so if it one member of a structure that is one element of the array being passed, what is it a pointer to exactly? and i'm thrown off, because it is not using & operand and just the name of the array it would normally be array[0]- but since this is a member of a structure that is one element of the array being passed it can't be element 0.
Your example inventory[num_parts].name involves two arrays. inventory is an array of part and the name member in one part is an array of char.
I can't tell which of those two arrays you mean in most of your question. Maybe you are also confusing yourself about the two arrays.
inventory[num_parts] specify a specific single element of the inventory array. The name member of that struct names an entire array. As in most (not quite all) uses of the name of an array, it means the address of the first element of the array, so the address of character 0, within the name array of the num_parts part within inventory.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.