Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Pointers and Pointer to Pointers in C, Exercises of Computer Programming

The concept of pointers and pointer to pointers in c programming language. It includes examples of defining and manipulating pointers to strings and integers, as well as a lab task to write a program that copies one string to another using pointers and a program to swap the addresses stored in two pointers using pointer to pointer.

Typology: Exercises

2011/2012

Uploaded on 07/31/2012

dhairya
dhairya 🇮🇳

5

(4)

31 documents

Partial preview of the text

Download Pointers and Pointer to Pointers in C and more Exercises Computer Programming in PDF only on Docsity! Pointers and C-type Strings  Pointer notation can be applied to the characters in strings, just as it can to the elements of any array. docsity.com #include <iostream> using namespace std; int main() { char str1[ ] = “Defined as an array”; char* str2 = “Defined as a pointer”; cout << str1 << endl; cout << str2 << endl; //str1++; str2++; cout << str2 << endl; return 0; } docsity.com LAB TASK  Write a program that copies one string to another using pointers docsity.com Pointer to Pointer  We can have pointers to int, and pointers to char and in fact pointers to any type in C, it shouldn't come as too much of a surprise that we can have pointers to other pointers.  If we're used to thinking about simple pointers, and to keeping clear in our minds the distinction between the pointer itself and what it points to, we should be able to think about pointers to pointers, too, although we'll now have to distinguish between the pointer, what it points to, and what the pointer that it points to points to. docsity.com Pointer to Pointer  Of course, we might also end up with pointers to pointers to pointers, or pointers to pointers to pointers to pointers, although there is a minimum chance that they would be of any practical use docsity.com Pointer to Pointer  If we say  *ipp = ip2;  we've changed the pointer pointed to by ipp (that is, ip1) to contain a copy of ip2, so that it (ip1) now points at j: docsity.com Pointer to Pointer  If we say  *ipp = &k;  we've changed the pointer pointed to by ipp (that is, ip1 again) to point to k: docsity.com Pointer to Pointer  What are pointers to pointers good for, in practice? One use is returning pointers from functions, via pointer arguments rather than as the formal return value. docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved