Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

Programming with Data Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Programming with Data Structures

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 16

Structures Data Types

Passing arrays as parameters


// Program to calculate the sum of array elements by passing to a
function

#include <stdio.h>

float calculateSum(float num[]) {


float sum = 0.0;
for (int i = 0; i < 6; ++i) {
sum += num[i];
}
return sum;
}
Passing arrays as parameters
int main()
{
float result, num[] = {23.4, 55, 22.6, 3, 40.5, 18};

// num array is passed to calculateSum()

result = calculateSum(num);
printf("Result = %.2f", result);
return 0;
}
Pointers
Declaration of a pointer

data_type *identifier;
Example: int *x;

Note 1: Once an ordinary variable is declared memory is allocated.


However, memory will not be allocated for pointers. Before storing data
memory needs to be allocated first.

Note 2: As it is an advanced programming it is not discussed in this course.


Return an array from function
C programming does not allow to return an entire array as
an argument to a function. However, you can return a
pointer to an array by specifying the array's name without
an index.

If you want to return a single-dimension array from a


function, you would have to declare a function returning a
pointer as in the following example −
int * myFunction() {
.
}
Second point to remember is that C does not advocate to
return the address of a local variable to outside of the
Return an array from function
Now, consider the following function which will generate array of
10 numbers and return them using an array and call this function
as follows −

#include <stdio.h>
/* function to generate and return numbers */

int * getArray( ) {
static int r[10];
int i;
/* populate the array */
for ( i = 0; i < 10; ++i) {
printf(“Enter an integer”);
scanf(“%d”, &r[i]); }
return r;
Return array from function in C
/* main function to call above defined function */
int main () {

/* a pointer to an int */
int *p;
int i;

p = getArray();

for ( i = 0; i < 10; i++ ) {


printf( "*(p + %d) : %d\n", i, *(p + i));
}
Structures
Structure is a collection of homogeneous or heterogeneous data elements which is
managed as a single unit.
Defining a structure variable

struct identifier {
Data_type identifier1;
Data_type identifier2;
………
Data_type identifiern;
}identifiers;
Variables declared within the structure are referred as structure fields.
Identifiers are specified at the end to declare structure variables
Structures
// Structure example

struct students {
char Name[30], degree[3], regNo[10];
int age;
double tution_fee;
}std;

Declaring structure variables separately


struct students std;
Accessing Structure Members
To access any member of a structure, we use the member access operator (.). The
member access operator is coded as a period between the structure variable name
and the structure member that we wish to access. You would use the keyword struct
to define variables of structure type. The following example shows how to use a
structure in a program −
#include <stdio.h>
#include <string.h>
struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
Accessing Structure Members
int main( ) {
struct Books Book1; /* Declare Book1 of type Book */
struct Books Book2; /* Declare Book2 of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* book 2 specification */
strcpy( Book2.title, "Telecom Billing");
strcpy( Book2.author, "Zara Ali");
strcpy( Book2.subject, "Telecom Billing Tutorial");
Book2.book_id = 6495700;
Accessing Structure Members
/* print Book1 info */
printf( "Book 1 title : %s\n", Book1.title);
printf( "Book 1 author : %s\n", Book1.author);
printf( "Book 1 subject : %s\n", Book1.subject);
printf( "Book 1 book_id : %d\n", Book1.book_id);

/* print Book2 info */


printf( "Book 2 title : %s\n", Book2.title);
printf( "Book 2 author : %s\n", Book2.author);
printf( "Book 2 subject : %s\n", Book2.subject);
printf( "Book 2 book_id : %d\n", Book2.book_id);
return 0;
}
Pointers to Structures
You can define pointers to structures in the same way as you define pointer to any
other variable −

struct Books *struct_pointer;


Now, you can store the address of a structure variable in the above defined pointer
variable. To find the address of a structure variable, place the '&'; operator before the
structure's name as follows −

struct_pointer = &Book1;
To access the members of a structure using a pointer to that structure, you must use
the → operator as follows −
struct_pointer->title;
Pointers to Structures
Let us re-write the above example using structure pointer.
#include <stdio.h>
#include <string.h>

struct Books {
char title[50];
char author[50];
char subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book );
Pointers to Structures
Let us re-write the above example using structure pointer.
#include <stdio.h>
#include <string.h>
struct Books {
char title[50], author[50], subject[100];
int book_id;
};
/* function declaration */
void printBook( struct Books *book ) {
printf( "Book title : %s\n", book->title);
printf( "Book author : %s\n", book->author);
printf( "Book subject : %s\n", book->subject);
printf( "Book book_id : %d\n", book->book_id);
Pointers to Structures
/* function declaration */
void printBook( struct Books *book );
int main( ) {
struct Books Book1; /* Declare variables of type Book */
/* book 1 specification */
strcpy( Book1.title, "C Programming");
strcpy( Book1.author, "Nuha Ali");
strcpy( Book1.subject, "C Programming Tutorial");
Book1.book_id = 6495407;
/* print Book1 info by passing address of Book1 */
printBook( &Book1 ); // pass by reference
return 0; }

You might also like