Fredamabob
Fredamabob

Reputation: 1

Why does this C code segfault?

I keep getting a segmentation fault in this code:

#include <stdio.h> 

void FillArray(int *array, int);
#define MAX 256

int main()
{
    int *array[MAX], size = 100;
    FillArray(*array, size);
    return 0;
}

void FillArray(int *array, int size)
{
    int i, temp;
    for (i = 0; i < size; i ++)
    {
        temp = (rand()%101);
        *array = temp;
        printf ("array[%d]. %d\n", i, *array);
        array += i;
    }
    printf ("AJGIUEROGUSHFDJGJDFK/n");
}

I put the printf on the last line so that i could tell if it would reach that point, so far it hasn't.

Edit: I added code. I have to use pointer arithmetic instead of array indexes.

Upvotes: 0

Views: 132

Answers (6)

Anvesh Yalamarthy
Anvesh Yalamarthy

Reputation: 1693

Code is fine except for your perception that *array = &array, which is wrong !!

Below points might help in understating pointers better:

  1. *array = array[0]
  2. array = &array[0]
  3. *(array+i) = array[i]

Made changes to your code and it should work fine:

#include <stdio.h> 

void FillArray(int *array, int);
#define MAX 256

int main()
{
    int *array, size = 100;
    array=(int *)calloc(MAX,sizeof(int));
    if(array !=NULL)
    {
        FillArray(array, size);    /* While calling send &array[0] but not array[0] */
    }
    return 0;
}

void FillArray(int *array, int size)
{
    int i, temp;
    for (i = 0; i < size; i ++)
    {
        temp = (rand()%101);
        *(array+i) = temp;
        printf ("array[%d]. %d\n", i, *(array+i));
        /* array += i;             <-- not necessary */
    }
    printf ("AJGIUEROGUSHFDJGJDFK/n");
}

Upvotes: 0

AnT stands with Russia
AnT stands with Russia

Reputation: 320531

Your array in main is declared as an array of int * pointers. This array is not initialized, i.e. all elements contain garbage values.

Layer your FillArray call in main

FillArray(*array, size);

passes the value of *array to FillArray function. *array is the same as array[0] - it is an uninitialized garbage pointer that points nowhere.

Inside FillArray function you are attempting to access (and write) data through that uninitialized garbage pointer. Expectedly, the code crashes.

As is always the case with invalid code, there's no way to fix the error until you explain what you are trying to do.

I can only guess that all you needed is an array of int elements, not int * elements. I.e. your array in main was supposed to be declared as int array[MAX]. And FillArray should have been called as FillArray(array, size). Also, inside the cycle it is supposed to be array += 1 (or just ++array), not your array += i, which does not make any sense.

Upvotes: 2

unwind
unwind

Reputation: 399863

Your loop should just be:

int i, temp;
for (i = 0; i < size; i ++)
{
    temp = rand() % 101;
    array[i] = temp;
    printf ("array[%d] = %d\n", i, array[i]);
}

This will do what you want. There's no need to re-assign array inside the function, although you can. It's easier to just use the indexing operator []. Remember that

a[i]

is the same as

*(a + i)

regardless of the types involved (but generally a is a pointer type and i an unsigned integer) as long as the sum is a pointer of course.

There are errors in main(), too:

  • The array should just be int array[MAX];.
  • The call should just be FillArray(array, size);.

Upvotes: 1

haccks
haccks

Reputation: 106012

If wanna fill the array passed to your function, then change

array = &temp; 

to

*array = temp; 

And also change

array += i;  

to

array++;  

EDIT: OP edited his question and want to fill an array of integers. You need to chage the declaration of your array

 int *array[MAX], size = 100;  // Declare an array of pointers

to

 int array[MAX], size = 100;   // Declates an array of ints 

Upvotes: 1

alk
alk

Reputation: 70941

int *array[MAX] is an array of MAX pointers to int, from which you pass the 1st to the function. There are no ints defined where the latter points to.

To fix this appliy the changes void FillArray(int *array, int size) provided by the other answers and then call it like this:

int main(void)
{
  int array[MAX], size = 100;
  FillArray(array, size);
  return 0;
}

Upvotes: 0

BLUEPIXY
BLUEPIXY

Reputation: 40145

probably your want.

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

void FillArray(int *array, int);
#define MAX 256

int main(){
    int array[MAX], size = 100;
    FillArray(array, size);
    return 0;
}

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

Upvotes: 0

Related Questions