C Programming
Java Programming
C++ Programming

What is the difference between array and pointer in C?


Top Answer
User Avatar
Wiki User
2012-10-02 16:01:55
2012-10-02 16:01:55

--> Array is a collection of same kind of data.

--> Pointer is a memory location where the data stored in the memory


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.

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

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

There is a difference: a pointer is a number that literally points to a place in memory. Arrays are groupings of a type. There is a close relationship between pointers and arrays, however: every expression with arrays (example: array[i]) can be expressed with pointers (example: *(array + i)), because for the computer, an array is just a list of pointers to the type of the array.

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.

the address of variable (pointer) that contains array

There is no similarity between the two.

Arrays are implemented as pointers in c.

arrays are the reserved sets of variables, which are supposed to store the similar data. pointers are the special variables which store the address of other variables.

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

The difference that i learnt very recently and the one i remember :-)Reference cannot be changed whereas pointer value can be changed.Actually, const pointer = reference.

The difference between a character array and an integer array in C (and also in C++) is simply one of size (and, thus, range) of the elements. In C and C++, characters are the smallest integers, usually one byte long. Other than size; chars, short, int, and long are the same fundamental type - integral.

Because that's how the language is defined. In C, C++, and many other languages, array indecies start with zero. This is by convention, and it also reflects the fact that, in C and C++, array-index syntax can also be expressed in pointer-offset syntax. The pointer locates the first element of the array, and the offset indicates how many elements to skip. If a is an array name, then the expression a[n] is completely identical to the expression *(a+n), and vice versa.

Mentioning the array name in C or C++ gives the base address in all contexts except one. Syntactically, the compiler treats the array name as a pointer to the first element. You can reference elements using array syntax, a[n], or using pointer syntax, *(a+n), and you can even mix the usages within an expression. When you pass an array name as a function argument, you are passing the "value of the pointer", which means that you are implicitly passing the array by reference, even though all parameters in functions are "call by value". There is, however, one very important distinction. While an array name is referentially the same as a pointer, it is not a pointer in that it does not occupy program referential space in the process. This means that, while you can change the value of a pointer, and thus the address to which it points, you can not change the value of an array name. This distinction is what we call R-Value (array or pointer) as opposed to L-Value (pointer only), i.e. can the object appear on the left sign of an assignment operator.

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]

There are no array operations in C. Arrays implicitly convert to pointers, thus any operation you might attempt upon an array you would actually perform on a pointer.

It's actually quite hard NOT to reference an array using a pointer. All array types will implicitly convert to a pointer at the slightest provocation: void f (const int* arr, const unsigned size) { for (int i=0; i<size; ++i) { printf ("%d\n", arr[i]); } } int a[100]; /* static array (global) */ int main (void) { int b[10]; /* local array */ int* c=malloc (1000 * sizeof(int)); /* dynamic array */ /* the following function calls are equivalent */ f (a, 100); /* a is a reference to the start address of the array named a */ f (&a, 100); /* &a is the address of the array named a */ f (&a[0], 100); /* &a[0] is the address of the first element of the array named a */ /* the following function calls are equivalent */ f (b, 10); /* b is a reference to the start address of the array named b */ f (&b, 10); /* &b is the address of the array named b */ f (&b[0], 10); /* &b[0] is the address of the first element of the array named b */ f (c, 1000); /* c is a pointer to the start address of an anonymous array */ f (&c, 1000); /* undefined behaviour: &c is the address of the pointer variable, not the array */ free (c); c=0; return 0; }

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.

In C you would simply return a char pointer to the start of the array. In C++ you can still do that but it's better to return a vector instead.

There is no "NULL array" as such, you may take a pointer to an array and set it to NULL (binary 0) e.g. int* foo; // Declare a pointer foo = malloc( 40 * sizeof(int)); //Allocate an array of 40 integers pointed to by "foo" foo = NULL; //Set the pointer to NULL, if you're using a garbage collector this should trigger an automatic free() of the memory allocated to the array. If you are NOT using a garbage collector (which is more common in C) this line is a memory leak.

There is no data type string in C. String is handled as an array of characters. To identify the end of the string, a null character is put. This is called a null terminated character array. So array of strings will be a double dimensioned array of chars. It is implemented as an array of pointers, each pointer pointing to an array of chars.

It depends on the type of data, but generally you would just implement a data array and have a static pointer to the "next" element of the array and a static pointer to the "last" element of the array. New data would be added to the location of the "last" pointer. Data would be processed from the "next" pointer. Pointers would be incremented to the appropriate element whenever reading or writing and special attention given to any time you come to the end of the array.

A String in C is an array of characters. An array is just a sequence of similarly-typed elements that are stored in consecutive memory locations and can be accessed by using the array index.

The name of an array can be looked as a pointer of this array,and it points to the local memory address of the first element.So we can gave the address to a pointer.The flow is an easy example to show hou to use a pointer to print an array.#include "iostream.h"void main(){char a[]="abcdefgh";char *b=a;//afor(int i=0;icout}

In C programming, an array name implicitly decays to a pointer. This makes sense because all arguments are passed by value in C. Passing by value means the value must be copied and we definitely don't want to copy an array if we can avoid it. The value of a pointer is a memory address, and copying a memory address is extremely efficient. If we really need to copy an array in C then we have to do so manually.

Copyright ยฉ 2020 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.