LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2011, 10:19 PM   #1
dman777
Member
 
Registered: Dec 2010
Distribution: Gentoo
Posts: 232

Rep: Reputation: 8
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);
}
 
Old 08-21-2011, 11:26 PM   #2
theNbomr
LQ 5k Club
 
Registered: Aug 2005
Distribution: OpenSuse, Fedora, Redhat, Debian
Posts: 5,399
Blog Entries: 2

Rep: Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908Reputation: 908
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 );

}

--- rod.

Last edited by theNbomr; 08-21-2011 at 11:27 PM.
 
1 members found this post helpful.
Old 08-21-2011, 11:54 PM   #3
dman777
Member
 
Registered: Dec 2010
Distribution: Gentoo
Posts: 232

Original Poster
Rep: Reputation: 8
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.

Quote:
read_line(inventory[num_parts].name, NAME_LEN);
 
Old 08-22-2011, 01:47 AM   #4
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

Quote:
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").

'Hope that helps .. PSM

Last edited by paulsm4; 08-22-2011 at 01:48 AM.
 
Old 08-22-2011, 07:13 AM   #5
johnsfine
LQ Guru
 
Registered: Dec 2007
Distribution: Centos
Posts: 5,286

Rep: Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197Reputation: 1197
Quote:
Originally Posted by dman777 View Post
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.
 
Old 08-22-2011, 06:16 PM   #6
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
dman777 - does this help?
Code:
#include <stdio.h>

#define MAX_PARTS 2
#define NAME_LEN  8

struct part {
  int number;
  char name[NAME_LEN+1];
  int on_hand;
}
  inventory [MAX_PARTS] =  {
  { 1, "ABC", 10 },
  { 2, "DEF", 10 }
};

void
dump (char * msg, struct part * record)
{
  printf ("%s:\n",
    msg);
  printf ("number: %d, name: %s, on_hand: %d:\n\n",
    record->number, record->name, record->on_hand);
}

int
main (int argc, char *argv[])
{
  dump ("example 1", &inventory[0]);
  dump ("example 2", &inventory[1]);
  dump ("example 3", inventory);
  return 0;
}
Quote:
example 1:
number: 1, name: ABC, on_hand: 10:

example 2:
number: 2, name: DEF, on_hand: 10:

example 3:
number: 1, name: ABC, on_hand: 10:
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Passing array to thread function zak100 Programming 4 08-12-2010 11:07 AM
Bash array Add function example using indirect array reference as function argument bobywelsh Programming 10 07-05-2010 04:44 AM
ARGGGH! Passing array of structs to function in C. CoderMan Programming 5 02-05-2009 10:44 PM
segfault when passing array[] as argument froboozle Programming 11 06-28-2005 03:06 PM
passing function pointer as argument worldmagic Programming 7 08-04-2004 03:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:37 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration