Archived

This topic is now archived and is closed to further replies.

omg...arrays & pointers

This topic is 5088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

going through a book, and i don''t quite understand it the way the author described it. does anyone know any good primers on-line that could aid in getting me comfortable with pointers & arrays? i don''t wanna go much further without getting them down....

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I have to admit array & pointers are a bit tricky to understand in the beginning.. Maybe you should start asking some questions and some of us can answer them.

Share this post


Link to post
Share on other sites
for starters....
arrays make sense:

short ArrayVariable [6] would equal 5 array slots i guess (0-5), etc..

but, when you start talking about pointers it gets odd. a pointer can point to another variables "address of variable" or "value of variable" (the operator ''&'' contorls the address aspect, and "*" the value).

okay, that makes sense, i can incorp. that into my code, and see the use.

but, the book suddenly talks about how "arrays" are related to "pointers"...they have a "relationship". so could i think of an "array" as a "list" and the "pointer" points to certain numbers in that list?

and lastly, you can call a pointer in a fucntion like this:

double FunctionName (int * ParameterOnePointer, int Parameter2)
FucntionName = function name, that''s easy
int * ParameterOnePointer = a pointer being defined?
Parameter2 = 2nd parameter in the function, again easy

so when i call & define this function, i need to define what the pointer "* ParameterOnePointer" is pointing to? so would i need to add in the body of the function a line like this:

ParameterOnePointer = whatever

so then whenever i use "ParameterOnePointer" in my code it will refer to "whatever".....

and THEN as if it''s not already confusing enough you need to allocate the memory!!! (i pull my hair out now, hehe)


so you see why im confused? im one big fat noob at this, but im not giving up. but, i don''t want to make some people on the forum respond to me, i can read more....im sure there''s primers out there somewhere...if anyone can help with some tips coming from a more advanced programmer familiar with common uses that would help....thx

Share this post


Link to post
Share on other sites
An array is just a pointer to a list in memory. So if you did this:

int intArray[6]; //create 6 slots for use, indexed 0-5

you would create a pointer called intArray which points to the start of an array of integers.

Remember what a pointer is: a variable which holds the address of an object in memory. So when you pass a pointer as an argument, you are just passing a memory address.

so the function call would go something like this:


double FunctionName (int * ParameterOnePointer, int Parameter2)
{
return *ParameterOnePointer + Parameter2;
/*you have to add the "*" (dereference operator) to the beginning to tell the compiler that you want the value that the pointer points to, and not the address. otherwise it would add Parameter2 to the address and return it.*/
};
#include <iostream>
using namespace std;
//so in main...

int main()
{
int a = 3, b = 4;

cout<<"the result of a and b is: "<<FunctionName(&a, b)<endl;
/*Here we use the "address of" operator to pass the address of the first integer. This is because the parameter is expecting an address, and not a value.*/

return 0;
};


[edited by - MrBeaner on January 4, 2004 12:25:15 AM]

Share this post


Link to post
Share on other sites
... pointers are really hard to learn. I read the first few chapters about pointer basics, but never really got it. The only reason I learnt them is by writing programs that use pointers, compiling and looking up the error codes and (more or less) teaching myself.
Probably not the best method though...


Don''t know why you had to know that, but I guess it''s good to try something even if you don''t know how it really works. (I''m doing the same for assembly code right now!!!)



Why do Suicide Bombers think they''re going to a better place?
Don''t they know their going all over the place?

Share this post


Link to post
Share on other sites
I''ll try to make this easy...

A pointer holds the address of the variable that it points to.

{
...
int num = 5;
int *pt;
pt = # // hold num''s address
cout << "pt address = " << pt << endl;
...
}

OUTPUT: pt address = 0x0065fd48

Then when you "dereference" the pointer, it gives the value of the pointed to variable.

{
...
cout << "*pt value = " << *pt << endl; // dereference
// operator ''*''
...
}

OUTPUT: *pt value = 5

OK, that was the easy part -- just think of "*pt" as the same as "num".

Now for the relationship of pointers and arrays…

The name of an array is a pointer to the allocated memory.

#include <stdio.h>
#include <iostream>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
int array[5] = {0, 1, 2, 3, 4}; // "array" is a pointer
int *pt = array;
int i;
for (i=0; i <= 4; i++)
{
cout << "pt[" << i << "]= " << pt << endl;
}
getchar();
getchar();
return 0;
}

And a simple pointer function...

#include <stdio.h>
#include <iostream>
using namespace std;

int add(int *, int *); // prototype

int _tmain(int argc, _TCHAR* argv[])
{
int one, two;
one = two = 2;
cout << add(&one, &two) << endl; // use ''&''
getchar(); // pause
getchar(); // pause
return 0;
}

add (int *pt_one, int *pt_two) // "*pt_one" is assigned to "one" and "*pt_two" is assinged to "two"
{
return (*pt_one + *pt_two);
}







"Do not flame people you don''t know in a public forum. Only amateurs do that. Professionals in the industry know they will run into each other over and over. The person you flame this year may the person you want to do business with next year. Don''t burn your bridges," (Diana Gruber, http://www.makegames.com/chapt6.html).

Share this post


Link to post
Share on other sites
Basically a pointer is just a variable, that contains an memory address of another variable.


Arrays are just like variables, but they can hold multiple values, correct?

Now, the relationship between pointers and arrays is that an array is ACTUALLY a POINTER that is pointing to the first memory address where the first value in the array will be stored.

Since each value in an array is usually stored sequentially, we can then do this:

*(myArray+2) and that would be the same as accessing myArray[2]

get it?


Share this post


Link to post
Share on other sites
It''s really hard to understand them. In my opinion, tutorials and explanations could greatly help if they explained the purposes of them, and interesting scenarios that make them useful to make it more visual to the readers.

Share this post


Link to post
Share on other sites
That''s the problem with most tutorials, they teach you HOW to use X, and not WHY to use it, which is frankly much more relevent. Sure I can tell you how you declare inheritance, but why? What is it used for, and what should it not be used for? That''s what a good fat book is for... That and experience.

Share this post


Link to post
Share on other sites
quote:
Original post by deadsexy
going through a book, and i don''t quite understand it the way the author described it.

does anyone know any good primers on-line that could aid in getting me comfortable with pointers & arrays? i don''t wanna go much further without getting them down....


Try the comp.lang.c FAQ section on arrays and pointers.




--
Dave Mikesell Software & Consulting

Share this post


Link to post
Share on other sites