Incrementing array of pointers - c

I've been trying to increment array of pointers to and assigning them to be addresses of an array but I can't seem to figure it out.
Input:
void * myParam(unsigned int *argv[]){
for(unsigned int i = 0; i < 5; i++){
printf("Block[%d] = Pointer Address %x\n", i, *(argv + i));
}
return *argv;
}
int main(){
unsigned int arr[5] = {1,2,3,4,5};
unsigned int *ptr;
ptr = arr;
ptr = myParam(&ptr);
}
Output:
Block[0] = Pointer Address ea6d6b60
Block[1] = Pointer Address 1
Block[2] = Pointer Address 3
Block[3] = Pointer Address 5
Block[4] = Pointer Address f6770089

You don't have an array of pointers. You have a pointer to one pointer (=an array of one pointer).
Subscripting that argv with anything but 0 (or 1 if you don't dereference that) is undefined. After you dereference it once, you can add 0 through 4 (and possibly dereference those to obtain the array values (1 through 5)).
#include <stdio.h>
void * myParam(unsigned int *argv[]){
for(unsigned int i = 0; i < 5; i++){
printf("Block[%u] = Pointer Address %p, value=%u\n", i, (void*)(*argv + i),
(*argv)[i]);
}
return *argv;
}
int main(){
unsigned int arr[5] = {1,2,3,4,5};
unsigned int *ptr;
ptr = arr;
ptr = myParam(&ptr);
}

Related

What does **p mean in c? [duplicate]

This question already has answers here:
Why use double indirection? or Why use pointers to pointers?
(18 answers)
Closed 1 year ago.
I know that int *p is a pointer but what does int **p mean exactly? What type of value is that? When I say now p= something, how is that working? I am seeing this in the creation of two-dimensional arrays with pointers.
In short, int **p; is a pointer to a pointer to an int. So, for example:
int i, j; // Integers;
int *p = &i; // Pointer to i
*p = 1; // i is now 1
int *q = &j; // Pointer to j
int **s = &p; // Pointer to p
**s = 2; // i is now 2
s = &q; // s now points to q (pointing to j)
**s = 16; // j is now 16
s = &p; // s now points to p (pointing to i)
**s = 3; // i is now 3
p = &j; // p now points to j
**s = 17; // j is now 17
One use case for a pointer to pointer could be a function which needs to output a pointer to an int and return a success/failure status:
#include <stdbool.h>
bool getIntHandle(unsigned handleId, int **handle)
{
static int handles[] = {12, 23, 34, 45};
bool success = (handleId < sizeof(handles)/sizeof(handles[0])); // Check that handleId is in range
if(success)
{
*handle = &handles[handleId];
}
return success;
}
int main(void)
{
int *handle;
bool success = getIntHandle(2, &handle); // Get a pointer to the integer at index 2
printf("*handle = %d\n", *handle);
return 0;
}
p is a pointer to a pointer to an int. It's the type int **p and the variable p stores an address.
Here is an example of its use. p is an array of two integer pointers. The first of the pointers p[0] points to an array of 3 integers, and the 2nd to an array of 4 integers (combined this is known as ragged array):
#include <stdio.h>
#include <stdlib.h>
int main() {
int **p = malloc(2 * sizeof(int *));
printf("p = %p\n", p);
p[0] = malloc(3 * sizeof(int));
printf("p[0] = %p\n", p[0]);
p[0][0] = 0;
p[0][1] = 1;
p[0][2] = 2;
p[1] = malloc(4 * sizeof(int));
printf("p[1] = %p\n", p[1]);
p[1][0] = 3;
p[1][1] = 4;
p[1][2] = 5;
p[1][3] = 6;
free(p[0]);
free(p[1]);
free(p);
return 0;
}
The most common use, however, is with a regular 2d array that is passed to a function which degrades to a pointer to a pointer.

why * double pointer value is same with * pointer array?

I have a question with double pointer to pointer array.
I couldn't understand why is *double_pointer value same with *pointer_array value.
I thought maybe there's something with this line, 'int **pptr = ptr'.
why isn't it 'int **pptr = &ptr' ?
Please help me..
#include <stdio.h>
int main(){
// doulble pointer with array.
int arr[4] = {1,2,3,4};
int *pt = arr;
int **ppt = &pt;
printf("%p, %d\n", *ppt, *pt);
// The result is different. *ppt != *pt
// double pointer with pointer array.
int ptr1[4] = {1,2,3,4};
int ptr2[4] = {5,6,7,8};
int ptr3[4] = {9,10,11,12};
int * ptr[3] = {ptr1, ptr2, ptr3};
int ** pptr = ptr;
// Why is it ptr? I thought it should be &ptr.
printf("%p, %p\n", *pptr, *ptr);
// The result is same. *pptr = *ptr
// Why *pptr is the address of ptr1? It thought should be the address of ptr.
return 0;
}

How to access a value pointed by pointer to pointer

I want to access the pointer to an array of pointers
I am successfullay able to map the top and the bottom block
unsigned int *outputAddress = NULL;
unsigned int *outputOffsetAddress = NULL;
unsigned int *OutputptrptrAddr = NULL;
unsigned int *PtrArr[250];
unsigned int **val = PtrArr;
void MemoryMapping(unsigned int outputOffsetRTI)
{
unsigned int *memBase;
memBase = (unsigned int *)malloc(2000);
outputAddress = (unsigned int *)(memBase + outputOffsetRTI);
for (int x = 0; x < 5; x++)
{
*outputAddress = 123;
*outputAddress++;
}
outputAddress = outputAddress - 5;
for (int x = 0; x < 5; x++)
{
PtrArr[x] = (unsigned int *)outputAddress;
outputAddress += 1;
}
outputOffsetAddress = outputAddress + 250;
for (int x = 0; x < 5; x++)
outputOffsetAddress[x] = (unsigned int)PtrArr[x];
}
How to tranverse through the input pointer block to get all values from the input block?
You need to dereference (e.g. *ptr) to access a value through a pointer. The following block illustrates the access you mention. But using triple pointers is not a good idea: Triple pointers in C: is it a matter of style?
// This is the pointer to an array of pointers
unsigned int *** input;
// Dereference it to get the array base
unsigned int ** ptr_array = *input;
// You can use a temporary pointer to iterate over the array
unsigned int * ptr;
// This will be the data pointed to
unsigned int data;
// You need to know the array size as well, assuming it is 5 here
for (int x=0; x<5; x++) {
// The array contains pointers
ptr = ptr_array[x];
// Dereference the pointer to reach the data
data = *ptr;
}

Dereferencing a double pointer

I have code snippet that I can't understand how it works, because of one line that does a double dereference. The code looks like this:
void afunction(int**x){
*x = malloc(2 * sizeof(int));
**x = 12;
*(*x + 1) = 13;
}
int main(){
int *v = 10;
afunction(&v);
printf("%d %d\n", v[0], v[1]);
return 1;
}
I understand that the first element of the pointer to pointer gets the value 12, but the line after that I just can't seem to understand. Does the second element in the first pointer get value 13?
The code is rather easy to understand if you use a temporary variable, eg:
void afunction(int**x)
{
int* t = *x;
t = malloc(2 * sizeof(int));
*t = 12;
*(t+1) = 13;
}
so:
x is a pointer to a pointer to int
*x yields a int* (pointer to int)
**x = is like *(*x) = so you first obtain the pointer to int then by dereferencing you are able to set the value at the address
The last part *(*x+1) = can be broken down:
int* pointerToIntArray = *x;
int* secondElementInArray = pointerToIntArray + 1;
*secondElementInArray = 13;
The purpose of using a pointer to pointer here is that you can pass the address to an int* to the function and let the function allocate the memory and fill it with data. The same purpose could be done by returning an int*, eg:
int* afunction() {
int* x = malloc(sizeof(int)*2);
*x = 12;
*(x+1) = 13;
return x;
}
int main() {
int* v = afunction();
return 0;
}

Sum an array in C, what it should be inserted in the (???) to complete the function?

int arraySum (int array[], int n)
{
int sum = 0, *ptr;
for (ptr = array; ???; ++ptr)
sum += ???
return sum;
}
int n Is the size of the array.
Since it seems to be your homework / assignment, I'll just give you 2 hints and let it finish you on your own:
Hint 1:
When you have a pointer to int: int* ptr, you can access the int it points to by dereferencing your pointer using dereference operator: *ptr
Hint 2:
When you increment ptr by 1 3 times, it will point to the same memory as ptr + 3 does. Note that ptr + 3 points to the memory at the address &ptr[3].
int arraySum (int array[], int n)
{
int sum = 0, *ptr;
for (ptr = array; ptr - array < n; ++ptr)
sum += *ptr;
return sum;
}
The first ??? should be replaced with ptr < &array[n], the second with *ptr.
A pointer is an address in memory. array contains an address (where array[0] is located).
So array + i is a pointer to array[i].
You want to travel on the array until you reach the last object, since we're comparing addresses in the for loop, so we compare ptr with the address of the last element in the array. So your program will look:
int arraySum (int array[], int n)
{
int sum = 0, *ptr;
for (ptr = array; ptr < &array[n]; ++ptr)
sum += *ptr;
return sum;
}

Resources