You are on page 1of 5

Arrays and Pointers

#include <stdio.h>

int main(void)
{
char str[5] = {'H', 'E', 'L', 'L', 'O'};
char *ptr = &str[0];
printf(“ptr = %08x\n”, ptr);

Lecture 17 printf(“str
return 0;
= %08x\n”, str);

Pointers }
CSE115: Programming Language I

Arrays and Pointers Arrays and Pointers


#include <stdio.h> • The array name is basically the address content

name of a pointer variable which 0x00000000

int main(void)
contains the starting address of the 0x00000001
.
array (address of the first element)
.
{
str 0x180A96e7
char str[5] = {'H', 'E', 'L', 'L', 'O'}; str
0x180A96e8
0x180A96f3
0x180A96e9
char *ptr = &str[0];
0x180A96f0
printf(“ptr = %08x\n”, ptr); 0x180A96f1
‘H’ ‘E’ ‘L’ ‘L’ ‘O’
0x180A96f2
printf(“str = %08x\n”, str);
0x180A96f3 ‘H’
return 0; 0x180A96f4 ‘E’
0x180A96f5 ‘L’
}
0x180A96f6 ‘L’
Output: 0x180A96f7 ‘O’
ptr = 0028ff17
.
str = 0028ff17
.
Arrays and Pointers Arrays and Pointers
• The array name is basically the address content

name of a pointer variable which 0x00000000


str
contains the starting address of the 0x00000001
.
array (address of the first element)
.
str 0x180A96e7
str ‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’
0x180A96e8
0x180A96f3
0x180A96e9
0x180A96f0
0x180A96f1
‘H’ ‘E’ ‘L’ ‘L’ ‘O’
0x180A96f2
0x180A96f3 ‘H’
0x180A96f4 ‘E’ char str[6] = “HELLO";
c = str[2]; is equivalent to
0x180A96f5 ‘L’
c = *(str + 1 × 2); 0x180A96f6 ‘L’
0x180A96f7 ‘O’
Base offset .
.

Arrays and Pointers Arrays and Pointers

str str

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’ ‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’

ptr

char str[6] = "HELLO"; char str[6] = "HELLO";


char *ptr = str; char *ptr = str;
Arrays and Pointers Arrays and Pointers

str str

‘H’ ‘E’ ‘L’ ‘L’ ‘O’ ‘\0’ ‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’

ptr ptr

char str[6] = "HELLO"; char str[6] = "HELLO";


char *ptr = str; char *ptr = str;
ptr[2] = ‘l’; ptr[2] = ‘l’;

Arrays and Pointers Arrays and Pointers

str str

‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’ ‘H’ ‘E’ ‘l’ ‘L’ ‘O’ ‘\0’

ptr ptr

char str[6] = "HELLO"; char str[6] = "HELLO";


char *ptr = str; char *ptr = str;
ptr[2] = ‘l’; ptr[2] = ‘l’;
ptr = ptr + 2; ptr = ptr + 2;
Arrays and Pointers address
0x00000000
content
Arrays and Pointers
0x00000001
.
• int A[3] = {3, 1, 8}; . • int A[3] = {3, 1, 8};
A 0x180A96e7

A 0x180A96e8
0x180A96f3
A
0x180A96e9
0x180A96f0
0x180A96f1
0x180A96f2

3 1 8 0x180A96f3 3 1 8
0x180A96f4
3
0x180A96f5

i = A[2]; is equivalent to 0x180A96f6


0x180A96f7
i = *(A + 4 × 2); 0x180A96f8
1
0x180A96f9
0x180A96fA

Base offset 0x180A96fB


int *ptr = A;
0x180A96fC
8
0x180A96fD
0x180A96fE

Arrays and Pointers Arrays and Pointers


• int A[3] = {3, 1, 8}; • int A[3] = {3, 1, 8};
A A

3 1 8 3 1 8

ptr ptr

int *ptr = A; int *ptr = A;


ptr = ptr + 2;
Arrays and Pointers Passing Arrays as Parameters to Functions
• int A[3] = {3, 1, 8}; • The following two are identical function prototypes!
void init(float A[], int arraySize);
A void init(float *A, int arraySize);
• The “[ ]” in the formal parameter specification indicates
that the variable is an array.
3 1 8
• It is a good practice to pass the size of the array as
another parameter.
ptr • If the function must not change any element of the array
then const should be used in the formal parameter
int *ptr = A; specification of that array.
ptr = ptr + 2;

Passing Arrays as Parameters to Functions Passing Arrays as Parameters to Functions


#include <stdio.h> #include <stdio.h>
double average(int inp_list[], int size) double average(int* inp_list, int size)
{ {
double sum = 0.0; double sum = 0.0;
int i; int i;
for (i=0; i<size; i++) for (i=0; i<size; i++)
sum += inp_list[i]; sum += inp_list[i];
return sum/size; return sum/size;
} }
int main() int main()
{ {
double avg; double avg;
int base[5] = {3, 7, 2, 4, 5}; int base[5] = {3, 7, 2, 4, 5};

avg = average(base, 5); avg = average(base, 5);


printf("Average = %.2lf", avg); printf("Average = %.2lf", avg);
return 0; return 0;
} }

You might also like