Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a function, and I know how to print and change every element, how I can return array in index without print? Or create one more function only for return arr, for example [3]

What I have tried:

void a(int arr[], int arr_el, int i)
{
printf(“%d\n”, arr[i]);
}




int main(){
int arr[3] = {24, 6, 18};
int arr_el=3;
int i;
a();
Posted
Updated 20-Dec-22 12:53pm

Try:
C
int* returnArray(int arr[])
   {
   ...
   return arr;
   }
   
int main()
{
    printf("Hello World\n");
    int a[3] = {1, 3, 5};
    int *b = returnArray(a);
    printf("%d\n", b[1]);
    return 0;
}
An array and a pointer are very similar: the name of an array is a pointer to the first element (according to the language definition).
So if you have an array, you can treat it like a pointer, and vice versa
 
Share this answer
 
Returning just one item of an array is pretty straightfoward, e.g.
C++
#include <stdio.h>                                                                                                     #include <stdlib.h>                                                                                                                                                                                                                             int get_item( int a[], size_t size, size_t index)                                                                       {                                                                                                                         
  if ( index < size )                                                                                                       
    return a[index];                                                                                                      
  else                                                                                                                      
    return 0; // there is actually no meaningful way to signal the error                                                          }                                                                                                                                                                                                                                               
int main()                                                                                                              {                                                                                                                         
  int arr[] = {1, 42, -12};                                                                                               
  printf("arr[%u] = %d\n", 1, get_item(arr, 3, 1));                                                                       
  return 0;                                                                                                             }                                                                                                                       
 
Share this answer
 
You can return an array in this way:
C++
int* getArray() {
  int *array = malloc( 5 * sizeof(int));
  // do stuff with that array
  return array
} 
important: the returned array needs memory managment so use free, when done. Read about new and free.

Remark: C++ has built-in arrays like vector.
 
Share this answer
 
Comments
Member 15858418 20-Dec-22 4:12am    
Thanks

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900