Save pointer value

As I know, when a pointer is passed to a function, it becomes just a copy of the real pointer. Now I want to change the real pointer without returning the pointer from the function. For instance:

int *ptr; void allocateMemory(int *pointer) { pointer = malloc(sizeof(int)); } allocateMemory(ptr); 

Another thing that is, how can I allocate memory for 2 or more dimensional arrays? Not by index, but by pointer arithmetic. It:

 int array[2][3]; array[2][1] = 10; 

same as:

 int **array; *(*(array+2)+1) = 10 

In addition, why do I need to pass the memory address of the function pointer, and not the pointer itself. For instance:

int * a;

why not:

 allocateMemory(*a) 

but

 allocateMemory(a) 

I know that I always need to do this, but I really don’t understand why. Please explain me.

Last, in a pointer like this:

 int *a; 

Is the memory address containing the actual value, or is the memory address of the pointer? I always think that this is the memory address of the actual value that it indicates, but I'm not sure about that. By the way, when printing such a 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); 
+4
source share
5 answers

I will try to solve this problem in one go:

  • Now I want the real pointer to be changed without having to return a pointer from the function.

    You need to use another 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 .

  • Other that I can allocate memory for 2 or more dimensional arrays?

    One selection for the first dimension, and then a distribution loop for another dimension:

     int **x = malloc(sizeof(int *) * 2); for (i = 0; i < 2; i++) x[i] = malloc(sizeof(int) * 3); 

    Again, here is a link to this exact question from comp. lang.c FAQ .

  • It:

     int array[2][3]; array[2][1] = 10; 

    same as:

     int **array; *(*(array+2)+1) = 10 

    ABSOLUTELY NOT. Pointers and arrays are different. However, sometimes they can be used interchangeably. Check out these questions from the comp.lang.c FAQ .

  • Also, why do I need to pass a pointer to a function to the memory address, and not an actual pointer?

    why not:

     allocateMemory(*a) 

    These are two things: C does not have a pass-by-reference, unless you implement it yourself by passing pointers, in this case also because a is not yet initialized - if you want to dereference it, you can invoke undefined behavior . This problem is similar to this found in the comp.lang.c FAQ .

  •  int *a; 

    Is the memory address containing the actual value, or is the memory address of the pointer?

    This question does not make sense to me, but I will try to explain. a (with proper initialization - your example is not here) - this is the address (the pointer itself). *a is the object that it points to - in this case it will be int .

  • By the way, when printing such a 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.

+10
source

To answer your first question, you need to pass a pointer to a pointer. ( int** )

To answer the second question, you can use this syntax to access a location in an existing array.
However, the nested array ( int[][] ) does not match the pointer to the pointer ( int** )

To answer the third question:

The entry a passes the value of the variable a , which is the memory address.
The entry *a passes the value pointed to by the variable, which is the actual value, not the memory address.

If the function accepts a pointer, it means that it wants an address, not a value.
So you need to go through a , not *a .
If a were a pointer to a pointer ( int** ), you would pass *a rather than **a .

+1
source

Your first question:

you can pass the address of the pointer:

 void allocateMemory(int **pointer) { *pointer = malloc(sizeof(int)); } int *ptr; allocateMemory(&ptr); 

or you can return the value of the pointer:

 int *allocateMemory() { return malloc(sizeof(int)); } int *ptr = mallocateMemory(); 
0
source

I think you are a little confused by the fact that there is actually a pointer.
A pointer is simply a variable whose value is an address in memory. Therefore, when we say that int *p is a pointer to an integer, this means that p is a variable that contains a number, which is the memory address of int .
If you want the function to allocate a buffer of integers and change the value in the variable p , this function must know where the memory p is stored. Therefore, you need to point to a pointer to p (that is, memory address p ), which itself is a pointer to an integer, so what the function requires is a pointer to a pointer to an integer (i.e. the memory address where the function should store the number , which, in turn, is the memory address of integers allocated by the function), therefore

 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 this variable is used to represent the position in memory.

0
source

Note that returning a pointer or forcing the caller to move the pointer in the void * temp variable is only because you can use the void * type to allow it to work with different types of pointers. char ** , int ** , etc. do not convert to void ** . So, I would advise against what you are trying to do, and instead use the return value for functions that should update the pointer, if only your design function only works with a specific type. In particular, the simple malloc shells that try to change the interface for passing pointer types to pointers are inherently broken.

0
source

Source: https://habr.com/ru/post/1335125/


All Articles