Getting Started

(lily) #1

Chapter 8: C Pointers and Arrays


ht, ‘y’ the 4th element of the howdy array. If it

pointer
char c; // create a char variable;

confuseus to point to the howdy array;
// set it to point to howdy[4]

lear?
kay, w

1 now equals ‘o’ and c2 equals ‘i’.

e can express the array position using pointers:

usmore;

c1 = howdy[i]; // c1 = ‘y’ using array notation


confuseus = &howdy[0];
confuseus += 4;
c = *confuseus;


No tricks, what does c equal? Rig
wasn’t clear, try it with comments:


char *confuseus; // create a char

confuseus = &howdy[0]; //set
confuseus += 4;
c = *confuseus; //set the contents of c to the contents of howdy[4]

C
O hat about:


char c1, c2;


c1 = (confuseus + 1);
c2 =
confuseus + 1;


c


Groan.


For c1 we added 1 to the address of confuseus before we dereferenced it with the
indirection operator. For c2 we dereferenced it, making it equal to ‘h’ then we
added 1 to ‘h’ NOT the address, making it equal the char numerically following
‘h’, which is ‘i’.


Double groan.


W


int i = 4;
char c1,c2;
char* confuse

Free download pdf