Thursday, July 9, 2009

How to use pointer arrays in c?

Why do you want to make an array of pointers? Pointers are not that useful, unless you want to use dynamic memory.





I don't think many people here know C or C++, try posting on this website's forum: http://www.cprogramming.com/tutorial/les...

How to use pointer arrays in c?
pointer are "heart" of c language.


dynamic memory allocation is the brain of c language.





declaring a pointer which points to an array of integers


(eg)10 integers


int* ptr;


ptr=(int*)malloc(sizeof(int)*10);





declaring pointer which points to a string of length n


(eg)n=15





char* ptr;


ptr=(char*)malloc(sizeof(char)*n);





declaring a pointer which points to an array of pointers


(eg)let it be a structure pointer





struct node** ptr;


struct node* n; //let n be pointer to header of the linked list





//make ptr be a pointer for n


//pointer to a pointer





ptr=%26amp;n;





if you pass ptr to a function and make changes then the original linked list also changes





now we disscuss your questionb





let an array of pointers





int* ptr[10];





int **pp=%26amp;ptr;





now pp is a pointer to the array of pointers





it is mainly used for changing values of the pointers in a function





if you pass func(ptr);





the values wont change





you can use





func(pp);





it is what we want
Reply:#pragma RANT ON





Okay, contrary to what has been said. Pointers are fundamental to almost all programming structures in one form or another. Even simple arrays are implemented using pointers if you scratch the surface of the compiler.





#pragma RANT OFF





Anyway, that out of the way. Using a pointer array is just like using any other array except that the elements in the array point to values instead of directly containing them...





int index;


BYTE Value[10];


BYTE* BytePtr[10];





for(index = 0; index %26lt; 10; index++)


{


BytePtr[index] = %26amp;Value[index]; // Make BytePtr[index] point to Value[index]


*BytePtr[index] = 1; // Actually setting Value[index] to 1 as BytePtr[index] points to it.


}





for(index = 0; index %26lt; 10; index++)


*BytePtr[index] ++; // Actually incrementing Value[index] as BytePtr[index] points to it.





This is a pointless example but it does show the syntax of basic pointer array operations. At the end of it, all of the elements of the Value array will have a value of 2 and each of the BytePtr array pointers will be pointing to the corresponding elements of the Value array. And not a single dynamic allocation in sight. Although there may not be much point in this example, imagine the situation of sorting an array of 100kb images. Using pointers, you would only have to move a few bytes. If you were to shift the actual images, you would be moving 100kb at a time. A big performance difference, especially if you are talking about a real-time game.
Reply:Point at something and double click on any icon


No comments:

Post a Comment