Escolar Documentos
Profissional Documentos
Cultura Documentos
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;
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;
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
use a pointer array of size 3, where the ith element of the array will point to the ith column of length m.
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)
6. void main() { int *mptr, *cptr; mptr = (int*)malloc(sizeof(int)); printf(%d,*mptr); cptr = (int*)calloc(sizeof(int),1); printf(%d,*cptr); }
8. #include < stdio.h > int main() { char *str="IncludeHelp"; printf("%c\n",*&*str); return 0; }