- (*ptoStruct).member; is same as ptoStruct->member
- char name[40] = “mithun”, allocate 40 bytes on static memory block, and occupy the first 7 bytes (6 + ‘\0’), and “name” is a array constant, un modifiable lvalue
- char name[] = “mithun” , count number of chars, allocate memory for count+1 on static memory block, and “name” is a array constant un modifiable lvalue
- char* name = “mithun” , allocate the memory for number of characters + 1 for nul, and also 4 bytes for the pointer variable “name”, and “name” is a variable
- in the array notation name is the address of the first element i.e. &name[0], and will always point to the same location (a constant).
- which one to use?
- if you are modifying the contents (change the data at &name[0] for example), in the program then use array
- if you want “name” to point to some other memory location during the program then use pointer variable
- if you are modifying the contents (change the data at &name[0] for example), in the program then use array
- the important point is what is considered as data is modifiable, in case of array the data is what is stored at the array location, and in pointer the pointer itself is the data
- char multi[2][4] = {{‘m’,’i’,’t’,’h’},{‘u’,’n’,’_’,’_’}}, means array of 2 arrays of 4 character each, as shown below, but in memory stored as contiguous and &multi[0][0] having the address of ‘m’ in the characters of the mithun__
R/C 0 1 2 3 0 m(00) i(01) t(02) h(03) 1 u(10) n(11) _(12) _(13)
- now we can access the elements of array by
*(*(mlti + row)+col) so *(*(multi + 1) + 1) is the value ‘n’, second row second column and *multi + 1 is ‘u’
multi[row][col]
- because of the double de referencing it is sometimes called a pointer to pointer which is misleading as multi[0][0] is a constant, and pointer is a variable.
- evaluation of multi[1][1] to address of n
- the starting address of the array is multi
- the size of each element of array is 1 byte (sizeof char)
- the number of columns
- the row and the col ie [1] and [1] in this case
- so the address of multi[1][1] is (using pointer arithmetic)
- starting address + ( size of element * (row * number of columns + col) ) ie starting address + (1 * (1 * 4 + 1))
- starting address + ( size of element * (row * number of columns + col) ) ie starting address + (1 * (1 * 4 + 1))
- the starting address of the array is multi
- for an int array[4][5], the address of array[3][2] is starting address + ( 4 * (3 * 5 + 2)), size of int is 4 bytes
- for and int array[40][50][60], the address of array[3][4][5] is starting address + ( 4 * ((3 * 50 * 60)+(4 * 60)+5)), where 50 is second index and 60 is third index, so the first index 40, is not used by the compiler to generate the address
- for multi dimensional array all dimensions of higher order than one are needed for calculating the address
- int (*pArr)[10], is a pointer to array, and int *pArr[10] is a array of 10 pointers to int
- in case of an ANSI compliant compiler malloc() will return a void* if the memory is allocated or NULL if failed
- dynamic allocation
- for one dimensional array of size ‘s’ int *iptr = malloc(s * sizeof(int));
- for 2d array
- for one dimensional array of size ‘s’ int *iptr = malloc(s * sizeof(int));
Tuesday, September 14, 2010
Pointers to pointers, and arrays and strings II
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment