CReviewPart2 - Data Types in C We often want computers to...

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

View Full Document Right Arrow Icon
Data Types in C We often want computers to process large amounts of data, so we need ways to manipulate lists and other groupings of large amounts of data. To satisfy these needs, we require more than just the few basic data types that are built-into the language. We require constructs and methods that allow us to not only manipulate data but also create data abstractions. Three constructors that allow the definition of complex structured types: pointers arrays records 1
Background image of page 1

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

View Full DocumentRight Arrow Icon
Pointers A pointer is simply the internal machine address of a value inside the computer’s memory. int a; int *p; // p is a pointer to int . a = 7; p = &a; a p We can reach the contents of the memory cell addressed by p: printf(“%d\n”, *p); 2 7
Background image of page 2
Fundamental pointer operations 1. Assigning the address of a declared variable: int a, *this, *that; this = &a; 2. Assigning a value to a variable to which a pointer points. *this = 4; assigns 4 to the int variable to which the pointer variable refers. 3. Making one pointer variable refer to another: that = this; 4. Creating a new variable. Given: int *this; int * that; then this = malloc(sizeof(int)); allocates a new memory space for the pointer and makes the pointer variable refer to it. e.g.: 3 this 4 this that 4 this ?
Background image of page 3

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

View Full DocumentRight Arrow Icon
Addressing and Dereferencing int a, b; int *p, *q; a=42; b=163; p = &a; q = &b; printf(“*p = %d, *q = %d\n”, *p, *q); *p = 17; printf(“a = %d\n”, a); p = q; *p = 2 * *p – a; printf(“b = %d\n”, b); 4
Background image of page 4
p = &a; printf(“Enter an integer: “); scanf(“%d”, p); *q = *p; double x, y, *p; p= &x; y = *p; equivalent to y = *&x; or y =x; 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
void SetToZero (int var) { var = 0; } You would make the following call: SetToZero(x); This function has no effect whatever. Instead, pass a pointer: void SetToZero (int *ip) { *ip = 0; } You would make the following call: SetToZero(&x); This is referred to as call-by-reference. 6
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 06/12/2011.

Page1 / 21

CReviewPart2 - Data Types in C We often want computers to...

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

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