Pointers

3 class hours

  • Size of pointers and structures
  • Creating complex data types
  • nullptr

jamboard

Linked list example

#include<cp/cp.hpp>

struct athlete {
  cp::string name;
  athlete* next;
};

cp::string to_string(athlete* an_athlete) {
  return an_athlete->name;
}

int main() {
  athlete nc = {"Neeraj Chopra"};
  cp::println(to_string(&nc));

  athlete pvs = {"P V Sandhu"};
  nc.next = &pvs;

  cp::println(nc.next);
  cp::println(&pvs);

  athlete bp = {"Bajrang Punia"};
  pvs.next = &bp;
  cp::println(to_string(pvs.next));

  athlete* p = &nc;
  while (p != nullptr) {
  // while (p) {
    cp::println(to_string(p));

    p = p->next;
  }

  return 0;
}
Previous
Next