C Programming
C++ Programming

Can a pointer be indexed like array?

Top Answer
User Avatar
Wiki User
Answered 2013-04-29 14:51:02

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.

User Avatar

Your Answer

Still Have Questions?

Related Questions

What is the difference between array to pointer and pointer to an array?

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.

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

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

Passing an array name to a pointer assigns the first memory location of the array to the pointer variable?

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.

Can an array be accessed through a pointer?

yes.. an array can be accessed through pointer

What is the difference between pointer and array?

Pointer holds an address Array holds values

What is a pointer in the array?

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 ...}

What is the easiest way to pass arrays as argument in c?

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

When does an array behave a pointer?

An array behaves like a pointer when you use its name in an expression without the brackets.int 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.

What is a pointer to pointers?

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.

How do you declare A pointer to array of three chars?

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

How do you create a pointer to an array of function pointers?

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.

How do you return an array from function?

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

Relation between array and pointer?

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.

What is an array variable?

An array is a variable containing multiple values. Any variable may be used as an array. There is no maximum limit to the size of an array, nor any requirement that member variables be indexed or assigned contiguously. Arrays are zero-based: the first element is indexed with the number 0.

What does array in mat?

In Mathematics, an array is an arrangement of quantities or symbols in rows and columns; a matrix. In Computing, an array is an indexed set of related elements.

What is the difference between array of pointers and pointer to an array?

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.

Why does array index start with zero in C language?

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]

Can an array of pointers represent multidimensional array?

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.

Difference between pointer and array?

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

What is the difference between array and pointer in C?

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

How do you convert from array notation to pointer notation?

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

What is the base address of an array in c program?

the address of variable (pointer) that contains array

How do you declare a string array and add elements to it in C plus plus?

You cannot add elements to a fixed array in C or C++. If, however, the array is declared as a pointer to an array, you can add elements by allocating a new array, copying/adding elements as needed, reassigning the new array to the pointer, and deallocating the original array.

If an array is declared in user defined function and pointer to this array is returned to main function is it possible to access that array?

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.

Still have questions?

Trending Questions
Best foods for weight loss? Asked By Wiki User
Does Neil Robertson wear a wig? Asked By Wiki User
Previously Viewed
Unanswered Questions
Saan nagmula ang gitara? Asked By Wiki User
Uri ng tekstong nareysyon? Asked By Wiki User
Can you get Takis at 7 eleven? Asked By Wiki User