GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 18-Oct-2006, 07:33
nic.nmd nic.nmd is offline
New Member
 
Join Date: Aug 2006
Posts: 12
nic.nmd is on a distinguished road

passing a structure by reference


How do you pass a structure to a function by reference? I have no idea

CPP / C++ / C Code:
#include<stdio.h>

void readData(int*tableArray) /*<-problem!!!*/
{
  FILE*data;
  
  int x=0;
  
  data=fopen("data.txt","r");
  if(data==NULL)
  {
    printf("Failed to open data.txt");
    exit(0);
  }

  for(x=0;x<=19;x++)
  {
    fscanf(data,"\n%s %s %f %d",tableArray[x].site,tableArray[x].date,tableArray[x].reading,tableArray[x].rating);
  }

  fclose(data);
}

int main(void)
{
  struct table
  {
    char site[9];
    char date[11];
    float reading;
    int rating;
  };

  struct table tableArray[20];
  
  readData(&tableArray);
  
  return(0);
}
  #2  
Old 18-Oct-2006, 08:37
rastwi rastwi is offline
New Member
 
Join Date: Oct 2006
Posts: 3
rastwi is on a distinguished road
Smile

Re: passing a structure by reference



following is an example for passing structure with reference:
CPP / C++ / C Code:
struct p
{
	int i;	char c;
};
void func(struct p*);
void main()
{
	struct p myp;
	myp.i=10;	myp.c='a';
	func(&myp);
}
void func(struct p *mp)
{
	printf("%d %c",mp->i,mp->c);
}

In ur code just change the parameters of the function as follows:

void readData(struct table *t) if want to use array of struct. then do that.
Last edited by LuciWiz : 18-Oct-2006 at 08:55. Reason: Please insert your C/C++ code between [cpp] & [/cpp] tags
 
 

Recent GIDBlogToyota - 2008 November Promotion by Nihal

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Passing an object as a reference. What is wrong? _Y_ Java Forum 1 29-Aug-2006 12:58
Difficulty Passing Structure Data Elements Between Functions jdbrine C++ Forum 9 21-May-2006 13:03
G++ question... TreyAU21 C++ Forum 10 24-Feb-2006 09:45
passing pointers to structure to fns 00rish C Programming Language 4 07-Jun-2004 09:32
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 05:02.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.