Pointers. Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

Programming and Data Structure
Strings.
1 Chapter 10 Strings and Pointers. 2 Introduction  String Constant  Example: printf(“Hello”); “Hello” : a string constant oA string constant is a series.
Lecture 20 Arrays and Strings
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
By Senem Kumova Metin 1 POINTERS + ARRAYS + STRINGS REVIEW.
1 CS 201 Pointers (2) Debzani Deb. 2 Overview Pointers Functions: pass by reference Quiz 2 : Review Q & A.
Pointer, malloc and realloc 1. Name entered was 6 char, not enough space to put null terminator 2 Array of char.
More Pointers Write a program that: –Calls a function to input an integer value –The above function calls another function that will double the input value.
POINTER Prepared by MMD, Edited by MSY1.  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference.
1 Arrays and Strings. 2 An array is a collection of variables of the same type that are referred to through a common name. It is a Data Structure which.
C-Strings A C-string (also called a character string) is a sequence of contiguous characters in memory terminated by the NUL character '\0'. C-strings.
Pointers A pointer is a variable that contains memory address as its value. A variable directly contains a specific value. A pointer contains an address.
Declaring Arrays Declare an array of 10 elements: int nums[10]; Best practice: #define SIZE 10 int nums[SIZE]; // cannot be int[SIZE] nums; C99: int nums[someVariable]
1 More on Pointers. 2 Reminder 3 Pointers Pointer is a variable that contains the address of a variable Here P is said to point to the variable C C 7.
CSSE221: Software Dev. Honors Day 28 Announcements Announcements Simulation grades coming back Simulation grades coming back All C Projects due Friday.
C Arrays. One-Dimensional Arrays Proper ways of declare an array in C: int hours[ 24]; double Temp [24]; int test_score [15] = { 77, 88, 99, 100, 87,
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
CMPE-013/L: “C” Programming Gabriel Hugh Elkaim – Spring 2013 CMPE-013/L Arrays and Strings Gabriel Hugh Elkaim Spring 2013.
Pointers CSE 2451 Rong Shi.
ARRAYS In this Lecture, we will try to develop understanding of some of the relatively complex concepts. The following are explained in this lecture with.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
POINTERS. 1.a) POINTER EXPRESSIONS Pointer variables can be used in expression If p1 and p2 are properly declared and initialized pointers then following.
STRING Dong-Chul Kim BioMeCIS UTA 10/7/
Chapter 7: Pointers Basic concept of pointers Pointer declaration Pointer operator (& and *) Parameter passing by reference.
5. Arrays, Pointers and Strings 7 th September IIT Kanpur C Course, Programming club, Fall
1 CHAPTER 5 POINTER. 2 Pointers  Basic concept of pointers  Pointer declaration  Pointer operator (& and *)  Parameter passing by reference  Dynamic.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CSEB 114: PRINCIPLE OF PROGRAMMING Chapter 7: Pointers.
CSC141- Introduction to Computer programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 21 Thanks for Lecture Slides:
C Programming - Structures. Structures containing arrays A structure member that is an array does not ‘behave’ like an ordinary array When copying a structure.
Pointers *, &, array similarities, functions, sizeof.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
+ Pointers. + Content Address of operator (&) Pointers Pointers and array.
Arrays, Part 2 We have already learned how to work with arrays using subscript notation. Example: float myData[] = {3.5, 4.0, 9.34}; myData[0] += 2; printf("myData[0]
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Pointers and Arrays An array's name is a constant whose value is the address of the array's first element. For this reason, the value of an array's name.
Pointers PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
Arrays, Strings, and Memory. Command Line Arguments #include int main(int argc, char *argv[]) { int i; printf("Arg# Contents\n"); for (i = 0; i < argc;
Principles of Programming Chapter 8: Character & String  In this chapter, you’ll learn about;  Fundamentals of Strings and Characters  The difference.
Review (before the 1 st test): while (conditions) { statements; } while loop: if/else if/else statements: if (conditions) { statements; } else if (different.
1 Pointers: Parameter Passing and Return. 2 Passing Pointers to a Function Pointers are often passed to a function as arguments  Allows data items within.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z =
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
POINTER Dong-Chul Kim BioMeCIS UTA 3/13/
Pointers. Addresses in Memory Everything in memory has an address. C allows us to obtain the address that a variable is stored at. scanf() is an example.
Functions and Pointers Dr. Sajib Datta Oct 6, 2014.
Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal may.
13. Strings. String Literals String literals are enclosed in double quotes: "Put a disk in drive A, then press any key to continue\n“ A string literal.
Chapter 5 Pointers and Arrays Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Introduction to programming in java Lecture 21 Arrays – Part 1.
Pointers Pointers are variables that contain memory addresses as their values. A variable directly contains a specific value. A pointer contains an address.
Arrays Name, Index, Address. Arrays – Declaration and Initialization int x; y[0] y[1] y[2]
Arrays and Pointers (part 1) CSE 2031 Fall July 2016.
Chapter 8 Arrays, Strings and Pointers
Functions and Pointers
Pointers.
INC 161 , CPE 100 Computer Programming
Hassan Khosravi / Geoffrey Tien
Pointers.
Functions and Pointers
Pointers.
CSE1320 Strings Dr. Sajib Datta
CSE1320 Strings Dr. Sajib Datta
Exercise Arrays.
CS1100 Computational Engineering
Presentation transcript:

Pointers

Pointer Arithmetic Since arrays consist of contiguous memory locations, we can increment (or decrement) the addresses to move through the array. int data[] = {5, 6, 7}; int i; for (i = 0; i < 3; i++) printf("the value at address %p is %d\n", (data + i), *(data + i));

Pointer Arithmetic We can use pointer arithmetic with pointers to non-array ▫int data[] = {5, 6, 7}; ▫int* dataptr = data; ▫printf("pointer address = %p, next address = %p\n", dataptr, dataptr + 1);

Pointer Arithmetic #include int main(void) { char chararray[] = {68, 97, 114, 105, 110}; /* 1 byte each */ int intarray[] = {10, 11, 12, 13, 14}; /* 4 bytes each */ int i; printf("chararray intarray\n"); printf(" \n"); for(i = 0; i < 5; i++) printf("%p, %p\n", (chararray + i), (intarray + i)); return 0; }

chararray intarray FF58, 0012FF3C 0012FF59, 0012FF FF5A, 0012FF FF5B, 0012FF FF5C, 0012FF4C Press any key to continue...

Array of pointers We can have an array whose members are pointers, in this example pointers-to-int. int* data[3]; int i; int x = 5; int y = 89; int z = 34; data[0] = &x; data[1] = &y; data[2] = &z; for(i = 0; i < 3; i++) printf("%d\n", *data[i]);

#include int main() { printf("The address of the string is %p.\n", "apple"); printf("The length of the string is %d.\n", strlen("Hello")); return 0; } Output: The address of the string is The length of the string is 5. Press any key to continue...

Quiz 3

#include void main() { int num = 8; int* numptr = &num; int** ptr2 = &numptr; printf("num is %d\n", num); printf("*numptr is %d\n", *numptr); printf("The content in numptr is %p.\n", numptr); printf("*ptr2 is %p\n", *ptr2); printf("**ptr2 is %d\n", **ptr2); (*numptr)++; printf("*numptr is %d\n", *numptr); (**ptr2)-=3; printf("**ptr2 is %d\n", **ptr2); (numptr)++; printf("The content in numptr is %p.\n", numptr); } What does this program print? (10 points) Assumptions: Address of num is 208 Address of numptr is 220 Address of ptr2 is 232

Solution num is 8 *numptr is 8 The content in numptr is 208. *ptr2 is 208 **ptr2 is 8 *numptr is 9 **ptr2 is 6 The content in numptr is 212. Press any key to continue...

Passing the address after the last elements in the array #include int sump(int * start, int * end); int main(void) { int marbles[] = {20, 14, 23, 54}; int answer; answer = sump(marbles, marbles + 4); printf("The sum of integers in marbles is %d.\n", answer); return 0; } int sump(int * start, int * end) { int total = 0; while(start < end) { total += *start; start++; } return total; }

2D Array int score[4][2]; score, being the name of an array, is the address of the first element of the array. The first element of score is an array of two integers, so score is the address of an array of two integers. ▫That is score has the same value as &score[0]. Since score[0] is itself an array of two integers, so score[0] has the same value as &score[0][0], the address of its first element. Physically, both score and score[0] have the same address location.

2D Array Adding 1 to a pointer or address yields a value larger by the size of the object referred to. In this respect, score and score[0] differ, because score refers to 4 arrays containing two integers each. score[0] refers to one array of 2 integers. Therefore, score + 1 has a different value from score[0] + 1.

2D Array #include int main(void) { int score[4][2] = {{0,1},{2,3},{4,5},{6,7}}; printf("The value of score is %p.\n", score); printf("The value of score[0] is %p.\n", score[0]); printf("The value of &score[0][0] is %p.\n", &score[0][0]); printf("The value of score+1 is %p.\n", score+1); printf("The value of score[0]+1 is %p.\n", score[0]+1); return 0; }

References for pointers

String functions strcat() strcpy() strtok()

strcat() strcat() for string concatenation Take two strings for arguments A copy of the second string is tacked onto the end of the first, and this combined version becomes the new first string. The second string is not altered. The return type of strcat () is char *, the value of its first argument – the address of the first character of the string to which the second string is appended.

Strcat() cont. #include int main(void) { char flower[80]; char addon[] = " is a cat."; scanf("%s", flower); strcat(flower, addon); printf("%s\n",flower); return 0; } Note!!! strcat( ) not checking whether the first array is large enough to hold the second string.

If your input is Kitty, your output is going to be as follow. Kitty is a cat.

Strcpy() #include int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }

Strcpy() #include int main(void) { char orig[] = "beast"; char copy[80] = "Be the best that you can be."; strcpy(copy+7, orig); printf("copy - %s\n", copy); printf(“orig - %s\n", orig); return 0; }

Output copy - Be the beast orig - beast