|
Previous Page
5.1
Pointer
A Pointer is a variable, which contains the
address of a memory location of another variable. If one
variable contains the address of another variable, the first
variable is said to point to the second.
1000 (Memory Location) 50 (Value) var (Variable
Name)
- Used to return more than one value from a function
- To pass arrays and strings.
- To allocate memory and access it.
type *name;
main()
{
int var1, *var2, temp;
var1=500;
var2= &var1;
temp=*var2;
:
}
5.2
Pointer as function
Argument
Pointers are passed to a function to enable
data items, within the called routine of the program, to
access variables whose scope does not extend beyond the
calling function.
#include<stdio.h>
main()
{
void swap(int *u, int *v);
int x=15, y=20;
printf("x=%d, y=%d\n", x,y);
swap(&x, &y);
printf("\nAfter interchanging x=%d,y=%d\n",x,y);
}
void swap(int *u, int *v)
{
int temp;
temp=*u;
*u=*v;
*v=temp;
return;
}
5.3
Array of Pointers
A multidimensional array can be expressed in
terms of an array of pointers rather than as a pointers to a
group of contiguous arrays.
int *ptr_ary[10];
6.1
Structures
A Structure consists of a number of a data
items, which need not be of the same type, grouped together.
A Structure definition forms a template that is used to
carry structure variables. The variables that make up the
structure are called structure variables.
struct book
{
char bk_name[25];
char author[20];
int edn;
float price;
};
#include<stdio.h>
main()
{
struct
{
char name[20];
int numb;
float purchase_amt;
}customer1;
clrscr();
printf("\nEnter Customer Name:");
gets(customer1.name);
printf("\nEnter customer Number:");
scanf("%d",&customer1.numb);
printf("\nEnter Principal Amount:");
scanf("%f", &customer1.purchase_amt);
intcal(customer1);
}
intcal(struct{char cust_name[20];int cust_numb;float
amt;}bill)
{
float si;
float rate=5.1;
float yrs = 2.2;
si=(((bill.amt)*rate*yrs))/100;
printf("\nThe Customer Name is %s",bill.cust_name);
printf("\nThe Customer number is %d",bill.cust_numb);
printf("\nThe amount is %f",bill.amt);
printf("\n The interest is %f",si);
return;
}
6.2
Array of Structures
One of the most common use of structures is
as array of structures. To declare an array of structures, a
structure is first defined, and then an array variable of
that type is declared.
struct book
lost_books[3];
struct unit
{
char ch;
int i;
};
struct unit series[3]=
{
{'a',100}
{'b',200}
{'c',300}
};
6.3
Unions
A Union is same as structure except that
union allocates higher memory space used by union variable.
A union is a memory location that is shared by two or more
variables, generally of different types.
union temp
{
int a;
char ch;
};
7.1
Text Streams and
Binary Streams
A text stream is a sequence of characters,
which can be organized into lines terminated by a new line
character. The new line character is optional on the last
line and is determined by the implementation.
A binary stream is a sequence of bytes with a one-to-one
correspondence to those in the external device, that is,
there are no character translation.
7.2
Basic File Function
fopen() Opens a
file
fclose() Closes a file
putc() Writes a character to a file
fputc() Writes a character from a file
getc() Reads a character from a file
fgets() Reads a string from a file
fseek() Seeks to specified byte in a file
fprintf() Is to a file what printf() is to the console
fscanf() Is to a file what scanf() is to the console
feof() Returns true if EOF reached
ferror() returns true if an error has occurred
rewind() Resets the file position locator to the beginning
of the file.
remove() Erase a file
fflush() Flushes a file
7.3
File Pointer
(Opening & Closing)
Opening a file
FILE *fp;
fp=fopen("try.c","r");
Closing a file
fclose(FILE *fp);
Previous Page |