Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

LAB MANUAL 04/27/12 POINTERS

Declaration syntax
Check this syntax and use it in your simple program: int* ptr_a, ptr_b; Try this code for getting the concept of basic pointers:
#include <stdio.h> int j, k; int *ptr; int main(void) { j = 1; k = 2; ptr = &k; printf("\n"); printf("j has the value %d and is stored at %p\n", j, (void *)&j); printf("k has the value %d and is stored at %p\n", k, (void *)&k); printf("ptr has the value %p and is stored at %p\n", ptr, (void *)&ptr); printf("The value of the integer pointed to by ptr is %d\n", *ptr); return 0; }

Task 1:
Write a short C program that declares and initializes (to any value you like) a double, an int, and a string. Your program should then print the address of, and value stored in, each of the variables. Use the format string "%p" to print the addresses in hexadecimal notation (base 16). You should see addresses that look something like this: "0xbfe55918". The initial characters "0x" tell you that hexadecimal notation is being used; the remainder of the digits give the address itself.

Task 2:
Use this code for Array to the pointers
#include <stdio.h> int my_array[] = {1,23,17,4,-5,100}; int *ptr; int main(void) { int i;

ptr = &my_array[0];

printf("\n\n"); for (i = 0; i < 6; i++) { printf("my_array[%d] = %d ",i,my_array[i]); printf("ptr + %d = %d\n",i, *(ptr + i)); } return 0; }

/* point our pointer to the first element of the array */

/*<-- A */ /*<-- B */

Task 3:
Try selection sort or bubble sort or selection sort with pointers.

You might also like