DOC PREVIEW
Rose-Hulman CSSE 332 - Introduction to Pointers in C

This preview shows page 1-2-3-4 out of 12 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
View full document
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 12 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

CSSE 332Introduction to Pointers in C2Pointers made easy?f4300float f; // variable that stores a float?f_addr4304float *f_addr; // pointer variable that stores the address of a floatf_addr = &f; // & = address operator? 4300ff_addr4300 4304NULLDereferencing a pointer3*f_addr = 3.2; // indirection operator or dereferencing ff_addr4300 43043.2 43003.2g4308float g=*f_addr; // indirection: g is now 3.2f = 1.3;ff_addr4300 43041.3 43004Pointer operations Creation– int iVal, *ptr, *iPtr; – ptr = &iVal; // pointer assignment/initialization– iPtr = ptr;  Pointer indirection or dereferencing– iVal = *ptr;  *ptr is the int value pointed to by ptr5#include <stdio.h>int main(int argc, char *argv[]) {int j;int *ptr;/* initialize ptr before using it */ptr=&j; /* *ptr=4 does NOT initialize ptr */*ptr=4; /* j = 4 */j=*ptr+1; /* j = ? */return 0;}Pointer Example 106Pointers and arraysint p[10], *ptr; // Although p represents an array,// both p and ptr are pointers ,// i.e., can hold addresses.// p is already pointing to a fixed location and // cannot be changed. // ptr is still to be initialized.p[i] is an int valuep, &p[i] and (p+i) are addresses or pointers*p is the same as p[0] (They are both int values)*(p+i) is the same as p[i] (They are both int values)How arrays and pointers relateint a[10];a[0]a[1] a[9]a:int *pa;pa = &a[0]; // same as pa = a; a[0]a[1] a[9]a:pa:pa + 1:pa + 5:See below instead78Pointer arithmeticptr = p; // or ptr = &p[0]ptr +=2; // ptr = ptr + 2 * sizeof(int) = ptr+8 bytes=> ptr = &(p[2]);p = ptr; Gives ERROR because “p” is a constant address, points to the beginning of an array and cannot be changed.Summary of Arrays and Pointers In C there is a strong relationship between arrays and pointers Any operation that can be achieved by array subscripting can be done with pointers The pointer version will be faster, in general– A bit harder to understand910Example 11#include <stdio.h>int main(int argc, char *argv[]){int *ptr;/* allocate space to hold an int */ptr = (int*)malloc(4 * sizeof(int)); /* do stuff with the space */*ptr=4; //ptr[0] = 4;/* free up the allocated space */free(ptr);return 0;}Explicit memory allocation Explicit allocation and deallocationPractice exercise Checkout Pointers_Arrays from your svnrepository into your ~/Private/csse332 folder on addiator Study the program in the file pointersAndArrays.c Implement the function printArrayWithPointers() Test it in function main() to ensure that it works // similar to printArray() Commit your work to the svn server11Homework 4 Another exercise on C. Available on class’s Angel page– follow link from the schedule


View Full Document

Rose-Hulman CSSE 332 - Introduction to Pointers in C

Download Introduction to Pointers in C
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Introduction to Pointers in C and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Introduction to Pointers in C 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?