CS1100 Computational Engineering

Slides:



Advertisements
Similar presentations
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.
Advertisements

CSCI 171 Presentation 11 Pointers. Pointer Basics.
Kernighan/Ritchie: Kelley/Pohl:
Engineering Problem Solving With C++ An Object Based Approach Chapter 9 Pointers and Creating Data Structures.
Chapter 10.
Pointers Pointer - A pointer is a derived data type; that is it is a data type built from one of the standard types. Its value is any of the addresses.
Pointers. Topics Pointers Pointer Arithmetic Pointers and Arrays.
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.
Even More C Programming Pointers. Names and Addresses every variable has a location in memory. This memory location is uniquely determined by a memory.
Pointers Applications
Programming Pointers. Variables in Memory x i c The compiler determines where variables are placed in memory This placement cannot.
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
Chapter 9 Character Strings 9.1 Character String Constants A character string constant is a sequence of characters enclosed in double quotation mark. Examples.
CSC 2400 Computer Systems I Lecture 5 Pointers and Arrays.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Pointers.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
Representing Strings and String I/O. Introduction A string is a sequence of characters and is treated as a single data item. A string constant, also termed.
© Oxford University Press All rights reserved. CHAPTER 7 POINTERS.
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 PART - 2. Pointers Pointers are variables that contain memory addresses as their values. A variable name directly references a value. A pointer.
DCT1063 Programming 2 CHAPTER 3 STRINGS Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT:10 Advance Pointer Array, String and Dynamic Memory Allocation CS2311 Computer Programming.
Lecture 7: Arrays BJ Furman 06OCT2012. The Plan for Today Announcements Review of variables and memory Arrays  What is an array?  How do you declare.
1 Arrays and Pointers The name of an array is a pointer constant to the first element. Because the array’s name is a pointer constant, its value cannot.
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.
Principles of Programming - NI Chapter 10: Character & String : In this chapter, you’ll learn about; Fundamentals of Strings and Characters The difference.
Windows Programming Lecture 03. Pointers and Arrays.
CSC 215 Pointers and Arrays. Pointers C provides two unary operators, & and *, for manipulating data using pointers The operator &, when applied to a.
Strings C supports strings using one-dimensional character arrays. A string is defined as a null-terminated character array. In C, a null is 0. You must.
Topic 5 Addresses, Pointers and Arrays. 2 Objectives (Textbook Chapter 14) You should be able to describe: Addresses and Pointers Pointer Operators Pointer.
מערכים (arrays) 02 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 1602 אוקטובר אוקטובר אוקטובר 16 Department.
CSE 220 – C Programming malloc, calloc, realloc.
CS1010 Programming Methodology
CS1010 Programming Methodology
Stack and Heap Memory Stack resident variables include:
Pointers & Arrays 1-d arrays & pointers 2-d arrays & pointers.
UNIT 5 C Pointers.
Pointers.
© 2016 Pearson Education, Ltd. All rights reserved.
Lecture 8 String 1. Concept of strings String and pointers
Numeric Arrays Numeric Arrays Chapter 4.
CNG 140 C Programming (Lecture set 10)
INC 161 , CPE 100 Computer Programming
Arrays in C.
Programming Languages and Paradigms
Lecture 6 C++ Programming
Character Strings Lesson Outline
CS111 Computer Programming
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Lecture 8b: Strings BJ Furman 15OCT2012.
CS1100 Computational Engineering
7 Arrays.
Pointers Department of Computer Science-BGU יום רביעי 21 נובמבר 2018.
Pointers  Week 10.
Pointers and Pointer-Based Strings
Lecture 18 Arrays and Pointer Arithmetic
Outline Defining and using Pointers Operations on pointers
Introduction to Problem Solving and Programming
Pointers The C programming language gives us the ability to directly manipulate the contents of memory addresses via pointers. Unfortunately, this power.
Your questions from last session
7 Arrays.
Overloading functions
C++ Pointers and Strings
Variables, Pointers, and Arrays
Data Structures and Algorithms Introduction to Pointers
Exercise Arrays.
Arrays.
CS31 Discussion 1H Fall18: week 6
C++ Pointers and Strings
Pointers and pointer applications
Presentation transcript:

CS1100 Computational Engineering Pointers Instructors : Jayalal Sarma And Kamakoti V.

Accessing Arrays with Pointers #include <stdio.h> int myArray[ ] = {1,24,17,4,-5,100}; int *ptr; int main(void){ int i; ptr = &myArray[0]; printf(“\n”); for (i = 0; i < 6; i++){ printf("myArray[%d] = %d ", i, myArray[i]); printf(“value at ptr + %d is %d\n", i, *(ptr + i)); } return 0;

++ptr and ptr++ are both equivalent to ptr + 1 though they are “incremented” at different times Replace the following statement printf(“value at ptr + %d is %d\n", i, *(ptr + i)); with: printf("ptr + %d = %d\n",i, *ptr++); printf("ptr + %d = %d\n",i, *(++ptr));

This has to do with the precedence of the operators. *ptr++ *ptr++ is to be interpreted as returning the value pointed to by ptr and then incrementing the pointer value. This has to do with the precedence of the operators. (*ptr)++ would increment, not the pointer, but that which the pointer points to! i.e. if used on the first character of the example string “IIT” the ‘I’ would be incremented to a ‘J’.

Arrays The name of the array is the address of the first element in the array In C, we can replace ptr = &myArray[0]; with ptr = myArray; to achieve the same result Many texts state that the name of an array is a pointer

Arrays Names Are Not Pointers While we can write ptr = myArray; we cannot write myArray = ptr; The reason: While ptr is a variable, myArray is a constant That is, the location at which the first element of myArray will be stored cannot be changed once myArray has been declared

A void pointer is a generic pointer Pointer Types C provides for a pointer of type void. We can declare such a pointer by writing: void *vptr; A void pointer is a generic pointer For example, a pointer to any type can be compared to a void pointer Type casts can be used to convert from one type of pointer to another under proper circumstances

Trying Out Pointers #include <stdio.h> int j = 1, k = 2; int *ptr; main( ) { ptr = &k; printf(“\n j has the value %d and is stored at %p”,j,(void*)&j); printf(“\n k has the value %d and is stored at %p”,k,(void*) &k); printf(“\n ptr has the value %p stored at %p”, ptr, (void *) &ptr); printf(“\nThe value of the integer pointed to by ptr is %d\n”, *ptr); } Generic address of j Dereferencing – will print r-value of k

C does not have a string type Pointers and Strings C does not have a string type languages like Pascal, Fortran have… In C, a string is an array of characters terminated with a binary zero character (written as '\0’) remember this is not the character ‘0’ One can manipulate strings as character arrays Character arrays can also be accessed by pointers

One could create a string as follows, char myString[40]; A Character Array One could create a string as follows, char myString[40]; myString[0] = 'T'; myString[1] = 'e'; myString[2] = 'd’; myString[3] = '\0’; Note - terminated with a nul character nul (or ‘\0’) ≠ NULL (pointer to nothing) Obviously this is very tedious Ted Jenson’s tutorial on pointers http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

char myString[40] = {'T', 'e', 'd', '\0’}; “Strings” One might write: char myString[40] = {'T', 'e', 'd', '\0’}; But this also takes more typing than is convenient So, C permits: char myString[40] = "Ted"; Note that C automatically inserts ‘\0’ Compiler sets aside a contiguous block of memory 40 bytes long The first four bytes contain Ted\0

Strings: Input and Output The function gets( ) accepts the name of the string as a parameter, and fills the string with characters that are input from the keyboard till newline character is encountered. At the end, a null terminator is appended. Not a popular function because there are no built-in checks char *gets(char *s); gets(str) – reads from standard input into str puts(str) – writes to standard output from str

gets may Overwrite Memory char b1[] = “ABCDE”; char b2[] = “LMNOF”; char b3[] = “ZYXWV”; puts(b1); puts(b2); puts(b3); puts(“Input:”); gets(b2); A sample run Another run puts(b1); ABCDE puts(b2); LMNOP puts(b3); ZYXWV puts(…); Input: 1234 gets(b2); puts(b2); 1234 puts(b1); ABCDE puts(b2); LMNOP puts(b3); ZYXWV puts(…); Input: 1234567890 gets(b2); puts(b1); 7890 puts(b2); 1234567890

Character Pointers #include <stdio.h> char strA[80] = "A string to be used for demonstration"; char strB[80]; int main(void) { char *pA; /* a pointer to type character */ char *pB; /* another pointer to type character */ puts(strA); /* show string A */ pA = strA; /* point pA at string A */ puts(pA); /* show what pA is pointing to */ --continued 

Copying Strings… pB = strB; /* point pB at string B */ putchar('\n'); /* move down one line on the screen */ while(*pA != '\0') /* while string */ { *pB++ = *pA++; /* copy and increment pointer */ } *pB = '\0'; /* insert end-of-string */ puts(strB); /* show strB on screen */ return 0; Ted Jenson’s tutorial on pointers http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

A Version of strcpy char *myStrcpy(char *destination, char *source) { char *p = destination; while (*source != '\0') *p++ = *source++; } *p = '\0'; return destination; Ted Jenson’s tutorial on pointers http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

Copying Arrays Using Pointers Exercise – define a function to copy part of an integer array into another. Access the elements using pointers. Function prototype: void intCopy(int *ptrA, int *ptrB, int num); where num is the number of integers to be copied.

Equivalent Definition Using Arrays char *myStrcpy(char dest[], char source[]) { int i = 0; while (source[i] != '\0') dest[i] = source[i]; i++; } dest[i] = '\0'; return dest; char *myStrcpy(char *destination, char *source) char *p = destination; while (*source != '\0') *p++ = *source++; *p = '\0’; return destination;

Pointer Arithmetic  Array Indexing Both work identically Since parameters are passed by value, in both the passing of a character pointer or the name of the array as above, what actually gets passed is the address of the first element of each array. The numerical value of the parameter passed is the same. This would tend to imply that somehow source[i] is the same as *(source+i).

Indexes are Converted to Pointer Addresses We could write *(i + a) just as easily as *(a + i). But *(i + a) could have come from i[a] ! From all of this comes the curious truth that if: char a[20]; int i; Writing a[3] = ‘x’; is the same as writing 3[a] = ‘x’; ! Ted Jenson’s tutorial on pointers http://pweb.netcom.com/~tjensen/ptr/cpoint.htm

Equivalent Statements dest[i] = source[i]; due to the fact that array indexing and pointer arithmetic yield identical results, we can write this as: *(dest + i) = *(source + i); Also we could write while (*source != '\0’) as while (*source) since the code for ‘\0’ is 0 = false

char myName[40] = “IITM”; Declaring “IITM” char myName[40] = “IITM”; would allocate space for a 40 byte array and put the string in the first 5 bytes char myName[] = “IITM”; the compiler would count the characters, leave room for the nul character and store the total of the 5 characters in the memory location named by myName char *myName = “IITM”; in the pointer notation, the same 5 bytes required, plus 4 bytes to store the pointer variable myName