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}

.

+3


source to share


6 answers


I want to point arr

to an array temp

.

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.

+4


source


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]);

      

+1


source


#include <stdio.h>
#include <stdlib.h>

int main()
{
     float *arr;
     int i;
     float temp[4] = {1,2,3,4};
     arr=temp;    //initializing with base address of temp
     for(i=0;i<4;i++)
       {
          printf("%f",arr[i]);
       }
    return 0;
}

      

This will print the result.

0


source


I think this is what you need. Solution using an array of pointers

    #include<stdio.h>
    #include<stdlib.h>
    int main()
    {
       float *arr[4];
       float temp[4]={1,2,3,4};
       int i;
       for(i=0;i<4;i++){
            arr[i]=&temp[i];
            printf("%f\n",*(arr[i]));
       }

       return 0;
    }

      

0


source


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.

0


source


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,

      

0


source







All Articles