Professional Documents
Culture Documents
Objectives
No bounds checking!
Allowed – usually causes no obvious error
array[10] may overwrite b
0x1008 a[2]
0x1004 a[1] int a[3];
0x1000 a[0]
0x1014 i
0x1010 c2
0x100C a[2] Could be optimized by
a[1] making these adjacent,
0x1008
and reducing padding
a[0] (by default, not)
0x1004
0x1000 c1
Array aligned by
size of elements
main( )
{
int avg, sum = 0 ;
int i ;
int marks[30] ;
/* array declaration */
for ( i = 0 ; i <= 29 ; i++ )
{
printf ( "\nEnter marks " ) ;
scanf ( "%d", &marks[i] ) ;
/* store data in array */
}
for ( i = 0 ; i <= 29 ; i++ )
sum = sum + marks[i] ; /* read data from an array*/
avg = sum / 30 ;
printf ( "\nAverage marks = %d", avg ) ;
}
main( )
{
int x ;
x=5;
x = 10 ;
printf ( "\nx = %d", x ) ;
}
int array[10];
What is
sizeof(array[3])? 4
returns the size of
an object in bytes
sizeof(array)? 40
main( ) {
int i = 3, *x ;
float j = 1.5, *y ;
char k = 'c', *z ;
printf ( "\nValue of i = %d", i ) ;
printf ( "\nValue of j = %f", j ) ;
printf ( "\nValue of k = %c", k ) ;
x = &i ;
y = &j ;
z = &k ;
printf ( "\nOriginal address in x = %u", x ) ;
printf ( "\nOriginal address in y = %u", y ) ;
printf ( "\nOriginal address in z = %u", z ) ;
x++ ; y++ ; z++ ;
printf ( "\nNew address in x = %u", x ) ;
printf ( "\nNew address in y = %u", y ) ;
printf ( "\nNew address in z = %u", z ) ;
}
Value of i = 3
Value of j = 1.500000
Value of k = c
Original address in x = 65524
Original address in y = 65520
Original address in z = 65519
New address in x = 65526
New address in y = 65524
New address in z = 65520
main( ) {
int num[ ] = { 24, 34, 12, 44, 56, 17 } ;
int i ;
for ( i = 0 ; i <= 5 ; i++ )
{
printf ( "\nelement no. %d ", i ) ;
printf ( "address = %u", &num[i] ) ;
}
}
address = 65512
element = 24
address = 65514
element = 34
address = 65516
element = 12
address = 65518
element = 44
address = 65520
element = 56
address = 65522
element = 17
Cox / Fagan Arrays and Pointers 16
Passing an Entire Array to a
Function
Demonstration of passing an entire array to a function */
main( )
{
int num[ ] = { 24, 34, 12, 44, 56, 17 } ; dislpay ( &num[0], 6 ) ;
}
display ( int *j, int n )
{
int i ;
for ( i = 0 ; i <= n - 1 ; i++ )
{
printf ( "\nelement = %d", *j ) ;
j++ ; /* increment pointer to point to next element */
}
}
0x1014 matrix[1][2]
0x1010 matrix[1][1]
int matrix[2][3];
0x100C matrix[1][0]
0x1008 matrix[0][2]
matrix[1][0] = 17;
0x1004 matrix[0][1]
0x1000 matrix[0][0]
Recall: no bounds checking
matrix[0][3] = 42;
main( )
{
int s[4][2] = { { 1234, 56 }, { 1212, 33 },
{ 1434, 80 }, { 1312, 78 } } ;
int i ;
for ( i = 0 ; i <= 3 ; i++ )
printf ( "\nAddress of %d th 1-D array = %u",
i, s[i] ) ;
}
main( )
{
int s[4][2] = { { 1234, 56 }, { 1212, 33 },
{ 1434, 80 }, { 1312, 78 } } ;
int i, j ;
for ( i = 0 ; i <= 3 ; i++ )
{ printf ( "\n" ) ;
for ( j = 0 ; j <= 1 ; j++ )
printf ( "%d ", *( *( s + i ) + j ) ) ;
}
}
Arrays and Pointers 24
output
1234 56
1212 33
1434 80
1312 78
1234 56
1212 33
1434 80
1312 78
int
function(int n)
{
int array[n];
…
int *ptr;
Creation
& variable Returns variable’s memory address
Dereference
* pointer Returns contents stored at address
Indirect assignment
* pointer = val Stores value at address
Assignment
pointer = ptr Stores pointer in another variable
int i1;
int i2;
int *ptr1; 0x1014 … 0x1000
int *ptr2;
0x1010 ptr2:
i1 = 1; 0x100C … 0x1000
i2 = 2; 0x1008 ptr1:
0x1004 i2: 2
3
ptr1 = &i1;
ptr2 = ptr1; 0x1000 i1: 3
1
*ptr1 = 3;
i2 = *ptr2;
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;
What happens?
int1 becomes 8
Arrays and Pointers 33
Using Pointers (cont.)
int int1 = 1036; /* some data to point to */
int int2 = 8;
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;
What happens?
p = &a; p = &a;
p += 1; In each, p now points to b p += 1;
(Assuming compiler doesn’t
reorder variables in memory)
void
set_x_and_y(int *x, int *y)
{
*x = 1001;
*y = 1002; a 1001
1
}
b 1002
2
void
f(void)
{ x
int a = 1;
int b = 2; y
set_x_and_y(&a, &b);
}
int
foo(int array[],
unsigned int size)
{
… What does this print? 8
printf(“%d\n”, sizeof(array));
... because array is really
}
a pointer
int
main(void)
{
int a[10], b[5];
… foo(a, 10)… foo(b, 5) … What does this print? 40
printf(“%d\n”, sizeof(a));
}
C, … He l lo , w o r l d !\n terminator
C terminator: ’\0’
“3”
0x1020 argv[4]
“2” These are strings!!
0x1018 argv[3] Not integers!
0x1010 argv[2] “1”
0x1008 argv[1] “hello”
0x1000 argv[0]
“./program”
Recall:
If a is an array, and p = &a[0] then we can reference array
a[0] = *a and elements as a[i], *(p+i), but we can also reference them as
a[i] = *(a + i) p[i] and *(a+i) – that is, a and p are both pointers to the array
And can be dereferenced by * or by [ ]
Multidimensional Arrays
As in Java, C allows multidimensional arrays by using more [ ]
Example: int matrix[5][10 ];
Some differences:
Because functions can be compiled separately, we must denote all but
one dimension of a multiple dimensional array in a function’s
parameter list
• void afunction(int amatrix[ ][10]);
Because arrays are referenced through pointers, there are multiple
ways to declare and access 2+ dimensional arrays
• This will be more relevant when dealing with an array of strings (which is a
2-D array)