0

I am trying to create a pointer to dynamic array and thus need a function that takes in the size of the array and returns the pointer. However, I am getting error every time i do it. The function itself gives no error, but the allocation does.

Can someone point out what might be wrong? I have read many pages online but can't seem to figure out still. It would greatly help!

Edit: Updated code:

 int *ReturnBitVector(int bitvector_size); //static 
 int *ReturnBitVector(int bitvector_size) //static
{
   int *bitvector = malloc((bitvector_size*1024*8)*sizeof *bitvector);
   return bitvector; 
}
int* BV = (int*)ReturnBitVector; 

if(BV == NULL){ //error here!
    perror("error in allocating memory.\n");
}

The error I get:

join.c:71: error: expected identifier or ‘(’ before ‘if’
student001
  • 533
  • 1
  • 7
  • 20

3 Answers3

4

First, you dereferenced the pointer, correction below:

int* BV; BV = ReturnBitVector(1024);

Second, you forgot to specify the parameter type in your function definition. Shouldn't you have just copy pasted the header 2 lines above?

KevinZ
  • 3,036
  • 1
  • 18
  • 26
  • Hi. I get different errors when I follow what you suggest: join.c:70: error: conflicting types for ‘BV’ join.c:69: error: previous declaration of ‘BV’ was here join.c:70: warning: initialization makes integer from pointer without a cast join.c:70: error: initializer element is not constant – student001 Apr 10 '14 at 04:46
  • Read that second paragraph of mine from the answer again. Your definition of the function ReturnBitVector() is different from the declaration. You forgot to specify that the input is an int. Compiler thinks that you are trying to redefine that function. – KevinZ Apr 10 '14 at 05:15
  • I have changed it and it still persists. Thanks!! – student001 Apr 10 '14 at 06:18
  • OK, you changed those lines, but you changed them to the wrong thing. What you have currently, `int* BV = (int*)ReturnBitVector;` attempts to cast `ReturnBitVector` as a pointer to `int`. That is not what you want; you want to call the function instead of casting the function pointer as an integer pointer. Just use: `int* BV = ReturnBitVector(1024);` – KevinZ Apr 10 '14 at 16:54
1
int* BV;
BV = *ReturnBitVector(1024);

The function ReturnBitVector returns a value of type int *. You are dereferencing this point which evaluates to an integer and then assigning this integer value to BV which is of type int * - a different type. The compiler implicitly casts integer type to pointer type before assigning it and emits the warning -

join.c:69: warning: initialization makes pointer from integer without a cast

You have missed the type of the parameter in the function definition. Also, don't cast the result of malloc. Read this - Do I cast the result of malloc? I suggest the following changes -

// function declaration
int *ReturnBitVector(int bitvector_size);

// function definition
int *ReturnBitVector(int bitvector_size) 
{
   int *bitvector = malloc((bitvector_size*1024) * sizeof *bitvector);
   return bitvector;
}

// call the function

int *BV = ReturnBitVector(1024);
// check for NULL
if(BV == NULL) {
    perror("error in allocating memory.\n");
    // handle it
}

// do stuff with BV

free(BV);
BV = NULL;
Community
  • 1
  • 1
ajay
  • 9,402
  • 8
  • 44
  • 71
  • Hello. I get the following error. I think my compiler is C, the error I get is : join.c:65: warning: initialization from incompatible pointer type join.c:68: error: expected identifier or ‘(’ before ‘if’ – student001 Apr 10 '14 at 06:15
  • I think the value of BV is not being allocated right, even though it gives no more errors. – student001 Apr 10 '14 at 06:49
0

The problem is your function. The argument that it takes (bitvector_size) does not have a type. Try this way:

int *ReturnBitVector(int bitvector_size) //static
{
    int *bitvector;
    //bitvector   = new int[bitvector_size*1024*8];
    bitvector   = (int*) malloc(bitvector_size*1024*8);

    return bitvector;
}

int* BV;
BV = *ReturnBitVector(1024);
santahopar
  • 2,933
  • 2
  • 29
  • 50
  • Hello. When I do this, it gives no error. But I want to see if BV is null, and THAT command gives an error. if (BV == NULL){..print error..}; Do you have any idea? – student001 Apr 10 '14 at 08:41
  • The reason if (BV==NULL) returns an error is because BV has not been asssigned yet. When you declare a pointer you have to assign something to it. Either an address in memory or NULL. When you declare int* BV, it does not point anywhere. If you debug in visual studio int* BV value will be 0xCCCCCCCC (if your application is 32 bit). What is the problem you are trying to solve? – santahopar Apr 10 '14 at 22:26