0

Trying to make fill an array of pointers of a known size with blocks of memory I can later fill with names/pointers to said names. but when i go to compile it returns the following warning: assignment makes integer from pointer without a cast 'opList[0] = (int*)malloc(sizeof(char)....'

any idea whats going on here and how to clear the warning?

#define QUE_SIZE 256
#define MAX_REWARDED 21
#define TWITCH_MAX 1 /*Temp*/

int main ()
{
 int* opList[5] = {NULL};
 setup(&opList);
}

void setup(int* opList)
{
  int *x;
  int *y;

  opList[0] = (int*)malloc(sizeof(char) * QUE_SIZE * TWITCH_MAX);     /*currentNames*/
  opList[1] = (int*)malloc(sizeof(int) * MAX_REWARDED); /*correctAnswers*/
  opList[2] = (int*)malloc(sizeof(int) * QUE_SIZE);     /*chronoQue*/
  opList[3] = (int*)malloc(sizeof(int) * QUE_SIZE);     /*rewardQue*/
  opList[4] = NULL; /*End in NULL to make garbage collection easy*/

}
Ayakano
  • 9
  • 1

3 Answers3

0
  1. void setup(int* opList) is not correct. Chnage it to void setup(int* opList[5])

  2. call the setup() as setup(opList);

  3. add a forward declaration of setup().

Note : Please do not cast the return value of malloc().

Corrected code

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

#define QUE_SIZE 256
#define MAX_REWARDED 21
#define TWITCH_MAX 1 /*Temp*/

void setup(int* opList[5]);

int main ()
{
 int* opList[5] = {NULL};
 setup(opList);
}

void setup(int* opList[5])
{
//  int *x;
//  int *y;

  opList[0] = malloc(sizeof(char) * QUE_SIZE * TWITCH_MAX);     /*currentNames*/
  opList[1] = malloc(sizeof(int) * MAX_REWARDED); /*correctAnswers*/
  opList[2] = malloc(sizeof(int) * QUE_SIZE);     /*chronoQue*/
  opList[3] = malloc(sizeof(int) * QUE_SIZE);     /*rewardQue*/
  opList[4] = NULL; /*End in NULL to make garbage collection easy*/

}
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0

Compile with warnings:

note: expected ‘int *’ but argument is of type ‘int * (*)[5]’

Change

void setup(int* opList)

to

void setup(int* opList[5])

call it using setup(opList);

And opList[0] = (int*)malloc(sizeof(char) * QUE_SIZE * TWITCH_MAX); may be wrong if QUE_SIZE * TWITCH_MAX is not a multiple of sizeof(int)

David Ranieri
  • 39,972
  • 7
  • 52
  • 94
0

In the setup function definition, you declare opList as a simple pointer to int, so the expression opList[i] will have type int, hence the warning.

The expression &opList has type int *(*)[5], or "pointer to 5-element array of pointer to int", which is not necessarily what you need in this case.

You can simply pass opList in the function call (no & necessary), but you will need to change the prototype to int **opList:

setup( opList ); // function call

void setup( int **opList )
{
  opList[i] = malloc(...); // cast not necessary in C
  ...
}

Except when it's the operand of the sizeof or unary & operators, an expression of type "array of T" will be converted to an expression of type "pointer to T", and the value of the expression is the address of the first element. In this case, T is int *. That's why the prototype is int **opList. Note that the expression opList[i] will have type int *.

I'm worried about you storing text data in opList[0]; that's going to require some casting gymnastics later on, but we'll deal with that when it becomes a problem.

John Bode
  • 119,563
  • 19
  • 122
  • 198