20-CS-122-001 Computer Science II Spring 2012
Simple Data Structure - a struct

Virtual functions, classes, inheritance, lists, queues, stacks, applications

Prev      Next      Lectures      Source      Power Point

#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;

typedef struct {  char string[128]; } String;

int main() {
   String *a;
   String *b = new String();
   String c;
   String d;
   
   strcpy(b->string, "Hello There");
   
   a = b;
   cout << "Value of a: " << a->string << "\n";
   
   strcpy(b->string, "New String");
   cout << "Value of a: " << a->string << "  b: " << b->string << "\n";
   
   strcpy(d.string, "Hello There");
   c = d;
   cout << "Value of c: " << c.string << "  d: " << d.string << "\n";

   strcpy(c.string, "Different String");
   c = d;

   strcpy(d.string, "New String");
   cout << "Value of c: " << c.string << "  d: " << d.string << "\n";
}

// Sample Output:
//   Value of a: Hello There
//   Value of a: New String  b: New String
//   Value of c: Hello There  d: Hello There
//   Value of c: Hello There  d: New String