Programming

homeyg said:
Then I have one sentence to help you my friend. Are you ready? I'm only gonna say this once. *clears throat*

A pointer is a type of variable that holds a memory address.

This one sentence made the little light bulb click on in my head whenever I was stuck at pointers (a while back).

Thanks, I'll try learning it again this weekend and see if this helps.
 
Normal variable: You can ask somebody in who knows something.

Pointer: You can ask somebody who knows someone who knows something.

or

Normal variable: You look into a box. There's some sweets in.

Pointer: You look into a a box. There's a piece of paper that leads you to another box with some sweets in.
 
Comraddict said:
ok homeyg, what is the variable type of the pointer? i.e. string, int, etc.
:D
"Pointer to X," where X is whatever it's pointing to. For example and integer pointer would be of type "pointer to int." In C/++ the syntax is "(int *)" for type def or "int *var" to initilaze a variable called "var" as an integer pointer.
 
Comraddict said:
ok homeyg, what is the variable type of the pointer? i.e. string, int, etc.
:D

That's a trick question - there is no type, it's just memory used for storing another memory address. All pointers types can hold 4 bytes (which should be plenty to store an address).

Tryin' to get smart with me, eh?

But if that's not right (what I said above), I would say it is of type int.

Here's some basic C (rather than C++)

Code:
#include <stdio.h>
#include <conio.h>

int main(void)
{
    int testInt = 54;
    char testChar = 54;
    double testDouble = 5.67;
    int* pointInt;
    char* pointChar;
    double* pointDouble;
    int size;
    
    pointInt = &testInt;
    pointChar = &testChar;
    pointDouble = &testDouble;
    
    size = sizeof pointInt;
    printf("Size of pointer to int: %d", size);
    
    size = sizeof pointChar;
    printf("\n\nSize of pointer to char: %d", size);
    
    size = sizeof pointDouble;
    printf("\n\nSize of pointer to double: %d", size);
    
    _getch();
    
    return 0;
}
 
Sorry, pointer type is 32-bit integer: for 32-bit systems. I.e. everything is stored in 32-bit memory address.. :D
You were right.
 
Well It's not really an integer, just like it's not really a floating point number -- even though both of those are the same size.

It has it's own encoding system. The only noteworthy thing is that dispite this, you can increment and decrement it to access imediately proceeding or preceeding data.
 
Souron said:
Well It's not really an integer, just like it's not really a floating point number -- even though both of those are the same size.

It has it's own encoding system. The only noteworthy thing is that dispite this, you can increment and decrement it to access imediately proceeding or preceeding data.

No, it's an integer. It's just in binary form.
 
homeyg said:
No, it's an integer. It's just in binary form.
???
How can an integer not be binary on a computer? All information on your computer is fundementally binary.

Anyway, pointers are not integers.
Sure you could display it as an integer or floating point number with some compilers, but it's not really either of these.
For example, you cannot do most intger operations with pointers. Of course you can typecast/convert a pointer to an integer, and do integer operations with that, but then it's not really a pointer any more.
 
@Souron
You can't do had-hazard memory accesses with today's operating systems, but if you have random access memory then the memory addresses are indexes, which can be understood as integers. For instance: address 2342 would be the 2342th (most likely virtual) memory location.
 
Souron said:
???
How can an integer not be binary on a computer? All information on your computer is fundementally binary.

Whoops - I meant displayed in hexidecimal not stored in binary - addresses are usually displayed in hex. That's what I meant. :crazyeye:

I know that everything is stored in binary...
 
Conceeded. Pointers generally represent an ordered string of memmory locations, 0 though X, with X being the highest memmory value on the computer. And convention dictates that they are displayed in hexidecimal form. And for speach it often makes sence to talk in the desmal system, even when talking about pointers. Therefore pointers, being like integers in the aforementioned ways, are thought of as integers.

However, I would still say that it is good to differentciate pointers and integers. Pointers are not int's, and generally speaking should not be treated as such. Pointers are there own thing.

PS.
You can display pointers any way you want:
printf("%i,%X,%e,%f,%c,%O",ptr,ptr,ptr,ptr,ptr,ptr);
 
Can't you just go like this though? :P

Code:
printf("Address = %p\n\n", aPointer);
 
Souron: there is pointer arithmetic, for example:

Code:
int* pointInt;
*pointInt=40;
pointInt++;

will result mem address of stored integer being increased by 4 (size of int)

Hehe I have opened Pandora Box when I said that one word.:crazyeye:
 
If you're going from VB to C++ or Java I'd suggest you learn the basics of Object Orientation first, as those are object oriented lanugages.
 
Comraddict said:
Souron: there is pointer arithmetic, for example:

Code:
int* pointInt;
*pointInt=40;
pointInt++;

will result mem address of stored integer being increased by 4 (size of int)

Hehe I have opened Pandora Box when I said that one word.:crazyeye:
Yes, you can increment, decrement, and subrtact pointers. You can also add integers to or subtract integers from pointers. But that's it. You can't multipy, devide, mod, or use bitwise opperators on pointers.

Also in your example it is more usefull to think of the pointer as having advanced one unit instead, despite the fact that pointer can only point to byte increments.
 
Back
Top Bottom