Pointers & Arrays

In the Name of Most High
Session 7 : Pointer & Arrays (continued)
Multidimensional Arrays
int a[2][3] = {{1,2,3},{4,5,6}};
int a[2][3] = {{1,2},{4,5}};
int a[2][3] = {1,2,3,4};
a == &a[0]
a[0] == &a[0][0]
*a == a[0]
**a == *(a[0]) ==
a[0][0]
void write_table(int T[][3], int row_num)
{
// int T[2][3] is OK (2 is ignored)
// int T[][] is invalid
// int (*T)[3] is OK too
for (int k = 0; k < row_num; k++)
{
for (int j =0; j < 3; j++)
cout << T[k][j] << ‘ ‘;
cout << endl;
}
}
Pointer to void
Pointer to any type of object can be assigned to void * that can be explicitly converted
to another type. void* can be compared for equality or inequality. other operation would
be unsafe(e.g. dereference operator *).Usage example: malloc function in C.
void
{
f(int *pi)
void pv = pi;
// ok: implicit conversion of int to void *
*pv;
// error: can not dereference void *
pv++;// error: can not increment void* (size of the object pointed to is unknown)
// explicit conversion back into int*
int* pi2 = static_cast<int*>(pv);
double* pd1 = pv;
//error
double* pd2 = pi;
// error
double* pd3 = static_cast<double>(pv);
//unsafe
}
Dynamic memory Allocation for pointers
//C++ style: new and delete operators
int *a;
a = new int;
// memory allocation for one integer
*a = 10;
delete a;
a = new int[1000];
a[10] = 578;
delete [] a;
// memory allocation for 1000 integer
// Old style of C is now obsolete: malloc() and free() functions
#include <stdlib.h>
...
int *a;
a = (int*)malloc(sizeof(int)*1000);
// memory allocation for 1000 integer
if(!a)
{
*(a + 10) = 578;
free(a);
}
Advanced Programming
Instructor: Khansari
1
Character Pointer and String Literal
char s1[] = “This is a string”;
// string literal is an array of const char
char alpha[] = {‘a’, ‘l’, ’i’, 0}; // alpha == “ali”
sizeof alpha ==4
// Terminated with null char (‘\0’) with value 0
void f()
{
char *s1 = “Place”;
char s2[] = “Place”
s1[4] = ‘a’;
s2[4] = ‘a’;
}
//
//
//
//
allowing assignment of string literal to char *
s2 is array of 5 char
error: assignment to const; result is undefined
OK
void f1(char v[])
{
for(int j=0; v[j]!=0; j++) use(v[j]);
}
void f2(char v[])
{
for(char *p=v; *p!=0; p++) use(*p);
}
void CopyStr1(char*p, const char*q)
{
for(int j=0; q[j]!= 0; j++) p[j] = q[j];
p[j] = 0
//terminating zero
}
void CopyStr(char*p, const char*q) //Try
{
while(*p++ = *q++);
}
char * strcpy(char p, const char q);
to use standard
string copy instead of this!!
// copy from q into p
//Array of char pointer initialization: Storing strings with variable length
char *suit[] = {“Hearts”, “Diamonds”, “Clu\
// use \ to join the lines
bs”, “Spades};
suit[0]
suit[1]
’H’ ’e’ ’a’ ’r’ ’t’ ’s’
’D’ ’i’ ’a’ ’m’ ’o’ ’n’ ’d’ ’s’
suit[2]
’C’ ’l’ ’u’ ’b’ ’s’
suit[3]
’S’ ’p’ ’a’ ’d’ ’e’ ’s’
char **p = names;
for(int j=0;j <=3; j++)
cout << suit[j] << endl;
// or cout << *p++ <<endl;
// we can’t use *suit++; suit can’t be modified
what is the difference between a and b (access, allocation, flexibility)?
char a[10][50];
Advanced Programming
Instructor: Khansari
2
char *b[10];
•
•
•
•
Access to elements in a and b is similar: a[2][44], b[2][44]
a is true array: all 500 cells are allocated while b has 10 pointers to
char.
In b the strings are not in the array and should be allocated separately.
a has a fixed size, but strings in b can be of any size
Command-Line argument
#include <iostream>
char* save_string(const char*p)
{
char *s = new char[strlen(p) + 1];
strcpy(s,p);
//copy from p to s
retrun s;
}
void main(int argc, char* argv[])
{
if(argc < 2)
{
exit(1);
cout << “Use the program with one parameter!”
}
char *p = save_string(argv[1]);
cout << argv[0] << endl;
// echo program name and the path
while (--argc > 0)
//echo arguments; argv[0] is program name and path
cout << *++argv << endl;
...
delete [] p;
}
Advanced Programming
Instructor: Khansari
3