pointers to array

Pages: 12
Hello everyone.
i am new here, and this is even my very first question i am asking here.
i have stuck over to pointers.
is there any way by which we can access the array index using pointers directly, which points to some array element that we don't know??
i mean, say,
u have an array and a pointer pointing to it.
now u perform some stuff (using an 'if' clause) and accordingly, the pointer is incremented/decremented. now you want to know at which array index the pointer is pointing to.
// one way can be storing the index in some variable .
but i want to access it directly via the pointer.
Last edited on
int a[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, };

int *p = a;

p += 5;

int index = p - a;
how 'a' is storing the address here?
and why don't we use the 'address of' (&) operator to access the array address?
how is it accessed without using '&' ??
Last edited on
Arrays decay to pointers to the first element in the array in many situations. It's the way it is. You could have written like this as well:
int *p = &a[0];
Last edited on
@ Peter87, just for the sake of trivia, which exactly are the situations when that decay does not occur?
An array used in expressions is implicitly converted to the pointer to its first element. So in expression p - a 'a' is converted to the pointer to the first element and has type int * that is the same type as the type of 'p'.

Expression &a is pointer to an array and has type int ( * )[9]. As you see it is different from int *.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
using namespace std;
void increase(void* data,int psize)
{
	char f[]="Nasser";
	if(psize==sizeof(f))
	{
		//"N" which will up to the next char "O"
		char* pchar; pchar=(char*)data;++(*pchar);
	}
	else if(psize==sizeof(int))
	{
		int* pint; pint=(int*)data; ++(*pint);
	}
}
	int main()
	{
		//Pointer is pointing to the first char"N"
		char a[]="Nasser";
		int b=1602;
		for(int n=0;n<5;n++)
		{
		increase(&a,sizeof(a));
		increase(&b,sizeof(b));
		cout<<"("<<a<<","<<b<<")"<<", ";
		}
		cout<<endl;
		return 0;
	}

as i understand ur question hope this program is useful i have make it while i was studying pointers to function u can ask me if u don't understand anything
and if it useless i'm sorry :D
Last edited on
@Catfish4, In situations when a is not used.. ;) I'm not sure about the exact rules. Is a decaying to pointer in a[i] and using the rules for operator[] on pointers or is operator[] applied to the array directly? I don't know.
Last edited on
@Nasser
In line-3 the type of data is void. So how can a char type array be passed to it( lines 23, 34) ??
also, kindly explain complete line 9 (line 13), if feasible.
Last edited on
void represents to the absence type
this allows "Void" to point to any data type
and i have used & to pass it from the function
well
line 9,13 well take a long time
we declare pointer "Pchar" and equal it with data because "Size of converts any data type to bytes then we add one to pchar
hope it useful
and u can see this code in the tutorial in this site at void pointers but i have modified it
In line-3 the type of data is void.

No, it's not. It's void* - i.e. a pointer to void. This means that it is a pointer that has no specified type - it can point to anything. You can pass a pointer of any type as a void*, without needing to cast it.

Of course, if you have a void* pointer and you need to interpret the data it's pointing to, your code will need to know what the real type of that data is, and cast the void* pointer to a pointer of the correct type.

Last edited on
well this a cool concept about void pointers. i really din't knew about it.
sorry to ask, but does a pointer occupy the same amount of memory for any data type?
i think so
closed account (Dy7SLyTq)
no it doesnt i believe it occupies no memory because its actually just an interface of sorts
yeah but u declare it with data type(int,char etc etc)and it occupies just for the data type :D
no it doesnt i believe it occupies no memory because its actually just an interface of sorts

That makes no sense at all. A void* is still a pointer, which means it's still an address in memory. It still needs some space to store that address.

I believe the storage for a void* pointer is the same as the storage for any other kind of pointer, i.e. it's the storage for a single number that represents a memory address.
closed account (Dy7SLyTq)
sorry your right. i wasnt thinking right. so if i could correct myself. it does use up a little bit to hold the address of what its pointing to but def not the same space as a class or built in type
I am not clear still...how much memory does a pointer occupy?
is it equal that of an integer??

what i think after reading all the comments is that,
pointer is just a place to hold memory location.
and that won't really depend upon the data type to which it points.
i guess... ;\
then what is the need of defining the type of pointer at the time of its declaration???
closed account (Dy7SLyTq)
4 or 8 bytes
http://answers.unity3d.com/questions/12294/how-much-memory-does-a-pointer-use.html

and it doesnt matter what its a pointer to because it just holds an address. you only have to specify the pointer type because c++ is strongly typed
sorry your right. i wasnt thinking right. so if i could correct myself. it does use up a little bit to hold the address of what its pointing to but def not the same space as a class or built in type

Every single pointer type just uses up "a little bit to hold the address of what its pointing to". That's what a pointer is - an address of what it's pointing to.

So a void pointer takes up the same amount of storage as any other pointer.


what i think after reading all the comments is that,
pointer is just a place to hold memory location.
and that won't really depend upon the data type to which it points.
i guess... ;\
then what is the need of defining the type of pointer at the time of its declaration???

But if you're storing the data, then presumably at some point you want to actually use it. Which means you - and the compiler - need to know what the memory at that address represents.

And if all you have is a void pointer, then how are you going to know?
Last edited on
Pages: 12