Trouble using pointers (with C)

Discuss all aspects of programming here.

Moderator: The Mod Squad

Trouble using pointers (with C)

Postby pgiustino » Mon Jul 17, 2006 1:01 pm

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
pgiustino
Green Belt
Green Belt
 
Posts: 162
Joined: Fri Sep 06, 2002 2:30 pm
Location: Texas

Postby cangove » Mon Jul 17, 2006 10:26 pm

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
cangove
Green Belt
Green Belt
 
Posts: 223
Joined: Tue Jun 04, 2002 2:51 pm

Postby pgiustino » Tue Jun 05, 2007 8:00 pm

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
pgiustino
Green Belt
Green Belt
 
Posts: 162
Joined: Fri Sep 06, 2002 2:30 pm
Location: Texas


Return to Programming

Who is online

Users browsing this forum: No registered users and 1 guest