Minions
Minions

Reputation: 1313

Using arrays and pointers

I have few questions with how to use the 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 the arr to the temp array. How do I do it? Is arr=&temp correct?

The output I want is arr pointing to the 1D array {1,2,3,4}.

Upvotes: 3

Views: 91

Answers (6)

ThunderWiring
ThunderWiring

Reputation: 738

Pay attention when you declared temp as float temp[4] = ... then the variable temp by itself is of type float*(pointer)

which means, the assignment arr = temp would make arr point to the same (static) array as arr does.

You can play around with this by sending them to a function and observe the behavior.

i already made 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 yields the output:

  in main
1, 2, 3, 4, 

 in Print
1, 2, 3, 4, 

 in Print
1, 2, 3, 4,

Upvotes: 0

user806168
user806168

Reputation: 477

You can simply assign the base address of the "temp" array to the "arr" pointer by simply doing

arr = temp; 

Furthermore, you can set the "arr" pointer to hold the address of an individual member of the array by doing

arr = &temp[0] 
arr = &temp[1] ... etc.

To print the contents of the array once the pointer is set to the array's base address you can use the * operator or simply use indexing with the pointer.

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 a "pointer array" is an array of pointers. Each pointer in this array can point to an memory address independent of the other pointers in the array. So no need to allocate memory to the pointer if you want to assign it to a valid memory address.

Upvotes: 0

Sourav Ghosh
Sourav Ghosh

Reputation: 134396

I want to point the arr to the temp array.

Then, no need to allocate memory to arr. Just use arr = temp; and you're good to go.

Otherwise, first if you allocate memory and then you assign another pointer, you'll be losing the allocated memory, facing a memory leak.


As per the suggestion in the comment by Mr. barak manos, it's worthy to mention that in the way described above, arr actually points to temp. Now, if this assignment is done inside a function and temp is local to the function, returning arr will be UB.

If the case arises, then, you have to use malloc() and memcpy() to allocate memory to arr and copy the contents of temp to that memory which has a lifetime until deallocated, so can be returned from a function.

Upvotes: 4

Merp
Merp

Reputation: 65

I think this is what you need.Solution using 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;
    }

Upvotes: 0

unwind
unwind

Reputation: 400129

Your comments are bit confusing, you use the verb "point" when talking about arrays, which doesn't make sense. Pointers can be made to point at things, but arrays are not pointers so they cannot. The name of an array acts as a pointer to the array's first element in some contexts, but it cannot be changed.

To copy the array into 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 the copy:

for(size_t i = 0; i < sizeof arr / sizeof *arr; ++i)
  print("Element #%zu is %f\n", i, ptr[i]);

Upvotes: 1

ameyCU
ameyCU

Reputation: 16607

#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 output .

Upvotes: 0

Related Questions