-2

What is the difference between the two, a ptr pointing at a variable and a ptr pointing at memory? I know a pointer is a variable that holds the memory address of a variable. So... A ptr pointing at a variable holds the address of that variable whereas a ptr pointing at memory is directly pointing at memory? The first somehow still holds some information about the initial variable whereas the later only knows an address?

EDIT: I didn't mean for this question to cause so much controversy and I'm not entirely sure why it's getting downvoted. I'm just trying to understand pointers... After doing some reading on the linked 'duplicate' it is my understanding that a ptr pointing to a variable always references that variable, cannot be reassigned, and cannot point to other pointers, and share an address with the variable. Memory pointers can point to NULL, can be reassigned, can point to other pointers, and have their own memory address. Are these true for C or only C++

Specerion
  • 115
  • 10
  • A dereferenceable pointer holds the *address of an object*. Whether that object is a variable is immaterial. – Kerrek SB Oct 31 '15 at 21:33
  • voting for reopening: this isn't a valid *duplicate*, this question is about pointers in C (**not** about references in C++) –  Oct 31 '15 at 21:33
  • No, you are way out with general understanding of how computers work. Variables are stored in memory and a pointer is a variable that holds the memory address of.. something, or contains NULL. – Martin James Oct 31 '15 at 21:34
  • 1
    @Rob this question has been incorrectly closed since the referenced duplicate is about references (no phun intended) and C doesn't have references. – Jack Oct 31 '15 at 21:34
  • @FelixPalmen Note the answer in that question has a long description of pointers in general. – Rob Oct 31 '15 at 21:36
  • 1
    @MartinJames that's correct, but it should be explained in an *answer*. Maybe there is a duplicate (I don't know), but the referenced one is just *plain wrong*. –  Oct 31 '15 at 21:36
  • @Rob a [tag:c++] question can never be a duplicate of a [tag:c] question! –  Oct 31 '15 at 21:36
  • @Jack You can de-reference a pointer so C definitely has reference pointers. – Rob Oct 31 '15 at 21:37
  • @Rob: C just has pointers, you can dereference a variable to obtain its address but what you get is just a pointer which is the same as every other pointer. There is no distinction between a pointer and a reference in C. Conceptually every pointer that is obtained by dereferencing a variable is a reference to the value but semantically it's just a pointer in C, that's not true in C++ if you use reference types. – Jack Oct 31 '15 at 21:40
  • http://cslibrary.stanford.edu/106/ – Rob Oct 31 '15 at 21:42
  • 1
    @Rob: Linking something which states that pointer and reference are equivalent terms when you requested to close the question as a duplicate of a C++ question in which a pointer __is definitely not equivalent to__ a reference is contradictory. – Jack Oct 31 '15 at 21:45
  • @Jack You said C has no reference pointers. My link, and a multitude of others, says they do. – Rob Oct 31 '15 at 21:52
  • One problem is that the word 'reference' is overloaded, like 'static' or 'interrupt'. The version called often has to be inferred by context. – Martin James Oct 31 '15 at 21:54
  • A reference in C is a pointer. A reference in C++ is not a pointer, except that it usually is. There, that's cleared that up:) – Martin James Oct 31 '15 at 21:57
  • Memory is an implementation detail; forget about it for a bit (both variables and 'memory' is in memory). A pointer points to an object, whether that be an object stored in a variable or returned from `malloc`. – MicroVirus Oct 31 '15 at 22:01
  • @Specerion: C doesn't have anything like C++ references, this question shouldn't have been closed as a dup. All non-const pointers are "reseatable" in C. But there's no such thing as a "pointer to a variable" either. So not sure what you mean. Post some sample code you're wondering about to make sure people understand your issue. – Mat Oct 31 '15 at 22:07
  • @Mat The dup pointed to had, as its first answer, a long explanation of pointers and, as stated by the OP, helped him find the answer. That is why it was marked as a dup. You state C has no such thing as a "pointer to a variable". Did you really mean to say that? Cause I've had pointers at variables for 30 years. – Rob Nov 01 '15 at 00:45
  • @Rob: C has pointers to objects and pointers to functions. "Pointer to variable" - no.. – Mat Nov 01 '15 at 08:37
  • @mat int i; int *a; a=&i; *a=2; printf("%d",i); – Rob Nov 01 '15 at 13:36
  • @Rob: `i` is a variable that refers to an object of type `int`. `a` is a pointer to the object that `i` refers to. `a` does not point to variable `i` - that doesn't make sense, variables don't exist after code is compiled. Only objects and code do. – Mat Nov 01 '15 at 13:40
  • @mat That you call it an "object" means you must be thinking in C++ which would be a different interpretation than those of us with experience in C and assembly. – Rob Nov 01 '15 at 14:10
  • @Rob: read the C standard. Object is a very well defined term in C (and has nothing to do with OOP). – Mat Nov 01 '15 at 14:42
  • @mat Then you want to talk about semantics and not what the pointer actually does. I'd rather talk about what the practicalities of what this pointer does and the pointer is pointing at a variable. That's all I have to say about this. – Rob Nov 01 '15 at 14:59

3 Answers3

2

A pointer is a variable whose value is an address in memory. The pointer also knows the type of whatever it's pointing to (or else it's a void*).

Basically that's all there is to it. There is no fundamental distinction between a "pointer that points to a variable" and a "pointer that points to memory". Whatever the pointer points to is in memory in any case. Whether the pointer points to a char variable, or a double variable, or an object, it's always simply pointing to the memory location where that char/double/object is stored.

Dabbler
  • 9,733
  • 5
  • 41
  • 64
0

Here's the way I see it, and I'll go by example.

In C, a pointer variable can be declared as:

char* p;

Then you can reserve stack memory for your program via a char array like so:

char buffer[5000];

You can then make the pointer reference this memory block like so:

p=buffer;

Variables by default point to their own addresses in stack memory for fast access. If you want to assign a pointer to memory from the heap, you can use calloc() or malloc(). For example, this code:

char* m;
m=malloc(5000);

... allocates 5000 bytes of memory from the heap (aka extended memory). then when you make reference to that pointer, you're actually reading to and writing from RAM.

This prints the letter "A" to the same static memory location twice then reads it back:

char block[10];
char* p=block;
block[0]='A'; //write to 1st position of block memory
*p='A'; //write same value to 1st position of block memory again
char *r;
r=p;
printf("%c",*r);

And now the same program but this time using just memory from the heap:

char* block;
block=malloc(10);
char* p=block;
block[0]='A'; //write to 1st position of block memory
*p='A'; //write same value to 1st position of block memory again
char *r;
r=p;
printf("%c",*r);
free(block);
Mike -- No longer here
  • 2,064
  • 1
  • 15
  • 37
0

please note that i am still learning and some information might be wrong

ok for example lets make

int i = 5;
int *ptr = &i;

lets say memory address for those are

i = 0x0001 
ptr =0x0002

in those memory address are

0x0001 = 0101  // this is 5 for binary
0x0002 = 0x0001 //this is the address of 'i'

so when ptr is called in these kind of ways u get these values

ptr = 0x0001 // the address stored in ptr
*ptr = 0101  // the * means whatever is at the address stored in ptr

if i had wrote like this

int i = 5;
int *ptr = *i;

then ptr will have whatever is at 'i'

i = 0x0001
ptr = 0101

so if i call ptr again in these ways

ptr = 0101
*ptr    // this will not work cus 0101 is not a memory address

edit

Also on your edit u said "that a ptr pointing to a variable always references that variable, cannot be reassigned, and cannot point to other pointers, and share an address with the variable."

This is not true for C, a pointer can be reassigned whenever u want and can point to other pointers

Unless it was made as a

const int *ptr = &i;

Then the pointer can not be changed later

Also u said "memory pointers can point to NULL, can be reassigned, can point to other pointers, and have their own memory address"

This is true as I said above but there are no such thing as a memory pointer and a variable pointer, there is only pointer that can hold a memory address.

BIG_FAN
  • 1
  • 3