Title: Pointers, Arrays, and Strings
1Pointers, Arrays, and Strings
Based upon http//www.cs.usask.ca/grads/nic169/csh
ort/week2/week2.html KR (Your C language text)
2What is a pointer?
- Integers, and other objects are all stored
somewhat in the computers memory - at a memory address or location.
- A pointer allows us to access these memory
addresses, and manipulate the addresses as well
as their contents - Creating a pointer int p
- creates a pointer whose target is an integer
3Operators
- Address-of operator If i is a variable, then i
tells us the memory address in which the value of
i is stored. - Dereferencing operator (AKA indirection
operator) The unary operator - When applied to a pointer, it gives us access to
what is stored in the memory address to which the
pointer is pointing.
4Example
- /
- foo1c A simple pointer example
- /
- include ltstdio.hgt
-
- int main(void)
-
- int x 5
- int ptr
- ptr x
- printf("The value of x is d\n", ptr)
- printf("The location of x is p\n", ptr)
- printf("The location of x is p\n", x)
- ptr 10
- printf("d\n", x)
- printf("d\n", ptr)
- printf("d\n", (ptr))
- printf("d\n", ptr)
- return 0
- Output
- The value of x is 5
- The location of x is 0xbff6b2f4
- The location of x is 0xbff6b2f4
- 15
- 16
- 16
- 17
From http//www.cs.usask.ca/grads/nic169/cshort/we
ek2/week2.html
5Associativity
- Unary operators associate from right to left
- (ptr)
- increments and returns the value stored at the
address pointed to by the pointer - ptr
- increments the pointer, and returns the value
stored at the next memory location
6Passing by Reference vs. Passing by Value
- / Pass by value /
- void swap1(int x, int y)
-
- int z
- z x
- x y
- y z
- / Pass by reference /
- void swap2(int x, int y)
-
- int z
- z x
- x y
- y z
-
Slight modification of http//www.cs.usask.ca/gra
ds/nic169/cshort/week2/week2.html
7Main method Output
- include ltstdio.hgt
- / function prototypes /
- void swap1(int x, int y)
- void swap2(int x, int y)
- int main(void)
-
- int a 3, b 5
- printf("a d, b d\n", a, b)
- swap1(a, b)
- printf("a d, b d\n", a, b)
- swap2(a, b)
- printf("a d, b d\n", a, b)
- return 0
-
- Output
- a 3, b 5
- a 3, b 5
- a 5, b 3
Slight modification of http//www.cs.usask.ca/gra
ds/nic169/cshort/week2/week2.html
8Pointers to pointers!
- /
- pointer2 Pointing to pointers
- /
- include ltstdio.hgt
- int main ()
-
- int x 5
- int ptr1 x / A pointer to x /
- int ptr2 ptr1 / A pointer to ptr1 /
- printf("The value of x is d\n", x)
- printf("The value of x is d\n", ptr1)
- printf("The value of x is d\n", ptr2)
- return 0
- Pointer variables have addresses of their own and
can be pointed to
9Arrays
- Declaring an array of 10 integers
- int a10
- This declaration creates 10 consecutive integer
elements named a0, a1, , a9 - Important Index numbers start at 0!
10Arrays (2)
- int a10
- int pnt
- pnt a3 / pnt now points to the 4th element
of the array a / - pnt a
- pa a0
- In each case, pnt is set to point to the initial
element of the array. The name of an array is a
synonym for its first element
11Example
- /
- foo4.c Pointers and Arrays
- /
- include ltstdio.hgt
- int main()
-
- int i
- int a5 1, 2, 3, 4, 5 / note this
/ - int ptr, ptr1
- ptr a0
- ptr1 a / ptr1 points to the array a
/ - for (i 0 i lt 5 i)
- printf("d d d d\n", ai,
(ai), (ptri), ptr1) -
- return 0
-
Output 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 4 5 5 5
5
From http//www.cs.usask.ca/grads/nic169/cshort/we
ek2/week2.html
12Strings
- Strings are stored as arrays of type char
- The last character of a string should be NULL
(written as \0)
13Strings - example
- /
- foo5.c string constants
- Illustrates string constants array of chars
another way of declaring a char array - /
- include ltstdio.hgt
- int main()
-
- char a "I am a constant" / a pointer /
- char b "Hello" / an array /
- char c 'H', 'e', 'l', 'l', 'o', '\0'
- printf("s\ns\ns\n",a, b, c)
- return 0
-
From http//www.cs.usask.ca/grads/nic169/cshort/we
ek2/week2.html
14String Storage
From http//www.cs.usask.ca/grads/nic169/cshort/we
ek2/week2.html
15Strlen ()
- /
- foo6.c the Strlen() function
- /
- include ltstdio.hgt
- int Strlen (char s) / prototype /
- int main()
-
- char str1100
- char ptr "Hello World"
- / what happens if Hello World is the input /
- printf("Enter a string")
- scanf("s", str1)
- printf("d\n", Strlen(ptr))
- printf("d\n", Strlen("Hello World"))
- printf("d\n", Strlen(str1))
- return 0
- int Strlen (char s)
-
- int n
- for (n 0 s ! '\0' s)
- n
- return n
-
From http//www.cs.usask.ca/grads/nic169/cshort/we
ek2/week2.html
16String functions
- Stdio.h defines some useful functions
- gets(), puts(), getchar(), putchar()
- Other functions require another header file
- include ltstring.hgt
- For a handy list of functions look at under the
string.h section of http//refcards.com/refcards/c
/ - Further information about many functions can be
found on lab machines by executing - Eg. man 3 strlen
17Multi-dimensional arrays
- int b325 / 3-dimensional array /
- int a54
- A 2-dimensional array
- Compiler allocates space for 54 20 chars
- Individual integers can be accessed in a variety
of ways
18Example
- /
- arr Multi-dimensional array referencing example
- /
- include ltstdio.hgt
-
- int main ()
- int a54 1,2,3,4, 5,6,7,8,
9,10,11,12, - 13,14,15,16,
17,18,19,20 - int i 2
- int j 3
- printf("d\n", aij)
- printf("d\n", (ai j))
- printf("d\n", ((ai))j)
- printf("d\n", (a00 4i j))
- return 0
Output 12 12 12 12
19Pointer arrays
- static char name January, February,
March, , December - Different month names are different length, and
here each is only allocated space as needed
20Multi-dimensional vs. Pointer
- int a1020
- A 2-dimensional array. 200 int-sized locations
allocated - int b10
- Allocates 10 pointers, but leaves them
uninitialized - Initialization must be done explicitly
- If each pointer in a points to a 20 element
array, then 10 pointer spaces 200 int-sized
spaces are allocated
21Command line arguments
- Declare main as a method with two parameters
- argc the number of parameters
- argv pointer to a list of strings that are the
arguments - int main (int argc, char argv)
-
-
22Example
- Execute
- ./a.out This is an example of a long list of
command line arguments - Output
- Argument 0 ./a.out
- Argument 1 This
- Argument 2 is
- Argument 3 an
- Argument 4 example
- Argument 5 of
- Argument 6 a
- Argument 7 long
- Argument 8 list
- Argument 9 of
- Argument 10 command
- Argument 11 line
- Argument 12 arguments
- /
- args print out command line arguments
- /
- include ltstdio.hgt
- int main (int argc, char argv)
- int i
- for (i 0 i lt argc i)
- printf ("Argument 3d\ts\n", i, argvi)
- return 0
23Function pointers
- Functions in C arent variables
- but we can still point to them
- int func (char s) / function prototype /
-
- (in code)
- int (ptr) (char s) func / function pointer
/
24Example