Cse1301 computer programming lecture 23 structures part 2
Download
1 / 23

lect23NoNotes - PowerPoint PPT Presentation


  • 233 Views
  • Uploaded on

CSE1301 Computer Programming Lecture 23 Structures (Part 2) Topics Structs and functions Arrays of structures Passing structs as Parameters Like any other variable, you can pass a struct as a parameter to a function Passing by value

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'lect23NoNotes' - Audrey


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Cse1301 computer programming lecture 23 structures part 2 l.jpg

CSE1301Computer ProgrammingLecture 23Structures (Part 2)


Topics l.jpg
Topics

  • Structs and functions

  • Arrays of structures


Passing structs as parameters l.jpg
Passing structs as Parameters

  • Like any other variable, you can pass a struct as a parameter to a function

  • Passing by value

    • Pass the struct – use the values of the members, but don't change them

  • Passing by reference

    • Pass a pointer to the struct – you can change the values of the members


Passing a struct by value l.jpg
Passing a struct by Value

  • As always, the formal parameters are copies of the actual parameters

void printRecord (Student item)

{

printf("Last name: %s\n",item.lastname);

printf(" Mark: %.1f\n\n",item.mark);

}

main()

{

Student studentA = {“Gauss”, 99.0};

printRecord(studentA);

}


Function returning a struct l.jpg
Function Returning a struct

StudentreadRecord ( void )

{

Student newStudent;

printf("Enter last name and mark: ");

scanf("%s %f",newStudent.lastname,&(newStudent.mark));

return newStudent;

}

  • A “package” containing several values

main()

{

Student studentA;

studentA =readRecord();

}


Passing structs by reference l.jpg
Passing structs by Reference

  • Pass a pointer to a struct

  • Change the value of some or all of the members

    • Changes are visible in the calling function as well as the called function


Passing structs by reference cont l.jpg
Passing structs by Reference (cont)

void readStudent (Student* sptr)

{

printf(“Please enter name and ID\n”);

scanf(“%s”, (*sptr).name ); /*parens needed*/

scanf(“%ld”, &((*sptr).id) );

}

int main()

{

Student studentA;

readStudent(&studentA);

}


Passing structs by reference cont8 l.jpg
Passing structs by Reference (cont)

  • The parentheses around the (*sptr) are needed because of precedence

    • e.g., (*sptr).item

  • If you need the address of a member, you need another set of parentheses

    • e.g., &((*sptr).item)


Arrays of structs l.jpg
Arrays of structs

  • Each element of the array is a whole struct, which includes all the members of that struct

  • To access a single value, you need to know which element of the array you're dealing with and which member of the struct, e.g.,

    studentList[0].name

    studentList[i].id


Array of structs l.jpg

id:123456789

0

name: "fred"

id:123456788

1

name: "ralph"

id: 123456787

2

name: "fong"

id: 123456786

3

name: "rachel"

Array of structs

studentList


Array of structs11 l.jpg

id:123456789

0

name: "fred"

id:123456788

1

name: "ralph"

id: 123456787

2

name: "fong"

id: 123456786

3

name: "rachel"

Array of structs

studentList[0]gives you the whole struct

studentList


Array of structs12 l.jpg

id:123456789

0

name: "fred"

id:123456788

1

name: "ralph"

id: 123456787

2

name: "fong"

id: 123456786

3

name: "rachel"

Array of structs

studentList

studentList[3].namegives you the struct member


Arrays of structs13 l.jpg
Arrays of structs

#define MAXCLASS 30

typedef struct {

long int id;

char name[20];

} Student;

...

Student sem1Class[MAXCLASS];


Arrays of structs14 l.jpg
Arrays of structs

Student sem1Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem1Class[i].name);

printf("enter id\n");

scanf("%d",&(sem1Class[i].id));

}

name of array


Arrays of structs15 l.jpg
Arrays of structs

Student sem1Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem1Class[i].name);

printf("enter id\n");

scanf("%d",&(sem1Class[i].id));

}

index into array


Arrays of structs16 l.jpg
Arrays of structs

Student sem1Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem1Class[i].name);

printf("enter id\n");

scanf("%d",&(sem1Class[i].id));

}

the structure at this position in the array


Arrays of structs17 l.jpg
Arrays of structs

Student sem1Class[MAXCLASS];

int i;

for (i=0;i<MAXCLASS;i++)

{

printf("enter name\n");

scanf("%s",sem1Class[i].name);

printf("enter id\n");

scanf("%d",&(sem1Class[i].id));

}

name of the member of the structure at that position in the array


Example array of structs and pass by value 1 l.jpg

#include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec {

char lastname[MAXLEN];

float mark;

};

typedef struct StudentRec Student;

Student readRecord ( void )

{

Student newStudent;

printf("Enter last name and mark: ");

scanf("%s %f", newStudent.lastname, &(newStudent.mark));

return newStudent;

}

void printRecord ( Student item )

{

printf("Last name: %s\n", item.lastname);

printf(" Mark: %.1f\n\n", item.mark);

return;

}

Example: array of structs and pass-by-value-1


Slide19 l.jpg

Example: array of structs and pass-by-value-2

int main()

{

int count = 0;

Student class[MAXN];

int i;

printf("How many students? ");

scanf("%d", &count);

if (count > MAXN)

{

printf("Not enough space.\n");

exit(1);

}

for (i=0; i < count; i++)

{

class[i] =readRecord();

}

printf("\nClass list:\n\n");

for (i=0; i < count; i++)

{

printRecord(class[i]);

}

return 0;

}


Using arrays and pointers l.jpg
Using Arrays and Pointers

typedef struct {

int age;

char name[20];

} Child;

Child s[20];

  • Some struct: s[13]

  • An item in some struct: s[4].age

  • A character in some name: s[7].name[3]

  • Address of some struct: &(s[2])

  • An item in some struct pointed to: (*sptr).age

  • Address of above: &((*sptr).age)

    This is an address within a struct


Example array of structs and pass by reference 1 l.jpg
Example: array of structs and pass-by-reference-1

#include <stdio.h>

#include <stdlib.h>

#define MAXLEN 50

#define MAXN 20

struct StudentRec {

char lastname[MAXLEN];

float mark;

};

typedef struct StudentRec Student;

void readStudent ( Student* sptr )

{

printf("Please enter name and mark\n");

scanf("%s", (*sptr).lastname ); /*parens needed*/

scanf("%f", &((*sptr).mark) );

return;

}

void printRecord ( Student item )

{

printf("Last name: %s\n", item.lastname);

printf(" Mark: %.1f\n\n", item.mark);

return;

}


Example array of structs and pass by reference 2 l.jpg
Example: array of structs and pass-by-reference-2

int main()

{

int count = 0;

Student class[MAXN];

int i;

printf("How many students? ");

scanf("%d", &count);

if (count > MAXN)

{

printf("Not enough space.\n");

exit(1);

}

for (i=0; i < count; i++)

{

readStudent(&class[i]);

}

printf("\nClass list:\n\n");

for (i=0; i < count; i++)

{

printRecord(class[i]);

}

return 0;

}


Slide23 l.jpg

Reading

  • King, 16.1 – 16.3

  • Deitel & Deitel,10.1 – 10.7

  • Kernighan & Ritchie, 6.1, 6.2, 6.7


ad