The Mother Board

Motherboards.org forums. Free tech support, motherboard ID, and more.
It is currently Fri Aug 17, 2018 7:31 am

All times are UTC - 8 hours




Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Mon Jul 17, 2006 1:01 pm 
Offline
Green Belt
Green Belt

Joined: Fri Sep 06, 2002 2:30 pm
Posts: 162
Location: Texas
I know that pointers refer to the address of an object, but I still can't understand what is going on. Take this program someone helped me with. It is gets 5 values from the user and outputs an array and then the same array with the values multiplied by 5.


#include <stdio.h>
#include <stdlib.h>

/* function declaration or function prototype */
int *myFunction(int []);
void mySecondFunction(int []);
int main(void)
{
int MyArray[100];
int *A;
A = myFunction(MyArray); /*function call*/
printf("\n");
mySecondFunction(A);
return 0;
}

int *myFunction(int MyArray [100]) /* function header */
{
int i;
for (i=0;i<=4;i++)
{
printf("\nEnter value %d: ",i+1);
scanf("%i",&MyArray[i]);
}
printf("The matrix you input is:\n\n");
for (i=0;i<=4;i++)
{
printf("%d",MyArray[i]);
}
return (MyArray);
}

void mySecondFunction(int MyArray[100])
{
int i;
for (i=0;i<=4;i++)
printf("%d \t", MyArray[i]*5);
}

_________________
MSI K8t Neo Fis2r


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Mon Jul 17, 2006 10:26 pm 
Offline
Green Belt
Green Belt

Joined: Tue Jun 04, 2002 2:51 pm
Posts: 223
Pointers can be fun, once you get a feel for them. Part of the power is their lexability, but this is confusin gon first sight. So first the basics.

You're correct a pointer is really a variable that contains the address of some data. So a:

int *data_ptr;

Is a pointer that points to an integer. Its not very useful until you have some data associated with it. One big use is that this allows you to pass in a variable to a function and have it get modified. For example you might have

void
my_function (int input_int)
{
input_int = 42;
}

This function actually does not change the value of input_int that is passed in., since its passed in by value (essentially the value is copied). If you want it updated you have to pass by reference, and in C this is done my using pointers, so you would have:

void
my_function (int *input_int_ptr)
{
(*input_int_ptr) = 42;
}

So now I pass in the address to my int, and so the data gets changed both in this function and in the code that calls it. The (*input_int_ptr) notation just dereferences the ptr to the int value, if you justhad input_int_ptr = 42; you would be setting the address to 42, and all of a sudden your pointer points to some unknown part of RAM, which is why pointers can be dangerous.

Now onto your example here. An array such as MyArray is a chunk of memory allocated all in a row. So if we know the memory address of the first element, we can find all elements in the array.

So MyAddress can be accessed as a pointer as well. When you call myFunction () you're passing in the address for the first element in the array. The [#] values are just memory dereferencing, it just says go # memory chunks ahead starting from the pointer. So in this way pointers and arrays are similar beasts.

There are many things you can do with these things, andthey are quite useful. But they are difficult to understand at times, and very dangerous if not used correctly (dangerous in the sense of causing the porgram to abort), and this is why some languages have removed pointers.

_________________
Shuttle SN45G, AMD AthlonXP 3000+, 2x512MB Crucial DDR333, 80GB WD HD, Sony DRU-720A DVRW, MSI Radeon 9800 Pro 128MB, Dell 24" widescreen LCD


Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: Tue Jun 05, 2007 8:00 pm 
Offline
Green Belt
Green Belt

Joined: Fri Sep 06, 2002 2:30 pm
Posts: 162
Location: Texas
Ha, saw this old post of mine and decided to update. I think I finally understood the use of pointers last semester in one of my courses. I think the main problem I had before was that I wasn't programming anything complex enough to require functions that changed more than one variable. I always thought I could just return a value and skip the pointer. Of course, I learned out of necessity after doing more than just this one program. Practice IS everything.

STill, I quit electrical engineering for a reason. Thanks cangove for the help![/quote]

P.s. The act of dereferencing is something one shouldn't overlook when learning about pointers. I spent hours adding pointers all over my programs and got thoroughly confused. Apparently two pointers were enough. Not one, or 50, just two. *sigh

_________________
MSI K8t Neo Fis2r


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC - 8 hours


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB® Forum Software © phpBB Group