SlideShare a Scribd company logo
Arrays and Pointers in C
Alan L. Cox
alc@rice.edu
Objectives
Be able to use arrays, pointers, and strings in
C programs
Be able to explain the representation of these
data types at the machine level, including
their similarities and differences
Cox Arrays and Pointers 2
Cox Arrays and Pointers 3
Arrays in C
No bounds checking!
Allowed – usually causes no obvious error
array[10] may overwrite b
Unlike Java, array size in declaration
int array[10];
int b;
array[0] = 3;
array[9] = 4;
array[10] = 5;
array[-1] = 6;
Compare: C: int array[10];
Java: int[] array = new int[10];
All elements of same type – homogenous
First element (index 0)
Last element (index size - 1)
Cox Arrays and Pointers 4
Array Representation
Homogeneous  Each element same size – s bytes
 An array of m data values is a sequence of ms bytes
 Indexing: 0th
value at byte s0, 1st
value at byte s1, …
m and s are not part of representation
 Unlike in some other languages
 s known by compiler – usually irrelevant to programmer
 m often known by compiler – if not, must be saved by
programmer
a[0]
a[1]
a[2]
0x1000
0x1004
0x1008
int a[3];
Cox Arrays and Pointers 5
Array Representation
char c1;
int a[3];
char c2;
int i;
c1
a[0]
a[1]
a[2]
i
0x1000
0x1004
0x1008
0x100C
0x1014
c2
0x1010
Could be optimized by
making these adjacent,
and reducing padding
(by default, not)
Array aligned by
size of elements
Cox Arrays and Pointers 6
Array Sizes
What is
sizeof(array[3])?
sizeof(array)?
int array[10];
4
40
returns the size of
an object in bytes
Cox Arrays and Pointers 7
Multi-Dimensional Arrays
int matrix[2][3];
matrix[1][0] = 17;
matrix[0][0]
matrix[0][1]
matrix[0][2]
0x1000
0x1004
0x1008
matrix[1][0]
matrix[1][1]
matrix[1][2]
0x100C
0x1010
0x1014
Recall: no bounds checking
What happens when you write:
matrix[0][3] = 42;
“Row Major”
Organization
Cox Arrays and Pointers 8
Variable-Length Arrays
int
function(int n)
{
int array[n];
…
New C99 feature: Variable-length arrays
defined within functions
Global arrays must still have fixed (constant) length
Cox Arrays and Pointers 9
Memory Addresses
Storage cells are typically viewed as being
byte-sized
 Usually the smallest addressable unit of memory
• Few machines can directly address bits individually
 Such addresses are sometimes called byte-
addresses
Memory is often accessed as words
 Usually a word is the largest unit of memory access
by a single machine instruction
• CLEAR’s word size is 8 bytes (= sizeof(long))
 A word-address is simply the byte-address of the
word’s first byte
Cox Arrays and Pointers 10
Pointers
Special case of bounded-size natural numbers
 Maximum memory limited by processor word-size
 232
bytes = 4GB, 264
bytes = 16 exabytes
A pointer is just another kind of value
 A basic type in C
int *ptr;
The variable “ptr” stores a pointer to an “int”.
Cox Arrays and Pointers 11
Pointer Operations in C
Creation
& variable Returns variable’s memory address
Dereference
* pointer Returns contents stored at address
Indirect assignment
* pointer = val Stores value at address
Of course, still have...
Assignment
pointer = ptr Stores pointer in another variable
Cox Arrays and Pointers 12
Using Pointers
int i1;
int i2;
int *ptr1;
int *ptr2;
i1 = 1;
i2 = 2;
ptr1 = &i1;
ptr2 = ptr1;
*ptr1 = 3;
i2 = *ptr2;
i1:
i2:
ptr1:
0x1000
0x1004
0x1008
…
ptr2:
…
0x100C
0x1010
0x1014
1
2
0x1000
0x1000
3
3
Cox Arrays and Pointers 13
Using Pointers (cont.)
Type check warning: int_ptr2 is not an int
int1 becomes 8
int int1 = 1036; /* some data to point to */
int int2 = 8;
int *int_ptr1 = &int1; /* get addresses of data */
int *int_ptr2 = &int2;
*int_ptr1 = int_ptr2;
*int_ptr1 = int2;
What happens?
Cox Arrays and Pointers 14
Using Pointers (cont.)
Type check warning: *int_ptr2 is not an int *
Changes int_ptr1 – doesn’t change int1
int int1 = 1036; /* some data to point to */
int int2 = 8;
int *int_ptr1 = &int1; /* get addresses of data */
int *int_ptr2 = &int2;
int_ptr1 = *int_ptr2;
int_ptr1 = int_ptr2;
What happens?
Cox Arrays and Pointers 15
Pointer Arithmetic
pointer + number pointer – number
E.g., pointer + 1 adds 1 something to a pointer
char *p;
char a;
char b;
p = &a;
p += 1;
int *p;
int a;
int b;
p = &a;
p += 1;
In each, p now points to b
(Assuming compiler doesn’t
reorder variables in memory)
Adds 1*sizeof(char) to
the memory address
Adds 1*sizeof(int) to
the memory address
Pointer arithmetic should be used cautiously
Cox Arrays and Pointers 16
A Special Pointer in C
Special constant pointer NULL
 Points to no data
 Dereferencing illegal – causes segmentation fault
 To define, include <stdlib.h> or <stdio.h>
Cox Arrays and Pointers 17
Generic Pointers
void *: a “pointer to anything”
Lose all information about what type of thing
is pointed to
 Reduces effectiveness of compiler’s type-checking
 Can’t use pointer arithmetic
void *p;
int i;
char c;
p = &i;
p = &c;
putchar(*(char *)p);
type cast: tells the compiler to
“change”an object’s type (for type
checking purposes – does not modify
the object in any way)
Dangerous! Sometimes necessary…
Cox Arrays and Pointers 18
Pass-by-Reference
void
set_x_and_y(int *x, int *y)
{
*x = 1001;
*y = 1002;
}
void
f(void)
{
int a = 1;
int b = 2;
set_x_and_y(&a, &b);
}
1
2
a
b
x
y
1001
1002
Cox Arrays and Pointers 19
Arrays and Pointers
Dirty “secret”:
Array name  a pointer to the
initial (0th) array element
a[i]  *(a + i)
An array is passed to a function
as a pointer
 The array size is lost!
Usually bad style to interchange
arrays and pointers
 Avoid pointer arithmetic!
Really int *array
int
foo(int array[],
unsigned int size)
{
… array[size - 1] …
}
int
main(void)
{
int a[10], b[5];
… foo(a, 10)… foo(b, 5) …
}
Must explicitly
pass the size
Passing arrays:
Cox Arrays and Pointers 20
Arrays and Pointers
int
foo(int array[],
unsigned int size)
{
…
printf(“%dn”, sizeof(array));
}
int
main(void)
{
int a[10], b[5];
… foo(a, 10)… foo(b, 5) …
printf(“%dn”, sizeof(a));
}
What does this print?
What does this print?
8
40
... because array is really
a pointer
Cox Arrays and Pointers 21
Arrays and Pointers
int i;
int array[10];
for (i = 0; i < 10; i++) {
…
array[i] = …;
…
}
int *p;
int array[10];
for (p = array; p < &array[10]; p++) {
…
*p = …;
…
}
These two blocks of code are functionally equivalent
Cox Arrays and Pointers 22
Strings
In C, strings are just an array of characters
 Terminated with ‘0’ character
 Arrays for bounded-length strings
 Pointer for constant strings (or unknown length)
char str1[15] = “Hello, world!n”;
char *str2 = “Hello, world!n”;
H e l l o , w l
o r d !n
length
H e l l o , w l
o r d !nterminator
Pascal, Java, …
C, …
C terminator: ’0’
Cox Arrays and Pointers 23
String length
Must calculate length:
Provided by standard C library: #include <string.h>
int
strlen(char str[])
{
int len = 0;
while (str[len] != ‘0’)
len++;
return (len);
}
can pass an
array or pointer
Check for
terminator
array access
to pointer!
What is the size
of the array???
Pointer to Pointer (char **argv)
Cox Arrays and Pointers 24
Passing arguments to main:
int
main(int argc, char **argv)
{
...
}
an array/vector of
char *
Recall when passing an
array, a pointer to the
first element is passed
size of the argv array/vector
Suppose you run the program this way
UNIX% ./program hello 1 2 3
argc == 5 (five strings on the
command line)
Cox Arrays and Pointers 25
char **argv
argv[0]
argv[1]
argv[2]
0x1000
0x1008
0x1010
argv[3]
argv[4]
0x1018
0x1020
“./program”
“hello”
“1”
“2”
“3”
These are strings!!
Not integers!
Cox Arrays and Pointers 26
Next Time
Structures and Unions
Ad

Recommended

Module 02 Pointers in C
Module 02 Pointers in C
Tushar B Kute
 
See through C
See through C
Tushar B Kute
 
C Programming : Pointers and Arrays, Pointers and Strings
C Programming : Pointers and Arrays, Pointers and Strings
Selvaraj Seerangan
 
Unit 3(Arrays).pptx arrays topics in the c lang
Unit 3(Arrays).pptx arrays topics in the c lang
meesalasrinuvasuraon
 
ch08.ppt
ch08.ppt
NewsMogul
 
Learn C program in Complete c programing string and its functions like array...
Learn C program in Complete c programing string and its functions like array...
paraliwarrior
 
C for Java programmers (part 2)
C for Java programmers (part 2)
Dmitry Zinoviev
 
Pointers in c - Mohammad Salman
Pointers in c - Mohammad Salman
MohammadSalman129
 
Embedded C The IoT Academy
Embedded C The IoT Academy
The IOT Academy
 
4.ArraysInC.pdf
4.ArraysInC.pdf
FarHanWasif1
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
Sreedhar Chowdam
 
POINTERS in C language:- Mastering-Pointers-in-C
POINTERS in C language:- Mastering-Pointers-in-C
mushtaqsaliq9
 
Memory Management for C and C++ _ language
Memory Management for C and C++ _ language
23ecuos117
 
C Programming - Refresher - Part III
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
ssusere19c741
 
pointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in c
ssuser2d043c
 
Advanced C programming
Advanced C programming
Claus Wu
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Pointers_c
Pointers_c
ahmed safwat
 
l7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
SPL_PS2 (1).ppt
SPL_PS2 (1).ppt
tadudemise
 
Pointers C programming
Pointers C programming
Appili Vamsi Krishna
 
Pointers definition syntax structure use
Pointers definition syntax structure use
dheeraj658032
 
C programming session 07
C programming session 07
Vivek Singh
 
C programming - Pointer and DMA
C programming - Pointer and DMA
Achyut Devkota
 
Chapter09-10 Pointers and operations .PPT
Chapter09-10 Pointers and operations .PPT
ShalabhMishra10
 
Chapter09-10.PPT
Chapter09-10.PPT
shubhamshukla9769280
 
C programming day#2.
C programming day#2.
Mohamed Fawzy
 
Reinforcement learning ppt in machine learning.pptx
Reinforcement learning ppt in machine learning.pptx
bhargavi804095
 
Presentation-lokesh IMAGES for research.pptx
Presentation-lokesh IMAGES for research.pptx
bhargavi804095
 

More Related Content

Similar to concept on arrays and pointers with examples arrays-pointers.ppt (20)

Embedded C The IoT Academy
Embedded C The IoT Academy
The IOT Academy
 
4.ArraysInC.pdf
4.ArraysInC.pdf
FarHanWasif1
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
Sreedhar Chowdam
 
POINTERS in C language:- Mastering-Pointers-in-C
POINTERS in C language:- Mastering-Pointers-in-C
mushtaqsaliq9
 
Memory Management for C and C++ _ language
Memory Management for C and C++ _ language
23ecuos117
 
C Programming - Refresher - Part III
C Programming - Refresher - Part III
Emertxe Information Technologies Pvt Ltd
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
ssusere19c741
 
pointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in c
ssuser2d043c
 
Advanced C programming
Advanced C programming
Claus Wu
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
Pointers_c
Pointers_c
ahmed safwat
 
l7-pointers.ppt
l7-pointers.ppt
ShivamChaturvedi67
 
SPL_PS2 (1).ppt
SPL_PS2 (1).ppt
tadudemise
 
Pointers C programming
Pointers C programming
Appili Vamsi Krishna
 
Pointers definition syntax structure use
Pointers definition syntax structure use
dheeraj658032
 
C programming session 07
C programming session 07
Vivek Singh
 
C programming - Pointer and DMA
C programming - Pointer and DMA
Achyut Devkota
 
Chapter09-10 Pointers and operations .PPT
Chapter09-10 Pointers and operations .PPT
ShalabhMishra10
 
Chapter09-10.PPT
Chapter09-10.PPT
shubhamshukla9769280
 
C programming day#2.
C programming day#2.
Mohamed Fawzy
 
Embedded C The IoT Academy
Embedded C The IoT Academy
The IOT Academy
 
C Recursion, Pointers, Dynamic memory management
C Recursion, Pointers, Dynamic memory management
Sreedhar Chowdam
 
POINTERS in C language:- Mastering-Pointers-in-C
POINTERS in C language:- Mastering-Pointers-in-C
mushtaqsaliq9
 
Memory Management for C and C++ _ language
Memory Management for C and C++ _ language
23ecuos117
 
0-Slot11-12-Pointers.pdf
0-Slot11-12-Pointers.pdf
ssusere19c741
 
pointer in c through addressing modes esntial in c
pointer in c through addressing modes esntial in c
ssuser2d043c
 
Advanced C programming
Advanced C programming
Claus Wu
 
Pointers and Dynamic Memory Allocation
Pointers and Dynamic Memory Allocation
Rabin BK
 
SPL_PS2 (1).ppt
SPL_PS2 (1).ppt
tadudemise
 
Pointers definition syntax structure use
Pointers definition syntax structure use
dheeraj658032
 
C programming session 07
C programming session 07
Vivek Singh
 
C programming - Pointer and DMA
C programming - Pointer and DMA
Achyut Devkota
 
Chapter09-10 Pointers and operations .PPT
Chapter09-10 Pointers and operations .PPT
ShalabhMishra10
 
C programming day#2.
C programming day#2.
Mohamed Fawzy
 

More from bhargavi804095 (20)

Reinforcement learning ppt in machine learning.pptx
Reinforcement learning ppt in machine learning.pptx
bhargavi804095
 
Presentation-lokesh IMAGES for research.pptx
Presentation-lokesh IMAGES for research.pptx
bhargavi804095
 
Lec3-coa give sthe information abt instruction set.ppt
Lec3-coa give sthe information abt instruction set.ppt
bhargavi804095
 
computerregisters during data and address communication.pdf
computerregisters during data and address communication.pdf
bhargavi804095
 
Computer forensics and cyber security powerpoint presentation
Computer forensics and cyber security powerpoint presentation
bhargavi804095
 
chapter1-basic-structure-of-computers.ppt
chapter1-basic-structure-of-computers.ppt
bhargavi804095
 
Ch10_The_STACK_and_Subroutines_Slides.pptx
Ch10_The_STACK_and_Subroutines_Slides.pptx
bhargavi804095
 
Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.
bhargavi804095
 
Lec26.pptx An array is a linear data structure
Lec26.pptx An array is a linear data structure
bhargavi804095
 
08-Pointers.docx An array is a linear data structure
08-Pointers.docx An array is a linear data structure
bhargavi804095
 
The Greibach normal form is referred to as GNF gnf1.pdf
The Greibach normal form is referred to as GNF gnf1.pdf
bhargavi804095
 
java1.pptjava is programming language, having core and advanced java
java1.pptjava is programming language, having core and advanced java
bhargavi804095
 
Big Data Analytics is not something which was just invented yesterday!
Big Data Analytics is not something which was just invented yesterday!
bhargavi804095
 
Apache Spark™ is a multi-language engine for executing data-S5.ppt
Apache Spark™ is a multi-language engine for executing data-S5.ppt
bhargavi804095
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
bhargavi804095
 
A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...
bhargavi804095
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
bhargavi804095
 
While writing program in any language, you need to use various variables to s...
While writing program in any language, you need to use various variables to s...
bhargavi804095
 
Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...
bhargavi804095
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
bhargavi804095
 
Reinforcement learning ppt in machine learning.pptx
Reinforcement learning ppt in machine learning.pptx
bhargavi804095
 
Presentation-lokesh IMAGES for research.pptx
Presentation-lokesh IMAGES for research.pptx
bhargavi804095
 
Lec3-coa give sthe information abt instruction set.ppt
Lec3-coa give sthe information abt instruction set.ppt
bhargavi804095
 
computerregisters during data and address communication.pdf
computerregisters during data and address communication.pdf
bhargavi804095
 
Computer forensics and cyber security powerpoint presentation
Computer forensics and cyber security powerpoint presentation
bhargavi804095
 
chapter1-basic-structure-of-computers.ppt
chapter1-basic-structure-of-computers.ppt
bhargavi804095
 
Ch10_The_STACK_and_Subroutines_Slides.pptx
Ch10_The_STACK_and_Subroutines_Slides.pptx
bhargavi804095
 
Pointers are one of the core components of the C programming language.
Pointers are one of the core components of the C programming language.
bhargavi804095
 
Lec26.pptx An array is a linear data structure
Lec26.pptx An array is a linear data structure
bhargavi804095
 
08-Pointers.docx An array is a linear data structure
08-Pointers.docx An array is a linear data structure
bhargavi804095
 
The Greibach normal form is referred to as GNF gnf1.pdf
The Greibach normal form is referred to as GNF gnf1.pdf
bhargavi804095
 
java1.pptjava is programming language, having core and advanced java
java1.pptjava is programming language, having core and advanced java
bhargavi804095
 
Big Data Analytics is not something which was just invented yesterday!
Big Data Analytics is not something which was just invented yesterday!
bhargavi804095
 
Apache Spark™ is a multi-language engine for executing data-S5.ppt
Apache Spark™ is a multi-language engine for executing data-S5.ppt
bhargavi804095
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
bhargavi804095
 
A File is a collection of data stored in the secondary memory. So far data wa...
A File is a collection of data stored in the secondary memory. So far data wa...
bhargavi804095
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
bhargavi804095
 
While writing program in any language, you need to use various variables to s...
While writing program in any language, you need to use various variables to s...
bhargavi804095
 
Python is a high-level, general-purpose programming language. Its design phil...
Python is a high-level, general-purpose programming language. Its design phil...
bhargavi804095
 
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
cpp-streams.ppt,C++ is the top choice of many programmers for creating powerf...
bhargavi804095
 
Ad

Recently uploaded (20)

PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
BUSINESS QUIZ PRELIMS | QUIZ CLUB OF PSGCAS | 9 SEPTEMBER 2024
Quiz Club of PSG College of Arts & Science
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Introduction to problem solving Techniques
Introduction to problem solving Techniques
merlinjohnsy
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
PEST OF WHEAT SORGHUM BAJRA and MINOR MILLETS.pptx
Arshad Shaikh
 
Overview of Employee in Odoo 18 - Odoo Slides
Overview of Employee in Odoo 18 - Odoo Slides
Celine George
 
How to Manage & Create a New Department in Odoo 18 Employee
How to Manage & Create a New Department in Odoo 18 Employee
Celine George
 
Unit- 4 Biostatistics & Research Methodology.pdf
Unit- 4 Biostatistics & Research Methodology.pdf
KRUTIKA CHANNE
 
Introduction to problem solving Techniques
Introduction to problem solving Techniques
merlinjohnsy
 
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
SPENT QUIZ NQL JR FEST 5.0 BY SOURAV.pptx
Sourav Kr Podder
 
The Man In The Back – Exceptional Delaware.pdf
The Man In The Back – Exceptional Delaware.pdf
dennisongomezk
 
Wax Moon, Richmond, VA. Terrence McPherson
Wax Moon, Richmond, VA. Terrence McPherson
TerrenceMcPherson1
 
Sustainable Innovation with Immersive Learning
Sustainable Innovation with Immersive Learning
Leonel Morgado
 
What are the benefits that dance brings?
What are the benefits that dance brings?
memi27
 
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
FEBA Sofia Univercity final diplian v3 GSDG 5.2025.pdf
ChristinaFortunova
 
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Paper 108 | Thoreau’s Influence on Gandhi: The Evolution of Civil Disobedience
Rajdeep Bavaliya
 
Plate Tectonic Boundaries and Continental Drift Theory
Plate Tectonic Boundaries and Continental Drift Theory
Marie
 
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Basic English for Communication - Dr Hj Euis Eti Rohaeti Mpd
Restu Bias Primandhika
 
Ray Dalio How Countries go Broke the Big Cycle
Ray Dalio How Countries go Broke the Big Cycle
Dadang Solihin
 
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
Assisting Individuals and Families to Promote and Maintain Health – Unit 7 | ...
RAKESH SAJJAN
 
Exploring Ocean Floor Features for Middle School
Exploring Ocean Floor Features for Middle School
Marie
 
How to Create an Event in Odoo 18 - Odoo 18 Slides
How to Create an Event in Odoo 18 - Odoo 18 Slides
Celine George
 
Nice Dream.pdf /
Nice Dream.pdf /
ErinUsher3
 
Ad

concept on arrays and pointers with examples arrays-pointers.ppt

  • 2. Objectives Be able to use arrays, pointers, and strings in C programs Be able to explain the representation of these data types at the machine level, including their similarities and differences Cox Arrays and Pointers 2
  • 3. Cox Arrays and Pointers 3 Arrays in C No bounds checking! Allowed – usually causes no obvious error array[10] may overwrite b Unlike Java, array size in declaration int array[10]; int b; array[0] = 3; array[9] = 4; array[10] = 5; array[-1] = 6; Compare: C: int array[10]; Java: int[] array = new int[10]; All elements of same type – homogenous First element (index 0) Last element (index size - 1)
  • 4. Cox Arrays and Pointers 4 Array Representation Homogeneous  Each element same size – s bytes  An array of m data values is a sequence of ms bytes  Indexing: 0th value at byte s0, 1st value at byte s1, … m and s are not part of representation  Unlike in some other languages  s known by compiler – usually irrelevant to programmer  m often known by compiler – if not, must be saved by programmer a[0] a[1] a[2] 0x1000 0x1004 0x1008 int a[3];
  • 5. Cox Arrays and Pointers 5 Array Representation char c1; int a[3]; char c2; int i; c1 a[0] a[1] a[2] i 0x1000 0x1004 0x1008 0x100C 0x1014 c2 0x1010 Could be optimized by making these adjacent, and reducing padding (by default, not) Array aligned by size of elements
  • 6. Cox Arrays and Pointers 6 Array Sizes What is sizeof(array[3])? sizeof(array)? int array[10]; 4 40 returns the size of an object in bytes
  • 7. Cox Arrays and Pointers 7 Multi-Dimensional Arrays int matrix[2][3]; matrix[1][0] = 17; matrix[0][0] matrix[0][1] matrix[0][2] 0x1000 0x1004 0x1008 matrix[1][0] matrix[1][1] matrix[1][2] 0x100C 0x1010 0x1014 Recall: no bounds checking What happens when you write: matrix[0][3] = 42; “Row Major” Organization
  • 8. Cox Arrays and Pointers 8 Variable-Length Arrays int function(int n) { int array[n]; … New C99 feature: Variable-length arrays defined within functions Global arrays must still have fixed (constant) length
  • 9. Cox Arrays and Pointers 9 Memory Addresses Storage cells are typically viewed as being byte-sized  Usually the smallest addressable unit of memory • Few machines can directly address bits individually  Such addresses are sometimes called byte- addresses Memory is often accessed as words  Usually a word is the largest unit of memory access by a single machine instruction • CLEAR’s word size is 8 bytes (= sizeof(long))  A word-address is simply the byte-address of the word’s first byte
  • 10. Cox Arrays and Pointers 10 Pointers Special case of bounded-size natural numbers  Maximum memory limited by processor word-size  232 bytes = 4GB, 264 bytes = 16 exabytes A pointer is just another kind of value  A basic type in C int *ptr; The variable “ptr” stores a pointer to an “int”.
  • 11. Cox Arrays and Pointers 11 Pointer Operations in C Creation & variable Returns variable’s memory address Dereference * pointer Returns contents stored at address Indirect assignment * pointer = val Stores value at address Of course, still have... Assignment pointer = ptr Stores pointer in another variable
  • 12. Cox Arrays and Pointers 12 Using Pointers int i1; int i2; int *ptr1; int *ptr2; i1 = 1; i2 = 2; ptr1 = &i1; ptr2 = ptr1; *ptr1 = 3; i2 = *ptr2; i1: i2: ptr1: 0x1000 0x1004 0x1008 … ptr2: … 0x100C 0x1010 0x1014 1 2 0x1000 0x1000 3 3
  • 13. Cox Arrays and Pointers 13 Using Pointers (cont.) Type check warning: int_ptr2 is not an int int1 becomes 8 int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; *int_ptr1 = int_ptr2; *int_ptr1 = int2; What happens?
  • 14. Cox Arrays and Pointers 14 Using Pointers (cont.) Type check warning: *int_ptr2 is not an int * Changes int_ptr1 – doesn’t change int1 int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; int_ptr1 = *int_ptr2; int_ptr1 = int_ptr2; What happens?
  • 15. Cox Arrays and Pointers 15 Pointer Arithmetic pointer + number pointer – number E.g., pointer + 1 adds 1 something to a pointer char *p; char a; char b; p = &a; p += 1; int *p; int a; int b; p = &a; p += 1; In each, p now points to b (Assuming compiler doesn’t reorder variables in memory) Adds 1*sizeof(char) to the memory address Adds 1*sizeof(int) to the memory address Pointer arithmetic should be used cautiously
  • 16. Cox Arrays and Pointers 16 A Special Pointer in C Special constant pointer NULL  Points to no data  Dereferencing illegal – causes segmentation fault  To define, include <stdlib.h> or <stdio.h>
  • 17. Cox Arrays and Pointers 17 Generic Pointers void *: a “pointer to anything” Lose all information about what type of thing is pointed to  Reduces effectiveness of compiler’s type-checking  Can’t use pointer arithmetic void *p; int i; char c; p = &i; p = &c; putchar(*(char *)p); type cast: tells the compiler to “change”an object’s type (for type checking purposes – does not modify the object in any way) Dangerous! Sometimes necessary…
  • 18. Cox Arrays and Pointers 18 Pass-by-Reference void set_x_and_y(int *x, int *y) { *x = 1001; *y = 1002; } void f(void) { int a = 1; int b = 2; set_x_and_y(&a, &b); } 1 2 a b x y 1001 1002
  • 19. Cox Arrays and Pointers 19 Arrays and Pointers Dirty “secret”: Array name  a pointer to the initial (0th) array element a[i]  *(a + i) An array is passed to a function as a pointer  The array size is lost! Usually bad style to interchange arrays and pointers  Avoid pointer arithmetic! Really int *array int foo(int array[], unsigned int size) { … array[size - 1] … } int main(void) { int a[10], b[5]; … foo(a, 10)… foo(b, 5) … } Must explicitly pass the size Passing arrays:
  • 20. Cox Arrays and Pointers 20 Arrays and Pointers int foo(int array[], unsigned int size) { … printf(“%dn”, sizeof(array)); } int main(void) { int a[10], b[5]; … foo(a, 10)… foo(b, 5) … printf(“%dn”, sizeof(a)); } What does this print? What does this print? 8 40 ... because array is really a pointer
  • 21. Cox Arrays and Pointers 21 Arrays and Pointers int i; int array[10]; for (i = 0; i < 10; i++) { … array[i] = …; … } int *p; int array[10]; for (p = array; p < &array[10]; p++) { … *p = …; … } These two blocks of code are functionally equivalent
  • 22. Cox Arrays and Pointers 22 Strings In C, strings are just an array of characters  Terminated with ‘0’ character  Arrays for bounded-length strings  Pointer for constant strings (or unknown length) char str1[15] = “Hello, world!n”; char *str2 = “Hello, world!n”; H e l l o , w l o r d !n length H e l l o , w l o r d !nterminator Pascal, Java, … C, … C terminator: ’0’
  • 23. Cox Arrays and Pointers 23 String length Must calculate length: Provided by standard C library: #include <string.h> int strlen(char str[]) { int len = 0; while (str[len] != ‘0’) len++; return (len); } can pass an array or pointer Check for terminator array access to pointer! What is the size of the array???
  • 24. Pointer to Pointer (char **argv) Cox Arrays and Pointers 24 Passing arguments to main: int main(int argc, char **argv) { ... } an array/vector of char * Recall when passing an array, a pointer to the first element is passed size of the argv array/vector Suppose you run the program this way UNIX% ./program hello 1 2 3 argc == 5 (five strings on the command line)
  • 25. Cox Arrays and Pointers 25 char **argv argv[0] argv[1] argv[2] 0x1000 0x1008 0x1010 argv[3] argv[4] 0x1018 0x1020 “./program” “hello” “1” “2” “3” These are strings!! Not integers!
  • 26. Cox Arrays and Pointers 26 Next Time Structures and Unions