Pointers |
|
Pointer & Array |
|
Pointers and arrays are very closely linked in C. |
|
Hint: Think of array elements arranged in consecutive memory locations. |
|
Consider the following: |
|
int a[10], x; |
int *pa; |
pa = &a[0]; /* pa pointer to address of a[0] */ |
x = *pa; |
|
/* x = contents of pa (a[0] in this case) */ |
|
 |
|
Warning: There is no bound checking of arrays and pointers so you can easily go beyond array memory and overwrite other things. |
|
C however is much more subtle in its link between arrays and pointers. |
|
For example we can just type: |
|
pa = a; |
instead of |
pa = &a[0] |
and |
a[i] can be written as *(a + i). |
i.e. &a[i] =a + i. |
|
We also express pointer addressing like this: |
|
pa[i] =*(pa + i). |
|
However pointers and arrays are different: |
|
A pointer is a variable. We can do pa = a and pa++. |
|
An Array is not a variable. a = pa and a++ ARE ILLEGAL |
|
This stuff is very important. Make sure you understand it. We will see a lot more of this. We can now understand how arrays are passed to functions. |
|
When an array is passed to a function what is actually passed is its initial element location in memory |
|
So: strlen(s) strlen(&s[0]) |
|
This is why we declare the function: |
|
int strlen(char s[]); |
|
An equivalent declaration is: |
|
int strlen(char *s); |
|
since char s[] is equivalent to char *s. |
|
strlen () is a standard library function that returns the length of a string. |
|
Let's look at how we may write a function: |
|
int strlength(char *s) |
{ |
char *p = s; |
while (*p != '\0'); |
p++; |
return p-s; |
} |
|
Now let’s write a function to copy a string to another string. strcpy () is a standard library function that does this: |
|
void strcopy (char *s, char *t) |
{ while ( (*s++ = *t++) != `\0' );} |
|
This uses pointers and assignment by value. |
|
Note: Uses of Null statements with while. |
|
Malloc Library Function |
|
Function: Allocates main memory |
Syntax: void*malloc(size_t size); |
Prototype in: stdlib.h, alloc.h |
|
Remarks: malloc allocates a block of size bytes from the C heap memory. It allows a program to allocate memory explicitly, as it is needed and in the exact amounts needed. |
|
Calloc Library Function |
|
Function: Allocates main memory |
Syntax: void*calloc(size_t n size); |
Prototype in: stdlib.h, alloc.h |
|
Remarks: Calloc provides access to the C heap memory . Calloc allocates a block of size n items of x size. The block is cleared to 0. |
|
|
|
|
Pointers |
|
Multi Dimensional Arrays & Pointer |
|
We should think of multidimensional arrays in a different way in C: |
|
A 2D array is really a 1D array, each of whose elements is itself an array |
|
|
Hence |
|
a[n][m] notation. |
|
Array elements are stored row by row. |
|
When we pass a 2D array to a function we must specify the number of columns and the number of rows is irrelevant. |
|
The reason for this is pointers again. C needs to know how many columns in order that it can jump from row to row in memory. |
|
Considerint a[5][35] to be passed in a function: |
|
We can do: |
|
f(int a[][35]) {.....} |
|
or even: |
|
f(int (*a)[35]) {.....} |
|
We need parenthesis (*a) since [] have a higher precedence than * |
|
So: |
int (*a)[35]; /*declares a pointer to an array of 35 int */ |
int *a[35]; /*declares an array of 35 pointers to int */ |
|
Now lets look at the (subtle) difference between pointers and arrays. Strings are a common application of this. |
|
Consider: |
|
char *name[10]; |
char Aname[10][20]; |
|
We can legally do name[3][4] and Aname[3][4] in C. |
|
|
|
|
Pointers |
|
Arrays of Pointer |
|
We can have arrays of pointers since pointers are variables. |
|
Example use: Sort lines of text of different length. |
|
Note: Text can't be moved or compared in a single operation. |
|
Arrays of Pointers are a data representation that will cope efficiently and conveniently with variable length text lines. |
|
How can we do this: Store lines end-to-end in one big char array n will delimit lines. |
|
Store pointers in a different array where each pointer points to 1st char of each new line. |
|
Compare two lines using strcmp () standard library function. |
|
If 2 lines are out of order swap pointer in pointer array (not text). |
|
|
|
|
Pointers |
|
Pointer & functions |
|
When an array is passed to a function as an argument , only the address of the first element of the array is passed , but not the actual values of the array elements. |
|
If x is an array, when we call sort(x), the address of x[0] is passed to the function sort(). |
|
The function uses this address for manipulating the array elements. |
|
The address of a variable can be passed as an argument to a function in the normal fashion. |
|
When address is passed to a function , the parameters receiving the address should be pointers. |
|
The process of calling a function using pointer to pass the address of variable is known as call by reference. |
|
The function which is called by reference can change the value of the variable used in the call. |
|
Example: |
|
main() |
{ |
int x; |
x=20; |
change(&x); |
printf("%d\n",x); |
} |
change( int *p) |
{ |
*p=*p+10; |
} |
|
Explanation: When the function change() is called, the address of the variable x, not its value, is passed into the function change(). |
|
Inside change(), the value at which p points is incremented by 10 , and the changed value is then displayed in the main function. |
|
|
|
|
Structures |
|
Introduction |
|
The C language allows us to create custom data types. |
|
The structure is a custom data type which c combines different data types . |
|
The structure is a custom data type which combine different data types to form a new user define data type. |
|
Definition |
|
A structure is a collection of variable reference under one name providing a convincible means of related information together. |
|
Format: struct tag_name |
{ |
data _type member1; |
data_type member2; |
------------------- |
--------------------- |
}; |
|
here a keyboard struct declares a structes to hold the details of field of different data types. |
|
Example: |
struct addr |
{ |
char name [30]; |
char city [15]; |
int pincode ; |
}; |
|
Creating Structure variable |
|
structure can be created in two ways: |
|
1. declaration using tagname anywhere in the program. |
|
Example: |
|
struct book |
{ |
char name [30]; |
char author [25]; |
float price; |
}; |
struct book book1 book2 |
|
2. it is also allowed to combine structure declaration and variable declaration in one statement. |
|
Example: |
|
struct person |
{ |
char *name; |
int age; |
char*address; |
}; |
p1,p2,p3 |
|
while declaring structure variable along with their definition, the use of tag-name is optional. |
|
Struct |
{ |
char *name; |
int age; |
char * address; |
} |
p1,p2,p3 |
|
Image showing how the given value allocate in structure with the help of an example > |
|
 |
|
|
|
|
Structures |
|
Giving values to member |
|
The link between a member and a variable is established using member operator `.' to dot operator. |
|
An example program to define a structure and assign value to members: |
|
 |
|
Out put of the program |
|
 |
|
|
|
|
Structures |
|
Structure Initialization |
|
a structure variable can be initialization as any other data type. |
|
Main() |
{ |
static struct |
{ |
int weight; |
float height; |
} |
} |
student{560,080,75}; |
|
This assign the value 60 to student weight and 180.75 student height. there is a one to one correspondents between the members and their initializing values. |
|
The following statements initialize two structures variables: |
|
Main() |
Struct st_decord |
{ |
int weight; |
float height; |
} |
static struct st_record student2={53, 170,60} |
} |
|
another method is to initlialize a structure variable outside the function. |
|
Struct st_record/* No static word*/ |
{ |
int weight; |
int height |
} |
student={60,50,75} |
} |
main() |
{ |
static struct st_record student2={53,170,60} |
} |
|
|
|
|
Structures |
|
Comparison of structure variables |
|
Two variables of the same structure type can be compared the same way as ordinary variables. |
|
operation meaning |
|
person1=person2*assign perosn2 to person1 |
|
person1== person2*compare all name of person1 and person2 and return1 |
|
|
|
|
|
|
|
|
|
|
|
Structures |
|
Arrays of structures |
|
The most common use of structures is in arrays of structures. To declare an array of structures, first the structure is defined then an array variable of that structure is declared. |
|
E.g.: struct class student [100]; |
|
It defines an array called student which consists of 100 elements of structure named class. |
|
Ans is stored inside the memory in the same way as a multidimensional array example program. |
|
To implements on array of structures. |
|
|
|
|
Structures |
|
Arrays with in structures |
|
Single as multidimensional arrays of type int as float can be defined as structure members. |
Example: |
struct marks |
{ |
int number; |
float subject[3]; |
} |
student [2]; |
|
Here the member subject contains three elements, subject[0], subject[1] and subject[2]there elements can be accessed using appropriate subscript. |
|
For instance, the name student [1] student [2]; would refer to the marks obtained in the third subject by the secured student. |
|
|
Structures |
|
Structures with in structures |
|
Structures within a structure means nesting of structures. |
|
Example: |
|
struct salary |
{ |
char name [20]; |
char department [10]; |
int basic-pay; |
int dearness-allowance; |
int huse_rent_allowance; |
int city_allowance; |
} |
employee; |
|
This structure defines name, department, basic pay and three kinds of allowances. |
|
All the items related to allowance can be grouped together and declared under a sub-stricture. As shown below, strut salary |
|
{ |
char name []2; |
char department [10]; |
struct; |
} |
int dearness; |
int house_rent; |
int city; |
[allowance; |
} |
employee's; |
|
The salary structure contains a member named allowance which use is a structures with. |
|
Three members. Now ; the member compared in the inner structure;, namely, ;dearness,house_rent and city can ;be left to as; |
|
employee. ;allowance. Dearness |
employee. Allowance. House_rent |
employee allowance. city |
|
The inner most member in a nested structure can be accessed by chaining all the concerned structure variables (from outermost to inner most) with the member using dot operator. |
|
|
|
|
Structures |
|
Passing structure to function |
|
There are three methods by which the values of structure can be transferred from one function to another: |
|
1. The first method is to pass each member of the structure as an actual argument of the function call. |
|
The actual argument is then treated independently like ordinary variables. |
|
2. The second methods involve passing of a copy of the entire structure to the called function |
|
Since the function is working on a copy of the entire structure to the called function, changes are not reflected in the original structure (in the calling function). |
|
It is necessary for the entire function to return the entire structure back to the calling function. |
|
3. The third approach employs a concept called pointers to pass the structure as an argument . |
|
In this case, the address location of the structure is passed to the called function. |
|
The function can access indirectly the entire structure and work on it. |
|
The general format of sending a copy of structure to the called function is: |
|
function_name (structure_variable_name) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
No comments:
Post a Comment