2012Week03Lecture3

2012Week03Lecture3 - Memory Management and Manipula1ons...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Memory’Management’and’ Manipula1ons’Using’Pointers’ Principles’of’Programming’ Computer’Engineering’2SH4’ Dr.’T.’E.’Doyle’
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
File’IO’Example’ Lab’2’requires’some’basic’FILE’IO’ Read’&’write’fles’(see’Horton’chapter’12)’ /* File IO example */ #include <stdio.h> #include <stdlib.h> #define OFFSET 1 int main(int argc, char** argv) { FILE *in, //file pointer in file->program *out; //file pointer out program->file char temp; in = fopen("testmein.txt", "r"); //function to return in file pointer out = fopen(“testmeout.txt”, “w”); fscanf(in, "%c", &temp); // assumes >= one item in file do{ printf("%c =\t %d\n", temp, temp); } while (fscanf(in, "%c", &temp) != EOF); fprintf(out, “This is only a test.\n”); close(in); close(out); return (EXIT_SUCCESS); }
Background image of page 2
3’ Pointers’ A’pointer’is’a’variable’that’contains’the’ memory’address’of’a’variable.’ Pointers’are’used’to’express’some’ computa1on’and’to’write’more’compact’and’ efficient’code.’ Careless’use’of’pointers’create’code’that’can’ be’impossible’to’read’and’understand.’
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
4’ Pointers’and’Addresses’ Recall’the’organiza1on’of’computer’ memory’by’looking’at’a’simpliFed’ picture’to’the’right.’ Typically’memory’can’be’considered’ an’array’of’consecu1vely’numbered’ (addressed)’memory’cells.’ The’cells’may’be’manipulated’ individually’or’in’con1guous’groups.’ p: c:
Background image of page 4
5’ Pointers’and’Addresses’(cont±d)’ So’if’ c ’is’a’ char ’and’ p ’is’a’ pointer’of’type’ char ,’then’the’ pointer’ p ’is’poin1ng’to’the’char’ c .’ The’memory’cell’ p ’contains’the’ address’of’the’variable’ c .’ We’declare’a’pointer’ p ,’ char *p; p: c:
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
6’ Pointers’and’Addresses’(cont±d)’ Recall’the’unary’operator’ & ,’that’gives’the’address’of’ an’object:’ & ’only’applies’to’objects’in’memory,’such’as’variables’and’ array’elements.’ & ’cannot’be’applied’to’expressions,’constants,’or’register’ variables.’ So’the’statement:’ p = &c; assigns’the’address’of’ c ’to’the’variable’ p .’ p ’is’said’to’ “point’to”’ c .’
Background image of page 6
7’ Pointers’and’Addresses’(cont±d)’
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 26

2012Week03Lecture3 - Memory Management and Manipula1ons...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online