Job Training and Career Qualifications

Why can't we increment an array like a pointer?

Top Answer
User Avatar
Wiki User
Answered 2009-06-16 11:36:43

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

and constant pointer

User Avatar

Your Answer

Related Questions

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.

the simple and efficient way to pass an array is pointer to an array like that int (*p)[30] ; // pointer to an array of integer having 30 element

An array behaves like a pointer when you use its name in an expression without the a[10]; /* a array of 10 ints */int *b = a; /* a reference to a as a pointer, making b like a */int c = *(a+3); /* a reference to a[3] using pointer semantics */myfunc(a); /* pass a's address, a pointer to myfunc */Note very carefully that, while an array name and a pointer can almost always be interchanged in context, the are not the same, in that a pointer is an l-value, such as b, above, and can be assigned, whereas a is an r-value and can only be referenced, such as in the same statement, the second statement. Also, an array name does not take up memory, while a pointer does.

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.

A pointer is normally considered an atomic data type. Therefore, a pointer cannot "be indexed like an array" in that it is not possible to access portions of a pointer through an index. However, a pointer can point to data which can be accessed through pointer operations as well as through index operations. Further, multiple pointers can be arranged in an array of pointers, and each individual pointer within that array of pointers can be reached through its array index. Example 1: an array of pointers const char* const array_of_pointers[] = { "Hello, World", "Hello, Venus", "Hello, Mars" }; In this example, each of array_of_pointers[0], array_of_pointers[1] or array_of_pointers[2] evaluates to one pointer, selected with the array index 0..2. Example 2: access to data through pointer using an array-style index void example(char* const text) { *text = 'a'; // set first character pointed to to 'a' *(text+1) = 'b'; // second char becomes 'b' // the following are equivalent to the above, but use index notation: text[0] = 'a'; text[1] = 'b'; } Key to example 2 is the declaration of the pointer as a constant pointer.

Because of pointers and that all arrays are really pointers. A pointer something like *pointer can also be written as pointer[0] and *(pointer + 1) can also be written as pointer[1]

Either put some array terminator as the last element (like '\0') or return the length of the array as an output from user defined function.

An array never behaves like a pointer.Understand that a pointer is a variable -- no different to any other variable, other than its type. like any other variable, it is allocated its own memory (4 bytes in a 32-bit system). Those 4 bytes can store any 32-bit value, from 0x00000000 to 0xFFFFFFFF, but because it is declared to be a pointer, that value actually represents a memory location, somewhere within the 4GB address space. 0x00000000 is a reserved memory location -- what we refer to as NULL.An array is not the same as a pointer in any sense. The array name refers to the actual memory (the starting address) where the array resides. But the array name is not a variable -- unlike a pointer, it is not separate from the memory it refers to -- it is merely an alias that refers to the memory allocated to the array itself.That said, pointers and arrays can appear to be the same. For instance, the strlen() function expects you to pass a const char pointer, and yet you can pass an array name instead and it works just fine:char cArray[12];char * p = cArray;strlen( p ); // Pass a pointerstrlen( &cArray[0] ); // Pass pointer to array element via AddressOf operatorstrlen( cArray ); // Pass an array nameAll three of these functions work exactly the same so it would be easy to assume the array is a type of pointer. But it is not. To understand what's really going on here you have to understand how pointers and arrays are actually passed to function.When you pass a pointer to a function, you don't pass the pointer itself, you pass the memory address stored in the memory allocated to the pointer. In other words, pointers are passed by value, not by reference. Similarly, when you pass an array name, you pass the starting address of the array.That's all there is to it! The function's parameter, a pointer, is allocated its own memory. It is a temporary variable, local to the function. All it expects is a memory location so whether you pass a pointer or an array name, that's exactly what it receives.

A one dimensional array is a scalar value repeated one or more times.A two dimensional array is an array of one dimensional arrays.A three dimensional array is an array of two dimensional arrays, and so forth.The one dimensional array is like a list of things, where the two dimensional array is like an array of things. (Think one row of a spreadsheet versus the whole spreadsheet.)[addendum]Every level of array depth is also a level of pointer depth. For example: A 3 dimensional int array is an int***. So a one dimensional int array is an int*, and a two dimensional int array is an int**. This is only important if you are doing pointer work, but it can become very important.

An array of pointers is exactly what is says on the tin - an array which contains pointers to memory addresses.If you were to create an array of ten integers like this:int array[10];This would create an array which you could directly access from the stack (memory allocated to the program by the Operating System).However, if you wanted to create an array of pointers to integers, allowing for a more efficient use of memory, you could do this:int *array = new int[10];This dynamically allocates an array of 10 pointers to integers.Comment:This example is hardly an efficient use of memory. Not only are you declaring 10 integers, you are declaring a pointer, which uses slightly more memory than would otherwise be required by the first example. An array of pointers is more useful when the memory they point to is non-contiguous and the number of elements is dynamic rather than static. Even dynamic multi-dimensional arrays can be allocated in contiguous space and simple pointer arithmetic can be used to access any element, thus only one pointer is required for the entire array rather than separate arrays of pointers and pointer-to-pointer arrays for each dimension. While pointer arrays can often improve performance, it is always at the expense of memory consumption.

The '+=' operator behaves like a pre increment operator.

In some programming languages, like C, you can pass the new method (or function) an address pointer to the first element in the array. As long as you don't leave the scope of the method the array was created in, the array will remain valid. In other languages that don't support memory addresses, like FORTRAN, it must be done by making the array global.

They both mean the same thing; an array is a type of data structure (a linear structure). A pointer variable is just a variable like any other, but one that is used to specifically store a memory address. That memory address may contain a primitive data type, an array or other data structure, an object or a function. The type of the pointer determines how the data being pointed at is to be treated. Pointers must always be initialised before they are accessed, and those that are not specifically pointing at any reference should always be zeroed or nullified with the NULL value. This ensures that any non-NULL pointer is pointing at something valid. Remember that pointer variables are no different to any other variable insofar as they occupy memory of their own, and can therefore point to other pointer variables.

There is no such increment operator in C language to increment the value of a variable by 2.An increment operator only increments the value by 1. however you can apply the increment operator twice to get an increment of 3. No: you cannot: ++(++a) won't compile. Yes. Example: a += 2; but += is not an increment operator, it's a shorthand of a=a+2; just like a++ is a shorthand for a= a+1

A pointer is a primitive variable that can be used to store a memory address. We say the variable "points to" the stored address, hence we call them pointers. The primary operator of a pointer is the dereference operator (prefix *) which allows us to access the value stored at the address being pointed at. Being a variable, pointers also have an address of their own, thus we can use a pointer to point at another pointer.Like all variables, pointers must be declared with a type although we can use void* if the type cannot be established at compile time. When dereferencing a void* pointer, we must cast the pointer to the appropriate type.Unlike a named variable which always refers to the same memory address, a pointer can refer to any memory address and we can change that address at any time unless the pointer is declared a constant pointer (in which case it must be initialised at the point of declaration). We can also change the value being pointed at unless the pointer is declared a pointer to constant. Thus for any given type, we have four possible pointer declarations:pointer to variable.pointer to constant.constant pointer to variable.constant pointer to constant.Before we can use a pointer we must first assign an address to it and thus initialise it. If we have no suitable address to initialise the pointer, we must assign the all-zeroes bit pattern. On most implementations, the NULL macro is defined to match the all-zeroes bit pattern of the system, which is typically 32 bits in length on a 32-bit system and 64 bits on a 64-bit system.As soon as the memory being pointed at falls from scope, we must assign NULL to the pointer. This ensures that we don't accidently attempt to dereference memory that is no longer valid (may have been overwritten) or has been released back to the system (no longer belongs to our program). Dereferencing a NULL pointer results in undefined behaviour.Pointers are primarily used to allow the call by reference semantic. Unlike C++, C has no built-in reference type thus all arguments are passed to functions by value. However, by passing a pointer (by value) we are effectively passing by reference because the value of a pointer is a memory address.Pointers also have an important role to play with regards arrays, especially variable-length arrays allocated dynamically on the heap (the free store). Whenever we allocate an array, we are setting aside a chunk of memory for one or more elements of a given type. If the array is allocated in static memory or on the stack (local memory), we can name the array just as we can an ordinary variable because the length of the array is known at compile time (fixed-length arrays). Thus the array name refers to the start address of the allocation. But if the array is allocated on the heap, we must use a pointer variable to store the start address. Regardless, the elements of an array are anonymous (we cannot refer to elements by name) with the exception of named arrays where the first element (only) can be referred to by the array name.Although they have no names of their own, every array element has identity (a memory address). Given that each element is of the same type and therefore the same length, it is trivial to calculate the address of each element by its zero-based index from the start of the array. Trivial as it is, the array suffix operator allows us to access elements without resorting to pointer arithmetic (the compiler generates the pointer arithmetic for us behind the scenes).Arrays implicitly convert to pointers at the slightest provocation. This makes it impossible to pass arrays by value because once converted to a pointer, the length of the array is lost. Thus arrays are always passed by reference and the length of the array (its dimension) is passed via a separate argument.

An array's name is not a constant pointer; it's not even a pointer! An array name is a reference to the array itself. Unlike pointers, references have no memory of their own, therefore a reference cannot be constant (only what it refers to can be constant). A reference is nothing more than an alias for a memory address. Since there is no separate storage for references, you cannot reassign references while they remain in scope. So, in that sense, it behaves like a constant pointer. But a pointer is a variable, so even if declared const, it requires memory of its own in order to store the memory address it points to.Example:int a[10];int * const p = a; // const pointerassert( *p &a ); would cause an assertion. Likewise, assert( p != &p); proves that p must reside in a separate memory address from that referred to by a. It has to: pointers are variables even when they are declared const.

Pointer needs freedom and happiness, like everyone else.

An associative array is one of a number of array-like data structures where the indices are not limited to integers.

Pointers are used for accessing the addresses of varibles and functions which are usedin our program directly.i have seen the answer from other site and improvedthe advantages of pointers as I see it.- Pointers allow you to implement sharing without copying i.e. pass by reference v/s pass by copying. This allows a tremendous advantage when you are passing around big arrays as arguments to functions.- Pointers allow modifications by a function that is not the creator of the memory i.e. function A can allocate the memory and function C can modify it, without using globals, which is a no-no for safe programming.- Pointers allow us to use dynamic memory allocation.- Pointers obviously give us the ability to implement complex data structures like linked lists, trees, etc- Pointers allow ease of programming, especially when dealing with strings. This is due to the fact that a pointer increment will move by the size of the pointee i.e. easy coding to increment to the next memory location of an array, without worrying about how many bytes to move for each data type. I.e. a pointer to a char will move the pointer by a byte, pointer to an int, by the size of the int, etc NOTE that this is important because you do not have to worry about the size of the data types which can vary on different architectures.- Pointers allow us to resize the data structure whenever needed. For example, if you have an array of size 10, it cannot be resized. But, an array created out of malloc and assigned to a pointer can be resized easily by creating a new memory area through malloc and copying the old contents over. This ability is very important in implementing sparse data structures also.

To empty an array in PHP you need to use the unset function like shown below: <?php $array = array('hello', 'hi', 'weee'); unset($array); // empty ?>

An array is an aggregate of elements, where memory is allocated to accommodate a given number of elements of a given type. The name of the array serves as a reference to the first element of the array. Unlike ordinary (non-array) variables, all the other elements of an array have no name; they are anonymous. However, all elements of an array have identity (they have an address) so if we know the address of an element within an array we can easily refer to it by that address. Given that each element is of the same type and therefore the same size (in bytes), we can easily calculate the address of each element offset from the start of the array. That is, the nth element of an array A of type T will be found at address A + sizeof(T) * (n-1). Although we are free to use "pointer arithmetic" like this to calculate the individual addresses of each element, C provides us with a much more convenient notation called the array suffix operator. The array suffix operator applies to pointer variables only. Fortunately, all arrays implicitly convert to a pointer at the slightest provocation so we don't have to do anything special to use them. The operator is denoted using square brackets [] such that for an array A we can refer to its nth element as A[n-1]. Given that A is of type T, the compiler has enough information to generate the required pointer arithmetic for us: A + sizeof(T) * (n-1). Note that array indices are in the range 0 to n-1 for an array of n elements. Attempting to access elements outwith this range has undefined behaviour, so it is important that we take steps to ensure all indices are kept within the bounds of the array. For fixed-length arrays, we can simply use a constant to store the array length, but for variable-length arrays we must keep track of the length using a variable. To range-check a given index against a given length, n, the index must be in the closed range [0:n-1]. However, array index ranges are often denoted using half-closed notation, [0:n), which essentially means 0 <= index < n.

Pointer is like variable address the members in memory shell

An array in any language is a contiguous block of memory that contains 1 or more elements of the same type. The array name is a reference to the starting address of the array itself, which is also the same address as the first element in the array. since all elements in the array must be of the same type, any element can be found at start_address+index*sizeof(<typename>), where the index is a zero-based value in the range 0 to n-1 for n elements. Like many other languages, C++ also allows use of the subscript operator [] to access individual elements without the need for pointer arithmetic in your code. Thus the nth element in an array can be found at <array_name>[n-1]. Behind the scenes, the compiler will convert subscripts to the equivalent pointer arithmetic for you. The upshot is that arrays allow random access to any element in constant time, with time complexity O(1).

The phrase "I-beam" describes the shape of the pointer in Word. The pointer looks like a capital i -- I -- with a line across the top and bottom

An array of pointers is exactly what it sounds like - one or more pointers arranged in order in memory, accessible through a common base name and indexed as needed. Philosophically, there is no difference between an array of pointers and an array of a[10]; // 10 integers, named a[0], a[1], a[2], ..., a[9]int *b[10]; // 10 pointers to int, named b[0], b[1], b[2], ..., b[9]If you initialize the array of i;for (i = 0; i ... then *b[0] would be the same as a[0], etc.

Copyright ยฉ 2021 Multiply Media, LLC. All Rights Reserved. The material on this site can not be reproduced, distributed, transmitted, cached or otherwise used, except with prior written permission of Multiply.