Você está na página 1de 27

POINTERS IN C

C AND DATA STRUCTURE AN INDUSTRIAL PERSPECTIVE

Pointer Variables, Pointer Expressions


Object, lvalue, rvalue A pointer is a memory address Declaring a pointer variable

Data type *variable;

Indirection / Dereferencing operator - * Address of Operator - &

Representation

Simple Representation

Work out
int j, k; k = 2; j = 7; <-- line 1 k = j; <-- line 2 What is a variable? What is a lvalue? What is a rvalue?

[contd]
The address of a variable temp of type float is (A)*temp (B) &temp (C) float& temp (D) float temp&

Pointer Types
int *ptr; char *str; double *dptr;

What is the size of a pointer?

Pointer Assignments, Arithmetic


pointer1 = pointer2, *pointer = val pointer + number, pointer number char *p; char a; char b; p = &a; p += 1; Adds 1*sizeof(char) to the memory address

Work out
What happens in this partial code int int1 = 1036; /* some data to point to */ int int2 = 8; int *int_ptr1 = &int1; /* get addresses of data */ int *int_ptr2 = &int2; int_ptr1 = *int_ptr2; *int_ptr1 = int_ptr2;

Comparisons, Pointers & Arrays


The comparison is valid only between pointers that point to the same array. Under this circumstances, the following relational operators work for pointers operation. ==, !=, >, <, >=, and <=

Array of Pointers
Data type *arrayPtr; Data type *arrayptr[size]; To assign the address of an integer variables called var to the first element of the array, we could write something like this: arrayPtr = &var; arrayptr[0] = &var;

[contd]

An array name without brackets is a pointer to the arrays first element. The arrays name is, therefore a pointer to the arrays first element and therefore to the string if any. A pointer is a constant. It cannot be changed and remains fixed for the duration of program execution. Element of an array are stored in sequential memory locations with the first element in the lowest address. Subsequent array elements, those with an index greater than 0 are stored at higher addresses.

Work out
Consider the following statements char *ptr; ptr = hello; printf(%d, *ptr); What will be printed? (A) first letter (B) entire string (C) it is a syntax error (D) last letter

[contd]
What would be the output of the following? #include<stdio.h> void main() { char *ptr=abcd char ch; ch = ++*ptr++; printf(%c,ch); } (A) a (B) b (C) c (D) d

Dynamically Allocated Arrays


malloc(), calloc() Example int *p, n, i; scanf (%d, &n); p = (int *) malloc (n * sizeof(int));

Methods to allocate space for 2-D array


1. Variable number of rows, fixed number of columns 2. Variable number of columns, but fixed number of rows 3. Both number of rows and columns variable

1. Allocating space for 2-D array n5


use a pointer of type (*q)[5] to allocate space for the array of n rows and 5 columns. int (*q)[5]; q = (int (*)[5]) malloc(n*5*sizeof(int));

2. Allocating space for 2-D array 3m

use a pointer array of size 3, where the ith element of the array will point to the ith column of length m.

Possible to have different number of elements in different rows.

int *r[3], i; for (i=0;i<3;i++) r[i] = (int *) malloc (m*sizeof(int));

3. Dynamic allocation of rc array


allocate a 2-D array of variable number of rows and columns, where both the number of rows and the number of columns as inputs. int **s; s = (int **) malloc (r*sizeof(int *)); for (i=0;i<r;i++) s[i] = (int *) malloc (c*sizeof(int));

Work out
1. Identify the operator that is NOT used with pointers (A) -> (B) & (C) * (D) >>

2. Can you combine the following two statements into one? char *p; p = (char*) malloc(100);

A. char p = *malloc(100); B. char *p = (char) malloc(100); C. char *p = (char*)malloc(100); D. char *p = (char *)(malloc*)(100)

3. What would be the equivalent pointer expression for referring the array element a[i][j][k][l] A. ((((a+i)+j)+k)+l) B. *(*(*(*(a+i)+j)+k)+l) C. (((a+i)+j)+k+l) D. ((a+i)+j+k+l)

4. int one_d[]={1,2,3}; main() { int *ptr; ptr=one_d; ptr+=3; printf("%d",*ptr); }

5. main() { char *p; p="%d\n"; p++; p++; printf(p-2,300); }

6. void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(%d,*mptr); cptr = (int*)calloc(sizeof(int),1); printf(%d,*cptr); }

7. main() { int a=2,*f1,*f2; f1=f2=&a; *f2+=*f2+=a+=2.5; printf("\n%d %d %d",a,*f1,*f2); }

8. #include < stdio.h > int main() { char *str="IncludeHelp"; printf("%c\n",*&*str); return 0; }

Você também pode gostar