I know that this code:
void incVar(int i){
i++;
}
We know this will create a copy of the integer and then increase that value, but not the actual value.
By nature: Methods in C create copies of parameters in their Stack Frame and not the original variable.
But:
void incVar(int *i){
(*i)++;
}
Is supposed to increase the actual value of the integer by the pointer dereference.
But then, why doesn't C just create a copy of the pointer *i instead? If this is the normal behavior with regular integers, then why doesn't the same thing happen with pointers?
It is the same with pointers. All variables in C are passed by value, even pointers.
You copy the address stored in the pointer outside the function, into its parameter.
But you can use that address to reference a variable which can be allocated anywhere. So in the following code:
int j = 0;
incVar(&j);
incVar receives by-copy the address of j. But it can use that address to read or modify j (in)directly.
In fact the same thing as with values happens with pointers. Just make sure to understand the syntax correctly. You are not passing the integer *i by copy to incVar, but you are passing the pointer i of type int* by copy. No matter how often you copy the pointer to an address, it always points to the same address. So i in your second incVar example points to the integer the caller took the address of. So by derefencing the copy of the pointer (in (*i)++), you are acessing the integer of the caller.
Its better to see int *i; as a variable named i of type pointer-to-int, instead of a pointer named *i.
In your example:
void incVar(int *i) {
(*i)++;
}
We probably use it as such in the main function:
int a = 5;
incVar(&a); //After this line, a is now 6.
What happens in incVar(..) is this:
A variable i is created, of type pointer-to-int.
It holds a copy of the value of &a.
Although i is a copy, its value is the same as that of the value of &a, and it still points to the same integer a.
As a result, by de-referencing i, I refer to the integer a.
Increment the integer a, which obviously is at the address pointed to by i.
Related
See the two codes below!
int main() {
int a = 12;
int *p;
*p = a;
}
and the this code,
int main() {
int a = 12;
int *p;
p = &a;
}
In the first piece of code dereferenced the pointer as this *p = a, and in the second piece of code, the address of variabe a is set to the pointer variable.
My question is what is the difference between both pieces of codes?
In your first piece of code:
int main() {
int a = 12;
int *p;
*p = a;
}
you have a serious case of undefined behaviour because, what you are trying to do is assign the value of a to the int variable that p currently points to. However, p has not been assigned an 'address', so it will have an arbitrary - and invalid - value! Some compilers may initialise p to zero (or NULL) but that is still an invalid address (on most systems).
Your second code snippet is 'sound' but, as it stands, doesn't actually achieve anything:
int main() {
int a = 12;
int *p;
p = &a;
}
Here, you are assigning a value (i.e. an address) to your pointer variable, p; in this case, p now points to the a variable (that is, it's value is the address of a).
So, if you appended code like this (to the end of your second snippet):
*p = 42;
and then printed out the value of a, you would see that its value has been changed from the initially-given 12 to 42.
Feel free to ask for further clarification and/or explanation.
Declaring *p and a is reserving some space in memory, for a pointer in first case, for what a is in the 2nd case (an int).
In these both cases, their values are not initialized if you don't put anything in it. That doesn't mean there is nothing in it, as that is not possible. It means their values are undetermined, kind of "random" ; the loader just put the code/data in memory when requested, and the space occupied by p, and the one occupied by a, are both whatever the memory had at the time of loading (could be also at time of compilation, but anyway, undetermined).
So you take a big risk in doing *p = a in the 1st case, since you ask the processeur to take the bytes "inside" a and store them wherever p points at. Could be within the bounds of your data segments, in the stack, somewhere it won't cause an immediate problem/crash, but the chances are, it's very likely that won't be ok!
This is why this issue is said to cause "Undefined Behavior" (UB).
When you initialized a pointer you can use *p to access at the value of pointer of the pointed variable and not the address of the pointed variable but it's not possible to affect value like that (with *p=a). Because you try to affect a value without adress of variable.
The second code is right use p = &a
The first one is bad:
int main() {
int a = 12;
int *p;
*p = a;
}
It means: put the value of variable a into location, pointed by pointer p. But what the p points? probably nothing (NULL) or any random address. In best case, it can make execution error like access violation or segmentation fault. In worst case, it can overwrite any existing value of totally unknown variable, resulting in problems, which are very hard to investigate.
The second one is OK.
int main() {
int a = 12;
int *p;
p = &a;
}
It means: get the pointer to (existing) variable a and assign it to pointer p. So, this will work OK.
What is the difference between dereferencing and assigning the address of a variable to pointer variable in C?
The latter is the premise for the first. They are separate steps to achieve the benefit of pointer dereferencing.
For the the explanation for where the difference between those are, we have to look what these guys are separately:
What is dereferencing the pointer?
First we need to look what a reference is. A reference is f.e. an identifier for an object. We could say "Variable a stands for the value of 12." - thus, a is a reference to the value of 12.
The identifier of an object is a reference for the value stored within.
The same goes for pointers. pointers are just like usual objects, they store a value inside, thus they refer to the stored values in them.
"Dereferencing" is when we "disable" this connection to the usual value within and use the identifier of p to access/refer to a different value than the value stored in p.
"Dereferencing a pointer" means simply, you use the pointer to access the value stored in another object, f.e. 12 in a instead through its own identifier of a.
To dereference the pointer the * dereference operator needs to precede the pointer variable, like *p.
What is assigning the address of a variable to a pointer?
We are achieving the things stated in "What is dereferencing a pointer?", by giving the pointer an address of another object as its value, in analogy like we assign a value to a usual variable.
But as opposed to usual object initializations/assignments, for this we need to use the & ampersand operator, preceding the variable, whose value the pointer shall point to and the * dereference operator, preceding the pointer, has to be omitted, like:
p = &a;
Therafter, The pointer "points" to the address the desired value is stored at.
Steps to dereferencing a pointer properly:
First thing to do is to declare a pointer, like:
int *p;
In this case, we declare a pointer variable of p which points to an object of type int.
Second step is to initialize the pointer with an address value of an object of type int:
int a = 12;
p = &a; //Here we assign the address of `a` to p, not the value of 12.
Note: If you want the address value of an object, like a usual variable, you need to use the unary operator of &, preceding the object.
If you have done these steps, you are finally be able to access the value of the object the pointer points to, by using the *operator, preceding the pointer object:
*p = a;
My question is what is the difference between both pieces of codes?
The difference is simply as that, that the first piece of code:
int main() {
int a = 12;
int *p;
*p = a;
}
is invalid for addressing an object by dereferencing a pointer. You cannot assign a value to the pointer´s dereference, if there isn´t made one reference before to which the pointer do refer to.
Thus, your assumption of:
In the first piece of code I dereferenced the pointer as this *p = a...
is incorrect.
You do not be able to dereference the pointer at all in the proper way with *p = a in this case, because the pointer p doesn´t has any reference, to which you are be able to dereference the pointer correctly to.
In fact, you are assigning the value of a with the statement of *p = a somewhere into the Nirwana of your memory.
Normally, the compiler shall never pass this through without an error.
If he does and you later want to use the value, which you think you´d assigned properly by using the pointer, like printf("%d",*p) you should get a Segmentation fault (core dumped).
I'm trying to learn pointers in c and have been trying to write samples of code for different types of situations that involve pointers that I have found in this C book
in "Examples of pointer constructs" but I can't seem to figure out how to return a pointer to a pointer to a pointer to an int from a function or how to dereference such a pointer in main.
I have been searching for a simple example all over but couldn't find one to make it clear for me. Could anyone explain this concept and how such pointers work inside functions?
I've had no problems returning a pointer to int from a function and understood the process but higher levels gave me a hard time.
I've been trying to write very basic examples but I can't manage to store what this function returns in main, and also how I should dereference it:
int ***func(int x) {
int n = x * x;
int *p1, **p2, ***p3;
p1 = &n;
p2 = &p1;
p3 = &p2;
return &p3;
}
You have one more level of indirection at the end than you need:
return &p3;
p3 has type int ***, which matches the return value of the function. However you're returning the address of p3 which has type int ****. That's a type mismatch. You can correct this by just returning p3:
return p3;
Assuming for a moment that the pointer you return is valid (as well as all intermediate pointers), the you would assign the return value of the function to a variable of type int ***. Then you would have to dereference 3 times to get the actual integer value:
int ***rval = func(2);
printf("value=%d\n", ***rval);
There's a bigger problem here however, and that's that you're not returning valid pointers. The pointer value you're returning contains the address of a local variable (i.e. the address of p2). When the function returns that variable goes out of scope and thus its address is invalid. Attempting to dereference a pointer to an out of scope variable is undefined behavior.
For the memory you're pointing to to be valid after the function returns, you would need to dynamically allocate memory using malloc.
Also, it's very rare that you'd actually need a triple pointer (i.e. int ***). If you find yourself using a variable of this type, that's a good sign that you need to rethink your design.
I'm new to programming, and at present I am learning about pointers in C.
I know that pointers are the variables which contain or hold address of another variable. Today when I was more learning about it with K&R I got confused in a line which says "&a is a pointer to a" in a given function swap(&a,&b) in Page No. 80. How &a is a pointer? It is not a variable, it is the address of the variable. Am I right?
I know that arguments can be passed to a function in two ways: call by value and call by reference. The caller's value of the argument is not changed in the first one, but it can be changed in the second one.
My question is I had read that if we want to change the value of the variable we have to pass the pointer to it (i.e. the location of the value we want to modify). What is meant by that? I mean, do we have to pass pointers to the function? And what is the meaning of the statement, "we have to pass the pointers to the location we want to modify".
A pointer is not a variable. A pointer is a value.
A variable, in C, designates a storage location, and a value can be stored in that location. Thus, if you have a variable a declared with int a, then a is a variable in which an integer value can be stored. If you have a variable int *x, then x is a variable in which a pointer to an integer value can be stored.
The address of a storage location can be obtained using the & operator. E.g., &a is the address of the storage location designated by a, or the address of a, and can be stored in (among other things) a variable of the corresponding type. Thus you can have:
int a = 42; /* a is a variable of type int, and has value 42 */
int* x = &a; /* x is a variable of type int*, and has value &a */
Although analogies in programming are often dangerous, you might think of things like page numbers in a book. The page number of a page is not the same thing as the page, but page numbers can still be written down on pages. E.g., the table of contents page has lots of page numbers written down on it. Thus:
actual_page p = ...; /* a page structure */
page_number n = &p; /* a page number */
A pointer is an address. A pointer variable is a variable holding an address.
It is perhaps analogous to the difference between a literal integer 1 and an integer variable int a. One might refer to both a and 1 as integers, just as you might refer to &a and int* p as pointers.
Note also that &a is not an lvalue and cannot be assigned - so it is as you say not a variable, but it is a pointer nonetheless, and can itself be assigned to a pointer variable.
If a is a variable of some type T, then &a is an expression which evaluates to the address of that variable, also known as a pointer to that variable—the terms are interchangeable. &a has the type T*, which is pronounced “pointer to T”.
int x = 4; // 4 has type int, so we can assign it to an int variable.
int *p = &x; // &x has type int*, so we can assign it to an int* variable.
The reason that you can modify function arguments passed by pointer is that a pointer introduces a sort of alias for a variable. You can alter it from multiple locations, either directly (via the variable) or indirectly (via the pointer).
// modifying x modifies *p.
++x;
printf("%d %d\n", x, *p);
// modifying *p modifies x.
++*p;
printf("%d %d\n", x, *p);
As others have observed, a pointer is a particular kind of value, not a particular kind of variable.
With regard to calling functions:
You cannot "pass a variable to a function" in C. If you put a variable name in the argument list of a function call then the value stored in that variable is passed, not the variable itself.
Although in a general sense programming languages may provide for passing function arguments either by value or by reference, C provides only pass by value. You can emulate pass by reference in C, however, by passing a pointer (value) to the location where another value is stored. That's what swap(&a, &b) does: you are passing the locations where the values of variables a and b are stored (that is, pointers to those values).
Pointer contains the address - so synonymous.
When you pass by reference, you are passing in the address - to directly modify the value.
A pointer is a type of variable that stores the address to an object.
Basically, the pointer is the address.
Think of it as a piece of paper. When it has a number printed on it, it's an integer (or other numeric type).
A pointer is a piece of paper that says "the data is on the peice of paper in location x", where "location x" is the address of the object.
How &a is a pointer to a if it generates the address of a?
The terms pointer and address in C are synonymous.
When you create a variable:
int a = 0;
It is created in memory at a specific memory location, or address, with sufficient memory to hold 1 int.
&a does not generate the address, it simply provides the address.
The & operator can be used to set the address of a pointer to a specific location, such as this:
int a = 0;//a now exists in memory at a specific location;
int *b = {0}; // b is created in memory as a pointer, and can be assigned a location
b = &a; //b is assigned the location (address) of the variable a
Stated a little differently, in the previous line, & is referred to as the address-of operator, so:
b = &x; Can be Read: Assign to b (a pointer) the address of a.
Even after years of C, pointers still confuse me.
Are these two the same:
int *num;
someFunc(num)
and
int num;
someFunc(&num);
Declaring a variable with * makes it a pointer, & turns a variable (momentarily) into a pointer?
No they are not the same. In the first case you have a pointer which is pointing to some random memory location (as you have not initialized it). In the second case, the pointer obtained in someFunc will be pointing to a valid location (the address of variable num).
They are the same in terms of the function call. However
The difference is....
int num is an actual integer, which you take the address of. Which can then be placed into a pointer
int *num is a pointer to an int, however, with your code as is, it does not point to an actual int that can hold a value.
so only the second example is working code.
first one would be working if...
int x;
int *num = &x;
someFunc(num)
someFunc would look like
void someFunc(int *blah)
{
}
so basically int* num = &x is whats going on when you do the function call someFunc(&num) ie, its effectively just doing int* blah = &num
The second passes the address of the integer num, a perfectly reasonable thing to do. The first passes whatever happens to be stored in the pointer num. That should be the address of an int, but in your example num is uninitialized and the address probably points to garbage (at best).
& doesn't "turn a variable into a pointer", its an operator that returns the address of the variable. Which is, by definition, a pointer. Hence, no difference, the function someFunc receives a pointer value either way (in the first case it receives a copy the value of the pointer variable, in the second case it receives a copy of the return value of the operator &).
In the first case, you're declaring num to be a pointer to an integer. In the second case num is an integer.
To the someFunc function, in both cases the argument passed is a pointer to integer. So, to print the value you'll need to dereference printf("%d\n", *num).
In both cases the value would be some garbage since you haven't initialized.
Hope that helps.
Update On FreeBSD I got segmentation fault with the first one since the pointer which is not initialized may have pointed to somewhere that it is not supposed to.
As I know, when a pointer is passed into a function, it becomes merely a copy of the real pointer. Now, I want the real pointer to be changed without having to return a pointer from a function. For example:
int *ptr;
void allocateMemory(int *pointer)
{
pointer = malloc(sizeof(int));
}
allocateMemory(ptr);
Another thing, which is, how can I allocate memory to 2 or more dimensional arrays? Not by subscript, but by pointer arithmetic. Is this:
int array[2][3];
array[2][1] = 10;
the same as:
int **array;
*(*(array+2)+1) = 10
Also, why do I have to pass in the memory address of a pointer to a function, not the actual pointer itself. For example:
int *a;
why not:
allocateMemory(*a)
but
allocateMemory(a)
I know I always have to do this, but I really don't understand why. Please explain to me.
The last thing is, in a pointer like this:
int *a;
Is a the address of the memory containing the actual value, or the memory address of the pointer? I always think a is the memory address of the actual value it is pointing, but I am not sure about this. By the way, when printing such pointer like this:
printf("Is this address of integer it is pointing to?%p\n",a);
printf("Is this address of the pointer itself?%p\n",&a);
I'll try to tackle these one at a time:
Now, I want the real pointer to be changed without having to return a pointer from a function.
You need to use one more layer of indirection:
int *ptr;
void allocateMemory(int **pointer)
{
*pointer = malloc(sizeof(int));
}
allocateMemory(&ptr);
Here is a good explanation from the comp.lang.c FAQ.
Another thing, which is, how can I allocate memory to 2 or more dimensional arrays?
One allocation for the first dimension, and then a loop of allocations for the other dimension:
int **x = malloc(sizeof(int *) * 2);
for (i = 0; i < 2; i++)
x[i] = malloc(sizeof(int) * 3);
Again, here is link to this exact question from the comp.lang.c FAQ.
Is this:
int array[2][3];
array[2][1] = 10;
the same as:
int **array;
*(*(array+2)+1) = 10
ABSOLUTELY NOT. Pointers and arrays are different. You can sometimes use them interchangeably, however. Check out these questions from the comp.lang.c FAQ.
Also, why do I have to pass in the memory address of a pointer to a function, not the actual pointer itself?
why not:
allocateMemory(*a)
It's two things - C doesn't have pass-by-reference, except where you implement it yourself by passing pointers, and in this case also because a isn't initialized yet - if you were to dereference it, you would cause undefined behaviour. This problem is a similar case to this one, found in the comp.lang.c FAQ.
int *a;
Is a the address of the memory containing the actual value, or the memory address of the pointer?
That question doesn't really make sense to me, but I'll try to explain. a (when correctly initialized - your example here is not) is an address (the pointer itself). *a is the object being pointed to - in this case that would be an int.
By the way, when printing such pointer like this:
printf("Is this address of integer it is pointing to?%p\n",a);
printf("Is this address of the pointer itself?%p\n",&a);
Correct in both cases.
To answer your first question, you need to pass a pointer to a pointer. (int**)
To answer your second question, you can use that syntax to access a location in an existing array.
However, a nested array (int[][]) is not the same as a pointer to a pointer (int**)
To answer your third question:
Writing a passes the value of the variable a, which is a memory address.
Writing *a passes the value pointed to by the variable, which is an actual value, not a memory address.
If the function takes a pointer, that means it wants an address, not a value.
Therefore, you need to pass a, not *a.
Had a been a pointer to a pointer (int**), you would pass *a, not **a.
Your first question:
you could pass a pointer's address:
void allocateMemory(int **pointer) {
*pointer = malloc(sizeof(int));
}
int *ptr;
allocateMemory(&ptr);
or you can return a pointer value:
int *allocateMemory() {
return malloc(sizeof(int));
}
int *ptr = mallocateMemory();
I think you're a little confused about what a pointer actually is.
A pointer is just variable whose value represents an address in memory. So when we say that int *p is pointer to an integer, that just means p is a variable that holds a number that is the memory address of an int.
If you want a function to allocate a buffer of integers and change the value in the variable p, that function needs to know where in memory p is stored. So you have to give it a pointer to p (i.e., the memory address of p), which itself is a pointer to an integer, so what the function needs is a pointer to a pointer to an integer (i.e., a memory address where the function should store a number, which in turn is the memory address of the integers the function allocated), so
void allocateIntBuffer(int **pp)
{
// by doing "*pp = whatever" you're telling the compiler to store
// "whatever" not in the pp variable but in the memory address that
// the pp variable is holding.
*pp = malloc(...);
}
// call it like
int *p;
allocateIntBuffer(&p);
I think the key to your questions is to understand that there is nothing special about pointer variables. A pointer is a variable like any other, only that the value stored in that variable is used to represent a position in memory.
Note that returning a pointer or forcing the caller to move the pointer in an out of a void * temp variable is the only way you can make use of the void * type to allow your function to work with different pointer types. char **, int **, etc. are not convertible to void **. As such, I would advise against what you're trying to do, and instead use the return value for functions that need to update a pointer, unless your function by design only works with a specific type. In particular, simple malloc wrappers that try to change the interface to pass pointer-to-pointer types are inherently broken.