<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pointers :: CS3 course</title><link>https://robark.gitlab.io/pointers/index.html</link><description>Pointers</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Fri, 10 May 2024 11:10:06 -0700</lastBuildDate><atom:link href="https://robark.gitlab.io/pointers/index.xml" rel="self" type="application/rss+xml"/><item><title>Intro</title><link>https://robark.gitlab.io/pointers/intro/index.html</link><pubDate>Thu, 03 Jun 2021 22:53:15 +0000</pubDate><guid>https://robark.gitlab.io/pointers/intro/index.html</guid><description>Pointers Intro #include &lt;iostream&gt; using namespace std; int main(){ /* //int* p,q,r; //q and r are not pointers int* p; int* q; int* r; int *p, *q, *r; //p, q and r are all pointers */ int a; int *iptr; a = 2; iptr = &amp;a; cout&lt;&lt;a&lt;&lt;endl; //2 cout&lt;&lt;iptr&lt;&lt;endl; //address of iptr *iptr = 6; cout&lt;&lt;a&lt;&lt;endl; //6 return 0; } Passing by reference vs passing pointers (only C++ has pass by ref. Must use pointers in C)</description></item><item><title>Const_arith</title><link>https://robark.gitlab.io/pointers/const_arith/index.html</link><pubDate>Mon, 07 Jun 2021 22:41:59 +0000</pubDate><guid>https://robark.gitlab.io/pointers/const_arith/index.html</guid><description>Constant Pointers and pointer arithmetic Constant pointers
#include &lt;iostream&gt; using namespace std; int main(){ int a(5); int b(6); //const pointer const int *p; // *p=1 illegal but p=&amp;a is legal // can't change value which p points to (p is a movable pointer but it's read only) p=&amp;a; cout &lt;&lt; *p &lt;&lt; endl; p=&amp;b; cout &lt;&lt; *p &lt;&lt; endl; //*p=7; //illegal //pointer const int* const q(&amp;a); // q = &amp;b illegal but *q=1 okay //must initialize q at creation. Can't change location where p points to a=9; cout &lt;&lt; a &lt;&lt;endl; *q=8; //okay cout &lt;&lt; a &lt;&lt; " " &lt;&lt;*q &lt;&lt;endl; //q=&amp;b; // illegal return (0); } //easy way to remember if const comes first it's a movable read only pointer // if it comes after int* then it's a fixed arrow but read/writeable. //const int* const xp(&amp;a) is possible Pointer Arithmetic</description></item><item><title>New_delete</title><link>https://robark.gitlab.io/pointers/new_delete/index.html</link><pubDate>Tue, 08 Jun 2021 23:17:27 +0000</pubDate><guid>https://robark.gitlab.io/pointers/new_delete/index.html</guid><description>Dynamic memory allocation: new and delete #include &lt;iostream&gt; #include &lt;stdio.h&gt; #include &lt;string.h&gt; using namespace std; void leak(){ int* q=new int; *q=1; } int main(){ int* p; p = new int; //allocated on heap (only way possible to reference this newly allocated memory is //through the pointer p. Possible pitfall. Every new requires a corresponding delete *p=5; cout&lt;&lt;p&lt;&lt;endl; cout &lt;&lt; *p &lt;&lt;endl; delete p; p=nullptr; // p=0; cout&lt;&lt;p&lt;&lt;endl; leak(); //memory leak function /* cout &lt;&lt; *p &lt;&lt;endl; //read memory *p=4; //writing to memory cout &lt;&lt; *p &lt;&lt;endl; */ return (0); } // all memory freed on stack</description></item><item><title>Arrays</title><link>https://robark.gitlab.io/pointers/arrays/index.html</link><pubDate>Thu, 10 Jun 2021 23:13:23 +0000</pubDate><guid>https://robark.gitlab.io/pointers/arrays/index.html</guid><description>Pointer Arrays #include &lt;iostream&gt; #include &lt;cstring&gt; using namespace std; void mystrncpy(char* p, string s, unsigned int n){ unsigned int siz; if ( s.size() &lt; n) siz= s.size(); else siz=n; unsigned int x; for (x=0; x&lt;siz; x++) { //*(p+x)=s[x]; p[x]=s[x]; } p[x]='\0'; //null char terminated } int main(){ char* c; c= new char[5]; //mystrncpy(c,"abcde",4); strncpy(c,"abcdef",5-1); //strncpy(c,"abc",5-1); //in line above, can't use sizeof(c) as c is just a pointer cout &lt;&lt;"strlen(c): "&lt;&lt; strlen(c) &lt;&lt;endl; cout&lt;&lt;endl; printf("%s\n", c); cout &lt;&lt; *c &lt;&lt; endl; //first letter cout &lt;&lt; c &lt;&lt; endl; // cstring c delete[] c; return 0; } C code commented</description></item><item><title>Void Func</title><link>https://robark.gitlab.io/pointers/void-func/index.html</link><pubDate>Fri, 11 Jun 2021 23:24:58 +0000</pubDate><guid>https://robark.gitlab.io/pointers/void-func/index.html</guid><description>C string arrays using pointers / struct pointers / void pointers / function pointers C string arrays using pointers and dynamic memory
#include &lt;stdio.h&gt; #include &lt;fstream&gt; // ofstream ifstream #include &lt;iostream&gt; #include &lt;cstring&gt; //#include &lt;algorithm&gt; //count using namespace std; unsigned int filesize(const char* fname){ ifstream f(fname); string dummy; unsigned int lines=0; /* //------------------- while( getline(f, dummy) ) { lines++; } //------------------- */ char c; while ( !f.eof() ) { c = f.get(); if ( c == '\n' ) lines++; } //------------------- //count requires algorithm //lines= count(istreambuf_iterator&lt;char&gt;(f), istreambuf_iterator&lt;char&gt;(), '\n'); //------------------- f.close(); cout&lt;&lt;lines&lt;&lt; " lines in file "&lt;&lt;fname&lt;&lt;endl; return lines; } int main(){ string s; unsigned int c=0; const unsigned int lines = filesize("fruit.txt"); char** v; v = new char* [lines]; ifstream inf("fruit.txt"); while ( getline(inf,s) ) { v[c] = new char [s.size()+1]; // +1 for null byte .c_str() //strncpy(v[c], s.c_str(), s.size()+1); strcpy(v[c], s.c_str()); c++; } cout &lt;&lt; "v = "&lt;&lt;v&lt;&lt;endl; //memory address cout &lt;&lt; "v[0] = "&lt;&lt;v[0]&lt;&lt;endl; // first line cout &lt;&lt; "v[0][0] = "&lt;&lt;v[0][0]&lt;&lt;endl; //first char of first line cout&lt;&lt;c&lt;&lt;endl; for (unsigned int x=0; x&lt;c; x++) { printf("%s\n", v[x]); delete[] v[x]; v[x]=nullptr; } delete[] v; v=nullptr; return 0; } Struct pointers and arrow operator</description></item><item><title>Decay</title><link>https://robark.gitlab.io/pointers/decay/index.html</link><pubDate>Fri, 10 May 2024 11:10:06 -0700</pubDate><guid>https://robark.gitlab.io/pointers/decay/index.html</guid><description>Pointer Decay Here is an example of an array decaying to a pointer when it is passed to a function
#include &lt;iostream&gt; using namespace std; //example of array decaying to a pointer when passed to a function void init(int a[] ){ // array decays to a pointer cout &lt;&lt; "In func init" &lt;&lt;endl; cout &lt;&lt; sizeof(a) &lt;&lt;endl; //8 cout &lt;&lt; sizeof(a[0]) &lt;&lt;endl; //4 size_t s= sizeof(a)/sizeof(a[0]); cout &lt;&lt; s &lt;&lt; endl; //2 // On 64 bit OS's // a pointer is 64 bits (8 bytes) and an int is 32 bits (4 bytes) //for (int x=0; x&lt;s; x++) //cannot iterate over array since size is unknown } //proper way of passing a C array. We must also pass the size void propinit(int *a, size_t s ){ cout &lt;&lt; "in func init2" &lt;&lt;endl; for (size_t x=0; x&lt;s; x++) { a[x]=x; cout &lt;&lt; a[x] ; } cout &lt;&lt; endl; } int main(){ int a[6]; cout &lt;&lt; sizeof(a) &lt;&lt;endl; //24 cout &lt;&lt; sizeof(a[0]) &lt;&lt;endl; //4 size of int size_t s= sizeof(a)/sizeof(a[0]); // 24/4 cout &lt;&lt; s &lt;&lt; endl; //6 init(a); //pass array to function init propinit(a,6); //pass array and the size return 0; }</description></item></channel></rss>