Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create lastexercise.c #339

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 67 additions & 0 deletions AY 2022-2023/Student Works/elenazdravkoska/lastexercise.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
/*
structures (records)
functions and parameter passing
by value and by ref (pointers)
*/

#include<stdio.h>

//define our structure
struct agent{
char cRole;
char sName[10];
int dID;
struct agent *a;
}; //we will use local variables later on

//function prototypes
void printAgentRef();
void printAgentVal();


int main(){

//local variables of struct agents
struct agent a1, a2, a3, a[100];

//we want the user to enter values for a1
printf("\n Enter agent name: ");
scanf("%s", a1.sName); //no &
printf("\n Enter the role of %s: ", a1.sName);
scanf(" %c", &a1.cRole);
printf("\n Enter the ID of %s :", a1.sName);
scanf("%d", &a1.dID);

//we want the user to enter values for a2
printf("\n Enter agent name: ");
scanf("%s", a2.sName); //no &
printf("\n Enter the role of %s: ", a2.sName);
scanf(" %c", &a2.cRole);
printf("\n Enter the ID of %s :", a2.sName);
scanf("%d", &a2.dID);

//we are going to use pointers to connect a1 to a2
a1.a = &a2;

//print a1 using the external function
printAgentRef(&a1); //we use & to pass the address of the struct
//printAgentVal(a1); //just duplicate no need for address
printAgentRef(a1.a);

return 0;
}//end main

//function definitions
void printAgentRef( struct agent *a ){
printf("\n Agent name: %s ", a->sName);
printf("\n Agent role: %c ", a->cRole);
printf("\n Agent ID: %d \n", a->dID);
}//endref

void printAgentVal( struct agent a ){
printf("\n Agent name: %s ", a.sName);
printf("\n Agent role: %c ", a.cRole);
printf("\n Agent ID: %d \n", a.dID);


}//endval