2. An Array is a data type that uses subscripted variables and
makes it possible to represantations of a large number of
homegeneus values.
Arrays and pointers are closely related concepts. An Array
name by itself is treated as a constant pointer, and
pointers, like arrays, can be subscripted.
Strings are one-dimensional arrays of characters.
2
Arrays, Pointers and Strings
3. Programs often use homogeneous data. For example we want
to store some grades and we should define as follows
int grade0,grade1,grade2,grade3;
We can define one array to hold these values.
int grade[3];
The integer 3 in the declerations represents the number of
elements in the array. The indexing of array elements always
starts at 0.
3
One-dimesional Arrays
4. int a[size];
Lower bound = 0
Upper bound = size – 1
Size upper bound + 1
İt is a good way to define size of and Array as symbolic constant
#define N 100
int a[N];
for(i=0;i<N;i++)
sum +=a[i];
4
One-dimesional Arrays
6. A simple variable in a program is stored in
a certain number of bytes at a particular
memory locaiton, or adress. Pointers are
used in programs to access memory and
manipulate them.
İf v is a variable, then &v is the locaiton or
address in memory of tis stored value.
int *p;
p=0;
p=NULL;
p=&i;
p=(int *)1776; 6
Pointers
İf p is a pointer,
then *p is the
value of the
variable of which
p is the address.
The direct value of
p is a memory
location, whereas
*p is the indirect
value of p
10. An array name by itself is an adress, and pointers can be subscripted.
Pointer variable can take different addresses as values. In contrast, an
array name is an address, or pointer, that is fixed.
Suppose that a is an array, iis an int and p is a pointer.
a[i] is equivalent to *(a+i)
p[i] is equivalent to *(p+i)
#define N 100
int a[N], i,*p,sum=0;
p=a; is equivalent to p=&a[0]
p=a;
for(p=a;p<&a[N];++p) for(i=0;i<N;++i) for(i=0;i<N;++i)
sum +=*p; sum +=*(a+i) sum +=p[i];
10
The relationship between arrays and
Pointers
11. İf the variable p is a pointer to a particular type, then the
expression p+1 yields the correct machine address for storing or
accessing the next variable of that type. And we can use these
expression as same meaning.
11
Pointer Arithmetic and Element Size
12. #include <stdio.h>
#include <stdlib.h>
#define N 10
int sum(int *);
int main(void) {
int a[10] = { 2, 3, 4, 5, 7, 8, 1, 2, 1, 6 };
printf("Sum of array is:%d", sum(a));
return 0;
}
int sum(int a[]) {
int toplam = 0, *p;
for (p = a; p < &a[N]; p++) {
toplam += *p;
}
return toplam;
}
12
Arrays as Function Arguments
13. void swap(int *, int *);
int main(void) {
int a[10] = { 2, 3, 4, 5, 7, 8, 1, 2, 1, 6 };
int i, j;
for (i = 0; i < N; i++)
for (j = N - 1; j > i; --j) {
if (a[j - 1] > a[j])
swap(&a[j - 1], &a[j]);
}
printf("{");
for (i = 0; i < N; i++)
printf("%d %s", a[i], (i < (N - 1)) ? "," : "}");
return 0;
} 13
Example Buble Sort
14. 14
Dynamic Memory Allocation With
calloc() and malloc()
Function prototypes are in the stdlib.h. The calloc stand for
contiguous allocation and malloc stands for memory allocation.
We can uses calloc() and malloc() to dynamically create space
for arrays, structures and unions.