.
carbonize.co.ukcarbonize.co.uk

Y!Tunnel
Trillian
YahELite
Opera
Ad Aware
Ultraedit
YahSEek

Advertisements:


Send honey to your loved ones

[Get Opera!]

Synfire's Guide to C Programming
11. Pointers

    Okay, there is alot of confusion about pointer amongst newbies that I feel before I continue I must explain. Many of the people I talk to that are learning C/C++, or whatever, that come across pointers always say "The [Book|Text|Person] I'm learning from taught me to create a pointer to a variable, but why would I want to rename a variable I already named?".
Okay, to some of you seasoned coders you may think, GOD WHAT A DUMBASS! but remember, half of all references on pointer just say...
int i;
int *p;
p = &i;
printf("%d", *p);
Now after looking at that, I can see where the confusion comes in. The person writing the tutorial|book is just too damn lazy to explain what your really gonna do with pointers, or they don't know themselves! (I'd hate to think that if they don't know that they would be writing a tutorial. :|) So, in my tutorial I'm not gonna show you shit like that. Instead of easing you into pointers, I'm gonna drop you right into the middle and let you learn your way out.

        So why would you use pointers? Well, pointers can be used for many things, like with scanf(). Scanf() takes the first argument and passes it through a pointer to the second argument, thats why you have to have the & before your second argument. But myself, the most use I get from pointers is by accessing data stored on the heap. This makes your programs run faster, each time data is called it is copied to memory used and then dumped.
  To optimize your program you can add your data to the heap so that it isn't dumped until your program says so. This will reduce loading time in your program and sometimes make your program run much faster. To access the heap you'll either have to know the exact address or use a pointer. Since pointers are much easier to use then that is how we use it...

malloc&free.c
------------------------------------
#include <stdio.h>

int main()
{
	int *pHeap;
	pHeap = (int *)malloc(sizeof(int));
	if(p == 0)
	{
		 printf("Error: Out of Memory!\n");
		 return(1);
	}
	*pHeap = 12;
	printf("%d is on the Heap.\n", *pHeap);
	free(pHeap);
	return(0);
}
--------------------------------
        Okay this is a simple example of putting something on the Heap.
  Now I did say I wasn't gonna do anything that you wouldn't use in this section, and I didn't, This may look useless, but by placing this on the heap, 12 is now accessible to any function. But here is the real deal...
  On line five the pointer is created, then on line six, a section of the heap the size of an int is created on the heap, and the address is assigned to the pointer. Lines seven through eleven test the pointer to make sure that there is memory available to the pointer. On line twelve we place the number twelve on the heap. On line thirteen you see something a bit different with printf, . Finally on line fourteen we free all allocated memory on the heap with free(). Now let me show you a GOOD example of heap usage.

heap.c
----------------------------------
#include <stdio.h>
#define MAX 20

struct New
{
	char *Name[MAX];
	unsigned int Grade;
};

int main()
{
	unsigned int count, Added, Average;
	New *Student;
	Student = (New *)malloc(sizeof(New[MAX])):
	if(!Student)
	{
		printf("Error: Not enough memory\n");
		return(1);
	}
	for(count = 0; (*Student[count]).Name != NULL || (*Student[count]).Grade != NULL ||count < MAX; count++)
	{
		printf("Name: ");
		scanf("%s", (*Student[count]).Name);
		printf("Grade: ");
		scanf("%d", (*Student[count]).Grade);
	}
	for(count = 0; (*Student[count].Grade != NULL; count++)
	{
		Added = Added + (*Student[count]).Grade;
	}
	Average = count/Added;
	printf("Class Average: %d", Average);
	return(0);
}
------------------------------------------
        Now the only thing really that you haven't covered in this example is the array of pointers. Well there it is! Another thing is the syntax for working with pointers to structs. Notice:
(*Student[count]).Name
        This isn't so confusing just add the parenthesis to it when you have it as a pointer. If it wasn't an array it would look like this:
(*Student).Name
        Much easier on the eyes. Now if you have followed along so far then you should be good to go for the since of this text. If you want more you'll have to get a freaking book. I wash my hands of this, I'm through.


Back to top | Contact me