0% found this document useful (0 votes)
2 views

Lesson 4-Structure Part2

The document presents a computer science course focused on structures and pointers in C programming. It includes examples of creating and accessing structures, modifying values, reading user input, and handling strings and arrays within structures. The content is structured to guide students through practical coding exercises related to the topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lesson 4-Structure Part2

The document presents a computer science course focused on structures and pointers in C programming. It includes examples of creating and accessing structures, modifying values, reading user input, and handling strings and arrays within structures. The content is structured to guide students through practical coding exercises related to the topic.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Higher School of Applied Sciences of Tlemcen

1-year computer science course

The structures
Part 2

Presented by:
Dr. Imane NEDJAR
Part 2
Structures and Pointers

2
Structures Pointers
Creating a Structure

struct Student{ Typedef struct Student{


int age ; int age ;
float score; float score;
}; } Student;
int main() { int main() {
struct Student e,*p; Student e,*p;
return 0;} return 0;}
3
Structures Pointers
Accessing the Structure's Elements
#include<stdio.h>
typedef struct Student{
int age ;
float score;} Student; The age is 23.00
int main() { The score is 15.50
Student e,*p;
p=&e;
p->age=23;
p->score=15,5;
printf("The age is %d\n",p->age);
printf("The score is %.2f\n",p->score);
return 0;} 4
Structures Pointers
Accessing the Structure's Elements
#include<stdio.h>
struct Student{
int age ; The age is 23.00
float score ;}; The score is 15.50
int main() {
struct Student e={23,15.5},*p;
p=&e;
printf("The age is %d\n",p->age);
printf("The score is %.2f\n",p->score);
return 0;} 5
Structures Pointers
Modifying the Values
#include<stdio.h>
struct Student{
int age ;
float score ;}; The age is 19.00
int main() { The score is 8.50
struct Student e={23,15.5},*p;
p=&e;
p->age=19;
p->note=8.5;
printf("The age is %d\n",e.age);
printf("The score is %.2f\n",e.score);
return 0;} 6
Structures Pointers
Reading a Structure
#include<stdio.h>
struct Student{
int age ; Provide the age
float score ;}; 12
int main() { Provide the score
struct Student e, *p; 6
p=&e; The age is 12.00
printf("Provide the age \n"); The score is 6.00
scanf ("%d",&p->age);
printf("Provide the score \n");
scanf("%f", &p-> score);
printf("The age is %d \n",e.age);
printf("The score is %.2f \n",e.score);
return 0;} 7
Structures Pointers
String

struct Student{
char name[20] ;
char first_name[20] ;
};

8
Structures Pointers & String
Accessing the Structure's Elements
#include<stdio.h>
#include<string.h>
struct Student{
char name[20] ; Name isYoubi
char first_name[20] ;}; First name is Mohamed
int main() {
struct Student e,*p;
p=&e;
strcpy (p->name, "Youbi" );
strcpy (p-> first_name, "Mohamed" );
printf("Name is %s \n", p-> name);
printf("First name first name is%s \n", p-> first_name);
return 0;} 9
Structures Pointers & String
Reading a Structure
#include<stdio.h>
#include<string.h>
struct Student{
char name[20] ;
char first_name[20] ;}; Name is Youbi
int main() { First name is Mohamed
struct Student e,*p;
p=&e;
printf("Provide the name\n");
gets(p->name);
printf("Provide the first name\n");
gets(p->first_name);
printf("Name is %s \n",p->name);
printf("First name is %s \n", p->first_name);
return 0;} 10
Structures Pointers
Arrays

struct Student{
char name[20] ;
float score[3]; };

11
Structures Pointers & Array
Accessing the Structure's Elements
#include<stdio.h>
#include<string.h>
struct Student{
char name[20] ;
float score[3]; };
int main() { int i;
Name is Youbi
struct Student e,*p; Score1=10.00
p=&e; Score 2=15.00
strcpy (p->name, "Youbi" );
p->score[0]=10;
Score 3=0.00
p-> score[1]=15;
p-> score[3]=8;
printf("Name is %s\n", p->name);
for(i=0;i<3;i++)
printf("Score %d=%d \n",i+1, p-> score[i]);
12
return 0;}
Structures Pointers & Array
#include<stdio.h> Reading a Structure
#include<string.h>
struct Student{ Provide the name
char name[20] ; Youbi
float score[3]; };
int main() { int i;
Provide the scores
struct Student e,*p; 10
p=&e; 2
printf("Provide the name\n"); 15
gets(p->name); Name is Youbi
printf("Provide the scores\n"); Score 1=10.00
for(i=0;i<3;i++)
scanf("%f",&p->score [i]);
Score 2=2.00
printf("Name is %s\n", p->name); Score 3=15.00
for(i=0;i<3;i++)
printf("Score %d=%.2f \n",i+1, p-> score[i]);
13
return 0;}

You might also like