CReviewPart2 - Data Types in C Pointers We often want...

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

View Full Document Right Arrow Icon
1 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 2 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); 7
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
3 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.: this 4 this that 4 this ? 4 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);
Background image of page 2
5 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; 6 Passing parameters by reference 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.
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
7 Arrays & An array is a sequence of data items that are of the same type that can be indexed and that are stored contiguously. & Arrays are a data type that is used to represent a large
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 11

CReviewPart2 - Data Types in C Pointers We often want...

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

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