omg...arrays & pointers

Started by
12 comments, last by deadsexy 20 years, 3 months ago
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....
Advertisement
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.
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
Hah aren''t pointers evil? I''m just learning them now and they''re kicking my arse. There are plenty of tutorials online though. Just yahoo/google it. The internet is great
Another one bites the dust.
http://www.gamedev.net/reference/articles/article1697.asp

ya, i noobed it....i just wanted to see if any people out there could guide me to some popular "unkown" sites or unsearchable sites...

thx :D
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]
------------------------------------------VOTE Patrick O'GradyWrite in Presidential CandidateThe Candidate who Cares.
... 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?
the future is just like the past, just later. - TANSTAAFL
I think cplusplus.com in their C++ tutorial has a good primer on all that beginner stuff; you should check it out. 8u)

-Brendan Bond
Brendan Bond
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 = &num // 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).
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?


n00b I am..

This topic is closed to new replies.

Advertisement