Using arrays and pointers
I have few questions about how to use pointers. I have the following code.
float* arr;
arr = (float*) malloc(4*sizeof(float));
float temp[4] = {1,2,3,4};
I want to point arr
to an array temp
. How should I do it? Is it correct arr=&temp
?
The output I want is arr
one pointing to a 1D array {1,2,3,4}
.
source to share
I want to point
arr
to an arraytemp
.
Then there is no need to allocate memory for arr
. Just use arr = temp;
and you're good to go.
Otherwise, first, if you allocate memory and then assign another pointer, you will lose the allocated memory by addressing a memory leak .
As suggested in the comment by Mr. barak manos , it is worth mentioning that in the above manner it arr
actually points to temp
. Now if this assignment is done inside a function and temp
is local to the function, the return arr
will be UB.
If the case arises, then you should use malloc()
both memcpy()
to allocate memory arr
and copy the contents temp
to that memory, which has a lifespan before being freed, so it can be returned from the function.
source to share
Your comments are a little confusing, you use the verb "point" when talking about arrays, which doesn't make sense. Pointers can point to things, but arrays are not pointers, so they cannot. The array name acts as a pointer to the first element of the array in some contexts, but it cannot be changed.
To copy an array to dynamic memory, you can do:
const float arr[] = { 1.f, 2.f, 4.f, 8.f };
float * const ptr = malloc(sizeof arr);
memcpy(ptr, arr, sizeof arr);
Then you can print a copy:
for(size_t i = 0; i < sizeof arr / sizeof *arr; ++i)
print("Element #%zu is %f\n", i, ptr[i]);
source to share
You can simply assign the base address of the "temp" array to the "arr" pointer by simply doing
arr = temp;
Alternatively, you can set the "arr" pointer to store the address of an individual member of the array by doing
arr = &temp[0]
arr = &temp[1] ... etc.
To print the contents of an array when the pointer is set to the base address of the array, you can use the * operator, or simply use pointer indexing.
printf("array element 1 = %f, element 2 = %f, element 3 = %f, ...",*arr,*(arr+1),*(arr+2), ...);
printf("array element 1 = %f, element 2 = %f, element 3 = %f, ...",arr[0],arr[1],arr[2], ...);
Also remember that a "pointer array" is an array of pointers. Each pointer in this array can point to a memory address that is independent of other pointers in the array. Therefore, there is no need to allocate memory to a pointer if you want to assign a valid memory address to it.
source to share
Notice when you declared temp
as float temp[4] = ...
, then the variable temp
itself is of type float*
(pointer)
which means that the assignment arr = temp
will make it arr
point to the same (static) array as arr
.
You can play with this by dispatching functions and observing the behavior.
I've already done this demo for you:
#include <stdio.h>
#include <stdlib.h>
void printTmp(int n, int tmp[n]);
int main() {
printf(" in main\n");
int* arr= malloc(4*sizeof(int));
int temp[4] = {1,2,3,4};
arr = temp;
for(int i = 0; i < 4; i++) {
printf("%d, ",arr[i]);
}
printTmp(4, temp);
printTmp(4, arr);
return 0;
}
void printTmp(int n, int tmp[n]) {
printf("\n \n in Print\n");
for(int i = 0; i < n; i++) {
printf("%d, ", tmp[i]);
}
}
which gives the output:
in main
1, 2, 3, 4,
in Print
1, 2, 3, 4,
in Print
1, 2, 3, 4,
source to share