notes113

# notes113 - February 18 Pointers Pointers in a 32 bit...

This preview shows pages 1–5. Sign up to view the full content.

February 18 Pointers Pointers in a 32 bit architecture are 4 bytes long, that is a pointer in a 32 bit architecture takes 4 bytes of memory. this amount varies in different types of architectures. Pointer Arithmetic We can do "+" and "-" operation with pointers. These operations scale the pointer to the type of the object to which the pointer points, that is if a pointer is pointing to a char data type then add one to it will increase it by one byte as char takes one byte of memory to be stored, on the other hand if the object to which the pointer is pointing is of int type then adding 1 to the pointer will increment it by 4 bytes as int takes 4 bytes of storage. e.g.: char c[20]; // this declares a character array char *pc; // this statement is used to declare a pointer pc = &c[0]; // this statement assigns the address of the first element of the array c to pc so that pc points to the start of the array c pc 0x1000 0x2000 0x2000 C 65 0 1 2 ! 20

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
0x2000 + 0x0013 = 0x2013 printf("pc = 0x%lx \n", pc); //The following command is used to print the value of a pointer //the above statement will print "pc = 0x2000" on the screen *pc = 'A'; //To store a value in the place where the pointer points to use the command //or you could use *pc = 65; printf("*pc = %c\n", *pc); // This statement is used to print the content of the place where a pointer is pointing // the above statement will print "*pc = A" on the screen Arithmetic commands pc = pc + 5; // this will move the place where pc is pointing by 5 positions pc 0x1000 0x2005 0x2000 0x2001 0x2002 0x2003 0x2004 0x2005
int ai[20]; int *pi; pi 0x3000 0x4000 0x4000 5 0x4004 0x4008 pi = &ai[0] // this statement assigns the address of the first element of the array ai to pc so that pc points to the start of the array ai // the same can be achieved by the following statement pi = ai // this is so as in C arrays can be treated as pointers *pi = 5; // this assigns 5 to the locationwhere pi is pointing to printf("pi = 0x%lx \n", pi); //The following command is used to print the value of a pointer //the above statement will print "pi = 0x4000" on the screen printf("*pi = %d\n", *pi); // This statement is used to print the content of the place where a pointer is pointing // the above statement will print "*pi = 5" on the screen

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
pi = pi + 5; // this will move the place where pc is pointing by 5 positions pi 0x3000 0x4014 0x4000 0x4004 0x4008 0x400C 0x4010 0x4014 0x4000 + 5 * sizeof(int) = 0x4000+5*4 = 0x4000 + 0x 0014
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### Page1 / 14

notes113 - February 18 Pointers Pointers in a 32 bit...

This preview shows document pages 1 - 5. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online