C++ Learning (2)

Strings

2 types are supported.
- C style char string (the last char will automatically be ‘\0’)

char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
char greeting[] = "Hello";
  • C++ library String

Pointers

* is not *

Note that the asterisk (*) used when declaring a pointer only means that it is a pointer, and should not be confused with the dereference operator.

myvar = 25;
foo = &myvar; // address 0f 
//These are ture:
myvar == 25;
&myvar == 1776;
foo == 1776;
*foo == 25;

A program test if you understand.

// my first pointer
#include <iostream>
using namespace std;

int main ()
{
  int firstvalue, secondvalue;
  int * mypointer;

  mypointer = &firstvalue;
  *mypointer = 10;
  mypointer = &secondvalue;
  *mypointer = 20;
  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
/*
    OUTPUT:
    firstvalue is 10
    secondvalue is 20
*/

Here is a even more elaborate example:

#include <iostream>
using namespace std;

int main ()
{
  int firstvalue = 5, secondvalue = 15;
  int * p1, * p2;

  p1 = &firstvalue;  
  p2 = &secondvalue; 
  *p1 = 10;          
  *p2 = *p1;   
  p1 = p2;     
  *p1 = 20;

  cout << "firstvalue is " << firstvalue << '\n';
  cout << "secondvalue is " << secondvalue << '\n';
  return 0;
}
int    *ip;    // pointer to an integer
double *dp;    // pointer to a double
float  *fp;    // pointer to a float
char   *ch     // pointer to character

The actual data type of the value of all pointers, whether integer, float, character, or otherwise, is the sam**e, a long hexadecimal number that represents a memory address. The only difference between pointers of different data types is the data type of the variable or constant that the pointer **points to.

Important operations

(a) we define a pointer variables. (b) assign the address of a variable to a pointer (c) finally access the value at the address available in the pointer variable.

NULL pointer:

It is always a good practice to assign the pointer NULL to a pointer variable in case you do not have exact address to be assigned.

int  *ptr = NULL;
Pointer arithmetic

There are four arithmetic operators that can be used on pointers: ++, –, +, and –.

let us consider that ptr is an integer pointer which points to the address 1000. Assuming 32-bit integers, let us perform the following arithmetic operation on the pointer: ptr++, the ptr will point to the location 1004 because each time ptr is incremented, it will point to the next integer. If ptr points to a character whose address is 1000, then above operation will point to the location 1001 because next character will be available at 1001.

Incrementing a Pointer example:

#include <iostream>

using namespace std;
const int MAX = 3;

int main (){
   int  var[MAX] = {10, 100, 200};
   int  *ptr;

   // let us have array address in pointer.
   ptr = var;
   for (int i = 0; i < MAX; i++){
      cout << "Address of var[" << i << "] = ";
      cout << ptr << endl;

      cout << "Value of var[" << i << "] = ";
      cout << *ptr << endl;

      // point to the next location
      ptr++;
   }
   return 0;
}

output on 32bit machine

Address of var[0] = 0xbfa088b0
Value of var[0] = 10
Address of var[1] = 0xbfa088b4
Value of var[1] = 100
Address of var[2] = 0xbfa088b8
Value of var[2] = 200

Decrementing a Pointer: ptr = &var[MAX-1];

#include <iostream>

using namespace std;
const int MAX = 3;

int main (){
   int  var[MAX] = {10, 100, 200};
   int  *ptr;

   // let us have address of the last element in pointer.
   ptr = &var[MAX-1];
   for (int i = MAX; i > 0; i--){
      cout << "Address of var[" << i << "] = ";
      cout << ptr << endl;

      cout << "Value of var[" << i << "] = ";
      cout << *ptr << endl;
      // point to the previous location
      ptr--;
   }
   return 0;
}
Pointer Comparisons

Pointers may be compared by using relational operators, such as ==, <, and >. If p1 and p2 point to variables that are related to each other, such as elements of the same array, then p1 and p2 can be meaningfully compared.

Pointer and Array
int  var[MAX] = {10, 100, 200};
*var = i; // this is equivalent to var[0] = i
var++;       /* This is incorrect because var is a constant that points to the beginning of an array. */

Because an array name generates a pointer constant, it can still be used in pointer-style expressions, as long as it is not modified. *(var + 2) = 500; is equal to var[2] = 500;

Array of pointers
#include <iostream>

using namespace std;
const int MAX = 3;

int main ()
{
   int  var[MAX] = {10, 100, 200};
   int *ptr[MAX]; // array of integer pointers

   for (int i = 0; i < MAX; i++)
   {
      ptr[i] = &var[i]; // assign the address of integer.
   }
   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of var[" << i << "] = ";
      cout << *ptr[i] << endl; // use pointer to output.
   }
   return 0;
}

Use an array of pointers to character to store a list of strings.

#include <iostream>

using namespace std;
const int MAX = 4;

int main ()
{
   char *names[MAX] = {
                   "Zara Ali",
                   "Hina Ali",
                   "Nuha Ali",
                   "Sara Ali",
   };

   for (int i = 0; i < MAX; i++)
   {
      cout << "Value of names[" << i << "] = ";
      cout << names[i] << endl;
   }
   return 0;
}
Pointer’s pointer (Multiple Direction)

A variable that is a pointer to a pointer must be declared as such. This is done by placing an additional asterisk in front of its name. int **var;

#include <iostream>
using namespace std;
int main ()
{
   int  var;
   int  *ptr;
   int  **pptr;
   var = 3000;
   // take the address of var
   ptr = &var;
   // take the address of ptr using address of operator &
   pptr = &ptr;
   // take the value using pptr
   cout << "Value of var :" << var << endl;
   cout << "Value available at *ptr :" << *ptr << endl;
   cout << "Value available at **pptr :" << **pptr << endl;
   return 0;
}
passing pointer to function
int main ()
{
   unsigned long sec;
   getSeconds( &sec ); //passing address to function.
   // print the actual value
   cout << "Number of seconds :" << sec << endl;
   return 0;
}

void getSeconds(unsigned long *par){
//...
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值