constant pointer and character pointer
An array name in programming is interpreted as a pointer to the first element of the array. When used in expressions, it typically evaluates to the address of the first element, allowing access to the entire array through pointer arithmetic. This means that the name of the array does not represent a single value, but rather a reference to a contiguous block of memory where the elements are stored.
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.
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.
Pointers are not structures, non-linear or otherwise. A pointer is a variable that holds a memory address. The primary operation that may be performed upon a pointer is the dereference operator, which allows indirect access to the value stored at the memory address held by the pointer variable. Pointer arithmetic may also be applied to pointers, such that the memory address stored in the pointer may be incremented or decremented in units equal to the size of the pointer's type. Thus a pointer to int where sizeof(int) is 4 would increment or decrement in units of 4 bytes. This pointer arithmetic makes it possible for a pointer to randomly access any element in a contiguous sequence of elements (such as an array) in constant time, thus allowing both linear and non-linear access to those elements. Pointers may also be used to provide links between elements that are stored non-contiguously, thus making it possible to create complex structures such as linked lists, trees, graphs and so on. This is achieved by using a node object to contain the element along with one or more links to other nodes in the sequence, both linearly and non-linearly.
By returning a pointer to the first element of the array.
once we initialize the array variable, the pointer points base address only & it's fixed and constant pointer
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.
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.
To find pairs of numbers in an array that add up to a value less than a given number k, you can use a two-pointer approach. Sort the array first, then use two pointers - one starting from the beginning and the other from the end. Check the sum of the two numbers pointed by the pointers. If the sum is less than k, increment the left pointer to increase the sum. If the sum is greater than or equal to k, decrement the right pointer to decrease the sum. Repeat this process until the pointers meet or the sum is less than k.
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.
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 ...}
Pointers are not structures, non-linear or otherwise. A pointer is a variable that holds a memory address. The primary operation that may be performed upon a pointer is the dereference operator, which allows indirect access to the value stored at the memory address held by the pointer variable. Pointer arithmetic may also be applied to pointers, such that the memory address stored in the pointer may be incremented or decremented in units equal to the size of the pointer's type. Thus a pointer to int where sizeof(int) is 4 would increment or decrement in units of 4 bytes. This pointer arithmetic makes it possible for a pointer to randomly access any element in a contiguous sequence of elements (such as an array) in constant time, thus allowing both linear and non-linear access to those elements. Pointers may also be used to provide links between elements that are stored non-contiguously, thus making it possible to create complex structures such as linked lists, trees, graphs and so on. This is achieved by using a node object to contain the element along with one or more links to other nodes in the sequence, both linearly and non-linearly.
because u freakin can
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.
In the C and C++ languages the array notation arr[i] is completely equivalent to the pointer notation *(arr + i).