Top Answer
User Avatar
Wiki User
Answered 2012-07-11 08:04:34

constant pointer and character pointer

User Avatar

Your Answer

Related Questions

once we initialize the array variable, the pointer points base address only & it's fixed and constant pointer

Pointer arithmetic simply means incrementing or decrementing the value of a pointer variable. We use pointer variables to store memory addresses, so incrementing or decrement a pointer increments or decrements the stored address. However, unlike integer arithmetic where an integer always increments and decrements in units of 1, the unit of a pointer depends upon the type being pointed to. That is, if a pointer refers to a type that is 8 bytes in length, then incrementing the pointer by 1 (unit) actually increments by 8 (bytes). This is useful when dealing with arrays because we can refer to any element by its offset address from the start of the array. That is, given the array A[N] of type T, we know that A is the start address of the array (array names implicitly decay to pointers, so A and &A are equivalent), thus the Xth element of N can be found at address A+(X-1)*sizeof(T). Given that, we can see that subscripting is really just a convenient shorthand for pointer arithmetic: &A[X-1] is equivalent to A+(X-1)*sizeof(T) Conversely: A[X-1] is equivalent to (A+(X-1)sizeof(T)) Note that we cannot use pointer arithmetic upon pointers of unknown type (void*).

A stack is easily implemented with an array and a top-of-stack marker (TOS), implemented as an array index. At initialization time, the TOS would be initialized to -1 or 0 depending on the implementation: initialize to -1 if the PUSH operation uses pre-increment, or 0 if the PUSH uses post-increment. The POP operation uses the reverse (post-decrement or pre-decrement, respectively). Following is a crude example using pre-increment for PUSH, not including boundary checks: int tos = -1; DataType array[MAX_STACK_SIZE]; void push(DataType v) { array[++tos] = v; } DataType pop() { return array[tos--]; }

Pointer arithmetic simply means performing arithmetic upon memory addresses. Memory addresses are fixed -- we cannot physically change them -- however we can store memory addresses in pointer variables and we can change those addresses using pointer arithmetic. Just as an integer variable stores an integer value in memory, a pointer variable stores a memory address in memory. A memory address is itself an integer value, thus we can increment and decrement that memory address just as we can any other integer: int i = 42; ++i; int* p = &i; ++p; When we increment an integer, that integer is increment by 1 unit, thus the value of i becomes 43. The value of p is also incremented by 1 unit, however that does not mean its address increments by 1 byte, it actually increments by 1 * sizeof (int) bytes. This is because p was declared a pointer to int. The compiler knows this and can therefore work out how many bytes to increment the pointer's value (the address stored in the pointer). In other words, p now points to the start address of the next int in memory. Normally we use pointer arithmetic upon arrays: int x[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; int* p = x + 10; Here, p is pointing at the element immediately after the last element of x (one element past the end of the array). This is because x + 10 increments the address of x by 10 elements of sizeof (int). Note that array names always implicitly decay to a pointer to the first element of the array, thus x + 10 is equivalent to writing &x[0] + 10. Although we must never dereference memory that may not belong to us (as is the case in both examples), a pointer to the one-past-the-end of an array is useful because we can use pointer arithmetic to work out how many elements there are in a given sequence: size_t length = p - &x[4]; Here, length will be 6 because there are 6 elements of sizeof (int) between the address of x[4] and the address stored in p. Note that we must subtract the lower address (&x[4]) from the higher address (p). In C, arrays are always allocated with the first element (index 0) in the lowest address of the array and all higher indices in higher addresses. An element's index is itself an offset memory address from the start of the array, thus when we refer to x[4] we're actually referring to the element at address &x[0] + 4. Just as we can increment memory addresses we can also decrement them. This is useful when we want to navigate to the next or previous element in an array: p = p - 4; Here, p is now referring to the element x[6]. Note that we can perform pointer arithmetic upon a pointer to any type except void. This is because a pointer to void can store the address of any type, but there's no way to determine that type's length. void* v = (void*) x; v += 2; // Error: sizeof (*v) is unknown! Before we can perform pointer arithmetic upon a pointer to void, we must cast it to the correct type: int* i = (int*) v; i += 2; v = (void*) i;

Yes, passing an array name to a pointer assigns the first memory location of the array to the pointer variable. An array name is the same as a pointer to the first location of the array, with the exception that an array name is a r-value, while a pointer is an l-value.

because they use pointer arithmetic. The name of the array refers to the starting memory position of the array so the first element of the array is at offset 0 int array[5]; the first element of array is at (array +0) and has the value *(array+0)

yes.. an array can be accessed through pointer

First take a array that contain all numbers then sort it. Now take a variable i = 0 and j = n-1 where n = size of array. increment i and decrement j till i < j. Now check that if i == j then a[i] is the median else{ increment j by one and decrement i by one. now median = (a[i] + a[j]) >> 1 //here right shifting an integer is same as deviding an integer by 2.

No. All array elements are anonymous (they have no names). The array name refers only to the start address of the array, hence array_name and &array_name are equivalent. To refer to an element you must use the subscript operator [] with a valid zero-based index or use the equivalent pointer arithmetic. Note that the compiler translates all subscripts into the equivalent pointer arithmetic behind the scenes. E.g., a[n] for an array of type T is equivalent to a + (n * sizeof(T)).

There is no such thing as an "array to pointer." What you might be asking is "array of pointers." An array of pointers is just that, an array in which the variables are pointers. In C this would be an array of pointer variables that are each 4 bytes in size. It is declared like this: int *pointers[3]; A pointer to an array is a pointer that points to the whole array. For example, in C if you have int numbers[5][10]; int (*pointerToArray)[10] = numbers + 2; pointerToArray points to the third element of numbers, which is itself an array.

Pointer holds an address Array holds values

A pointer into an array of elements of type E is a pointer to a single element of type E:typedef ..... E;E array[123];E* const pointer = &array[18]; // points to the 19th element inside 'array'An array of pointers is an array whose elements are pointers:typedef .... E;E* array[123];E** const pointer = &array[18]; // points to the 19th pointer within 'array'Referencing the name of the array variable without use of the index operator itself is a constant pointer to its first element. Therefore, the following if-clause is always true:typedef .... E;E array[123];if (array &array[N]) { // ALWAYS true ...}

char c[3]; //array of 3 chars char * p; //pointer of type char p=c; //point the pointer to the base of char array

You point at the array the same way you would with an array of any pointer type, by using an additional level of indirection than is employed by the pointers in the array itself. In this case, the array contains pointer-to-function data types (with one level of indirection), thus you must use a pointer-to-pointer-to-function data type (with two levels of indirection) in order to point at the array itself. Had the array contained pointer-to-pointer-to-function data types (where each pointer points to a separate array of pointer-to-function data types), then you'd use three levels of indirection, and so on. You increase the level of indirection by placing an additional asterisk before the pointer's name when you declare the pointer. That is, one asterisk per level.

A pointer is a variable like any other. As such, it has memory allocated to it. It is similar to an int in that they both store a value in their memory location. However, whereas an int value is interpreted as a whole number, a pointer's value is interpreted as a memory address. The pointer is said to "point to" the memory address it contains. The type of the pointer determines the type of the variable stored at that memory address. the following is a declaration of a pointer to an integer initialised with the value zero: int * p = new int(0); A pointer to a pointer is no different to an ordinary pointer, except the memory address it points to is that of another pointer. The following is an example of a pointer that points to the pointer we've just declared. int ** pp = p; Pointer-to-pointer variables are typically used whenever you need to pass a pointer by reference. All pointers are passed by value, so the memory address they contain is passed, not the pointer variable itself. To pass the pointer itself, you must pass a pointer to the pointer, which passes the memory address of the pointer. This then makes it possible to change the memory address stored in that pointer. Pointer-to-pointer variables are also used when declaring dynamic, multi-dimensional arrays. For instance, a 2-dimensional array requires a pointer-to-pointer to reference the array itself. This pointer points to a 1-dimensional array of pointers, each of which points to a 1-dimensional array of the actual variables in the array. Unlike a static multi-dimensional array, where the entire array occupies contiguous memory, a dynamic multi-dimensional array may not reside in contiguous memory. Pointer-to-pointer can be extended further to accommodate 3-dimensional arrays, and above. A 3D array requires a pointer-to-pointer-to-pointer, which points to a 1-dimensional array of pointer-to-pointer, each of which references a 2D array, as previously outlined.

The Advantages of using an array: 1. Static and compile time memory allocation 2. Contigious Memory allocation 3. Random access is possible based on Index 4. Gives the base address when array name is specified and hence easy to pass by pointer Disadvantages: 1. Run time memory increment or decrement is not possible so if not used properly then it can lead to memory wastage or to out of bound access 2. Run time no boundry check mechanism is provided

You cannot uses indices instead of subscripts. The subscript operator [] requires an index in order to determine the subscript. Even if you don't use the subscript operator you still need an index to determine the offset of the subscript. Indeed, the only time you do not need an index is when traversing the array using a roving pointer, which is arguably more efficient than using a subscript to traverse an array since subscripts use multiplication instead of the much simpler increment/decrement operation.

It is inappropriate and non-portable to consider the address or the relative address of a particular item in an array, because different compiler implementations might be different. Even within the same implementation, there might be issues due to alignment or segmentation. That said, it is perfectly legal and defined to assign a pointer to the address of an element and then perform arithmetic on the pointer, with consistent results. You should not, however, consider the "arithmetic" value of the pointer to have any particular meaning, especially since the concept of "adding one" to such a pointer means to add by the number of bytes in a single element, not (as thought) to add by one. Such "pointer arithmetic" is not advised (or defined ??) when you step beyond the bounds of any one sub-array, again, due to possible alignment issues. Consider your example of an array of char [200][150][50]. You can theoretically think of the memory layout as simply being sequential, with the left-most index varying quickest, and that might work 99.99% of the time. I go for the 100% case and treat the pointer as an opaque object, with no consideration of its internal representation.

By returning a pointer to the first element of the array.

Array name is a static pointer to the array.Meaning of static here is that we can not modify the pointer (array name) value to any other value. Array name is the base address of the array in memory. for example if we have array a[5] of float starting at address 1000 in memory then a tells us the base address of array or address of a[0] i.e. 1000. a+1 tells the address of element a[1] and so on. we can access the element of an array by considering the array name as pointer. *(a+2) will access the third element of an array a i.e. a[3]. but we can't achieve it by applying ++ operator 3 times since the array pointer can't be modified.

An array of pointers is that for eg if we have array of 10 int pointers ie int *a[10] then each element that which is stored in array are pointed by pointers. here we will have ten pointers. In pointer to an array for eg int(*a)[10] here all the elements that is all the ten elements are pointed by a single pointer.

Yes. More specifically, they can be used to represent a dynamic multi-dimensional array. As most people know, for a one-dimensional dynamic array, you simply need a pointer to the first element in the array, where each element contains an object of the same type as the pointer. The pointer can be passed around functions just as if it were a static array, the only difference being the requirement to pass the upper bound of the array as well as the array itself. For a two-dimensional array, you need a pointer-to-pointer which points to the first element of a one-dimensional pointer array, where each pointer in that array points to a one-dimensional array of objects. The pointer-to-pointer must be the same type as the pointers and the objects. For a three-dimensional array you need a pointer-to-pointer-to-pointer. And so on. Each additional dimension simply adds a new level of indirection, and a new level of one-dimensional pointer arrays. Of course multi-dimensional arrays are only useful if every dimension is fully utilised and doesn't require too much in the way of resizing. If that is not the case, then you may get more efficient memory consumption from a vector of vectors (of vectors), which allows dynamic resizing at every level without having to copy existing elements. The downside is you lose the random access provided by the array.

pointer is use to hold address of another variable whereas array is a collection of elements of similar datatype

--> Array is a collection of same kind of data.--> Pointer is a memory location where the data stored in the memory

In the C and C++ languages the array notation arr[i] is completely equivalent to the pointer notation *(arr + i).