0% found this document useful (0 votes)
41 views2 pages

Lab Manual 04/27/12 Pointers: Declaration Syntax

The document provides code examples for using pointers in C programs. It includes: 1) A code sample that declares pointers and demonstrates printing the values and addresses of variables and the value pointed to by a pointer. 2) A task that involves declaring variables of different types and printing their addresses and values. 3) A code sample that declares an array and pointer, and uses a for loop to print the values in the array and those pointed to by the pointer at different offsets. 4) A task that involves implementing a sorting algorithm like selection, bubble, or insertion sort using pointers.

Uploaded by

Baderalhussain0
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views2 pages

Lab Manual 04/27/12 Pointers: Declaration Syntax

The document provides code examples for using pointers in C programs. It includes: 1) A code sample that declares pointers and demonstrates printing the values and addresses of variables and the value pointed to by a pointer. 2) A task that involves declaring variables of different types and printing their addresses and values. 3) A code sample that declares an array and pointer, and uses a for loop to print the values in the array and those pointed to by the pointer at different offsets. 4) A task that involves implementing a sorting algorithm like selection, bubble, or insertion sort using pointers.

Uploaded by

Baderalhussain0
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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