-
passing structures??
Hello, I need to know how to pass structures from one function to another as well as the correct syntax for the Prototype and function call as well as the function itself..
Code:
#include <stdio.h>
struct names{
char fname[20];
char lname[20];
struct name *next;
}
/*Prototype*/
void function1(????)
int main () {
struct name *header;
struct name *current;
header->next=NULL;
current=header;
function1(??????);
}
void function1(??????) {
struct name newnode;
newnode=(struct name *)malloc(sizeof(struct name));
newnode->next=header->next;
header->next=newnode;
}
any assistance would be appreciated.
Thanks.
-
Do you have a good reference book or your text to help you. This is not a very difficult topic.
-
Re:
not really, My book doesn't show how to pass them. it shows everything globally defined...
I just need to know how to do the prototype, function call and function header... An example of any kind will do... I would just like to see it so I can implement it on a larger scale.
Thanks.
-
Well here's my crappy attempt of doing this..........
Code:
void AddCard (struct Card *NewCard);
void ViewCardInfo(struct Card *CardInfo);
//Struct To add a new card
struct Card {
char PlayersName[30];
char CardYear[10];
char CardType[15];
char BirthPlace[30];
}PlayersCard;
//something about
AddCard(&PlayersCard); //call function.......
/***********************/
void AddCard(struct Card *PC)
{
//do stuff here
}
-
that was me btw
incognito
Was trying to use my FD screen name I guess.
I point this out so you can know who to flame (me) :).
-
Code:
#include <stdio.h>
struct data_struct
{
int data_a;
int data_b;
};
// Note that you don't pass the struct here, but a pointer to it
void function1 (struct data_struct *data)
{
data->data_a = 1;
data->data_b = 2;
}
// Here you pass a struct
void function2 (struct data_struct data)
{
printf ("%d\n", data.data_a);
printf ("%d\n", data.data_b);
}
int main ()
{
struct data_struct data;
function1 (&data);
function2 (data);
return 0;
}
-
I, and many others, like using typedef for structs.
Code:
#include <stdio.h>
typedef struct{
char name[50];
char surname[50];
//blah blah...
}Data;
int main()
{
Data myData = {"The","Dog"};//Don't need the keyword struct
printf("%s %s", myData.name, myData.surname);
return 0;
}