Tuesday, September 14, 2010

Pointers to pointers, and arrays and strings II

  1. (*ptoStruct).member; is same as ptoStruct->member
  2. 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
  3. 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
  4. 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
  5. 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).
  6. which one to use?

    1. if you are modifying the contents (change the data at &name[0] for example), in the program then use array
    2. if you want “name” to point to some other memory location during the program then use pointer variable
  7. 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
  8. 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/C0123
    0m(00)i(01)t(02)h(03)
    1u(10)n(11)_(12)_(13)

  9. now we can access the elements of array by


    1. *(*(mlti + row)+col) so *(*(multi + 1) + 1) is the value ‘n’, second row second column and *multi + 1 is ‘u’

    2. multi[row][col]
  10. 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.
  11. evaluation of multi[1][1] to address of n

    1. the starting address of the array is multi
    2. the size of each element of array is 1 byte (sizeof char)
    3. the number of columns
    4. the row and the col ie [1] and [1] in this case
    5. so the address of multi[1][1] is (using pointer arithmetic)

      1. starting address + ( size of element * (row * number of columns + col) ) ie starting address + (1 * (1 * 4 + 1))
  12. 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
  13. 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
  14. for multi dimensional array all dimensions of higher order than one are needed for calculating the address
  15. int (*pArr)[10], is a pointer to array, and int *pArr[10] is a array of 10 pointers to int
  16. in case of an ANSI compliant compiler malloc() will return a void* if the memory is allocated or NULL if failed
  17. dynamic allocation

    1. for one dimensional array of size ‘s’ int *iptr = malloc(s * sizeof(int));
    2. for 2d array


No comments: