0

I new to pointers so i have a bit of a problem. i have a simple program to outprint random int values betwenn 0-100. But its only printing the first value and after that i'm getting a Segmentation fault (core dumped)

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

void printArray(int **array, int size) 
{
  int i;
  for (i=0; i<size; i++)
  {
    printf("index[%d] = %i \n",i,*array[i]);
  }
}

int main() 
{   
  int size = 10;
  int *array = (int *) malloc (sizeof(size));
  int i;
  for (i=0; i<size; i++) 
  {
    array[i] = rand() % 100 + 1;
  }

  printArray(&array,size);
  free(array);

  return 0;

}

I'm really not sure why, any help. thanks :)

3
  • I wouldn't advise casting the result of malloc. Commented Apr 7, 2013 at 20:51
  • Wrong -> int *array = (int *) malloc (sizeof(size)); Right -> int *array = malloc (sizeof(*array * size)); Commented Apr 7, 2013 at 20:51
  • 2
    A little advice: Unless you intend to change the pointer itself, you should just pass the pointer itself instead of a pointer to it. That way, you save yourself the & and two * (and some confusion) Commented Apr 7, 2013 at 20:53

2 Answers 2

4

You're seeing the effects of operator precedence. operator[] comes before operator*. Thus, you need to put it in parentheses:

printf("index[%d] = %i \n",i,(*array)[i]);

As it is, it takes index 0, your array, and dereferences the pointer to get the first element, then moves onto the next array (it doesn't exist), dereferences that pointer, and BOOM! Undefined behaviour.

Credits go to onon15 for being first, but your array isn't actually an array. It's a single element (because sizeof(size) happens to be the same as sizeof(*array) here). You want something like:

int *array = malloc(sizeof(int) * size); 
Sign up to request clarification or add additional context in comments.

5 Comments

@EdS., So I've noticed now :p
@EdS., Funny how the behaviour for the printing is still well-defined for the first output (if only that is accessed) despite that. Either way, it can access the first number ok and then it screws up. Also, geez, I swear the new answer thing popped up first, but the times say it all.
Yep, there's room for one (though UB has already been invoked in the loop before printArray. And yeah, his answer was first, I checked the time after leaving that comment.
@EdS., Screw my brain today. I thought more time ago meant later. Don't feel bad, you still have the uncasted malloc :p
Meh, everybody has a right to a mental blip once in a while. At 19 you're one of the brightest and most knowledgeable members on the site. If you are ever looking for a job in SD, let me know :D
2
int *array = (int *) malloc (sizeof(size));

size is int. So sizeof(size) is the size of one int, not an array of 10 ints. You probably meant to do

int *array = (int *) malloc (size * sizeof(int));

or better yet,

int *array = (int *) calloc(size, sizeof(int));

1 Comment

Good catch. I didn't see that.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.