0

I am trying to learn pointers by writing simple code snippets. I wrote the following today,

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

void funcall1(int *arr_p, int *num_elements_p)
{
  int i = 0;
  *num_elements_p = 10;
  int *temp = (int *)malloc(10 * sizeof(int));
  if (temp != NULL)
  {
    arr_p = (int *)temp;
  }
  else
  {
      free(arr_p);
      printf("Error\n");
      return;
  }
  printf("\n------------------------funcall1------------------------------\n");
  for (i=0; i<(*num_elements_p); i++)
  {
      arr_p[i]= i;
      printf ("%d\t", arr_p[i]);
  }

}
int main()
{
  int *arr = NULL;
  int num_elements = 0;
  int i = 0;
  /*int *temp = (int *)malloc(10 * sizeof(int));
  if (temp != NULL)
  {
      arr = (int *)temp;
  }
  else
  {
      free(arr);
      printf("Error\n");
      return;
  }*/

  funcall1(arr, &num_elements);
  printf("\n------------------------Main------------------------------\n");
  for (i=0; i<num_elements; i++)
  {
    printf ("%d\t", arr[i]);
  }
  printf ("\n");
  free(arr);
  return 0;
}

When I use malloc in the main function the code works as expected; but when I use it in the called function it doesn't, I get segmentation fault. I have researched a bit and understood few basics like, 1. Array name is actually a pointer to the first element in the array. So, I am passing the parameters correctly. 2. The array gets updated, because I am printing the array in the called function as well.

Since arr_p is in fact pointing to where arr points to, when I do "arr_p = (int *)temp" doesn't it mean, arr also points to this allocated memory space? I am looking for what happens in the memory and why do I get a memory access violation here? I don't want to convince myself with some partially derived assumptions.

Sara.0
  • 167
  • 3
  • 15
  • 6
    C is pass-by-value. – chris Jun 23 '14 at 23:53
  • 1
    Just to clarify further, `arr_p` is not the same as `arr`; when you assign a new value to `arr_p`, `arr` is still pointing to whatever it was pointing before. – vanza Jun 23 '14 at 23:55
  • *Since arr_p is in fact pointing to where arr points to...* Think of it this way. You take `int i`. You pass 5. Since they have the same value, should doing `i = 6;` change the original? – chris Jun 23 '14 at 23:56
  • I was trying to do pass-by-pointers for the arr, similar to what I did for num_elements. If C is pass by value only, how did num_elements get updated to 10? – Sara.0 Jun 24 '14 at 00:09
  • if you're trying to pass by pointer, like you did for num_elements, then it should look the same as the code that relates to num_elements. `funcall1(&arr, &num_elements);` and `void funcall1(int **arr_p, int *num_elements_p) { ... `. Then `*arr_p = (int *)temp;`, and so on. – Lee Jun 24 '14 at 00:20
  • 1
    @SaranyaDeviGanesan, The pointer gets passed by value. It stores the same address. You use that address to access the `int` indirectly. – chris Jun 24 '14 at 00:20
  • 1
    "Since arr_p is in fact pointing to where arr points to, when I do "arr_p = (int *)temp" ... you just threw out the very address that made the first part of this sentence true. – WhozCraig Jun 24 '14 at 03:03

0 Answers0