Question:
what is the difference between an array and pointer?
neki09
2007-06-17 06:41:46 UTC
what is the difference between an array and pointer?
Four answers:
Balk
2007-06-17 07:38:44 UTC
An array is a collection of data values of the same data type.

Example:

int myarray[4]= {1,2,3,4};

char blah[50];



The 1st line above creates an array of 4 integers and initializes the array to 1, 2, 3 ,4.

Array elements 0-3 (remember that computer numbers start at 0, not 1) would contain the integer numbers 1, 2, 3, and 4.

The second line creates an array which holds 50 chars, but I didn't set the values for each element in the array.

You can read or write to one of the elements in the array like this:

char foo;

myarray[2] = 5; // set 3rd element to value 5

foo = myarray[2]; // foo = 5



A pointer is a special type of variable; It holds a memory address. The data type that you declared for the pointer variable tells the compiler the type of data that the pointer points to.

If you increment or decrement the pointer, then the memory address that the pointer contains will increase or decrease by the type of data that the pointer points to.

This is why you specify the pointer's data type when you allocate a pointer.



Example:

char *fuzz; // pointer will hold the address to a char variable

int *fuzzy; // pointer to an int variable

char ack=5;

int buzz=45;

char result;



fuzz=&ack; // fuzz holds the address of ack, not the number 5!

fuzz=&buzz; // warning! buzz is an int, and fuzz is supposed to contain the address to a char variable

fuzzy=&buzz; // good

result = *fuzz; // de-reference fuzz. Since fuzz points to ack, which equals 5, then result equals 5 -- not the address that fuzz contains.
Otto
2007-06-17 06:46:54 UTC
in C, pointers are pointing to memory address.

for example, a pointer can point to a char, a function, an array,etc. It just give you the address of the element you are calling. Depending on the element you are calling or refering to with a pointer you can move across it (this is the case when a pointer is used to point to an array).

See an array as a table.
2007-06-17 06:48:46 UTC
An array is a data structure that holds multiple values for a single variable name (basically). A pointer points to the area of memory that these name/values are stored prior to program execution. The pointer reserves that memory address so the program has access to it, and knows where to find it. A simple explanation, I know. Good luck!
sriram r(18yrs and young)
2007-06-17 06:46:16 UTC
array variable-a set of variables with the same name.

pointer- a variable which points to the address of the storing memory slot.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...