-1

I am trying to use struct called BankAccount it takes char array of name 50 letters long , int ID, and double balance. I am trying to use malloc() to put it to pointer but i get this

error : void can not be assigned to an entity of type BankAccount.
typedef struct
{
    char name[50];
    int ID;
    double balance;
} BankAccount;

FILE *fp;
BankAccount *accounts = 0;
int accountSize = 0;
// I init like this in main function

accounts = malloc(accountSize * sizeof(*accounts));



void ReadAccountData(BankAccount *accounts, FILE *fp, int arraySize)
{
int i = 0;
while (!feof && i < arraySize) {
    fread(&accounts, sizeof(accounts), i, fp);
    i++;
 }

  }
rev2k9
  • 1
  • 3

2 Answers2

1

If you are using C language, you need to use C compiler for your code. The compiler you are using now looks like C++ compiler, which requires casting void * to destined type. In C you shouldn't do this, as pointed out by @Olaf (Why you can't cast void * in C).

How to change compilator? It depends on IDE and operating system, that you are using, this needs to be specified before I can give any more details.

If you want to use C++ you have few options:

  1. Use malloc, but cast return value:

    accounts = static_cast<BankAccount *>(
                   malloc(accountSize * sizeof(BankAccount)));
    
  2. Use dynamic allocation dedicated for C++ and treat BankAccount as C++ structure(you can add constructors, destructors, methods etc.):

    accounts = new BankAccount[accountSize];
    
Community
  • 1
  • 1
zoska
  • 1,684
  • 11
  • 23
-2

Should be like this:

accounts = (BankAccount *)malloc(accountSize * sizeof(BankAccount));
Afshin
  • 8,839
  • 1
  • 18
  • 53
  • 3
    Do not cast `void *` in C! – too honest for this site Dec 01 '15 at 21:29
  • 1
    If the OP intended to use c++ it should be `static_cast(malloc(...))` and more likely, `BankAccount` could have a constructor, so `new BankAccount[accountSize]`. – Iharob Al Asimi Dec 01 '15 at 21:29
  • in C we don't have constructor. But still I don't get way @Olaf says my answer is incorrect. – Afshin Dec 01 '15 at 21:34
  • 2
    [You don't cast `void *` in c, that's it](http://stackoverflow.com/a/605858/1983495). – Iharob Al Asimi Dec 01 '15 at 21:36
  • @iharob: Did not say to use a constructor in C, but in case he confuses C with C++ to use one for the class. But as this seems to be C code: `malloc` & friends return `void *`. And you should not cast this to/from another pointer type **in C**. In C++ you have to (but should not use these functions at all). – too honest for this site Dec 01 '15 at 21:37
  • @Olaf if you should not cast `void *` from `malloc()` in C,can you give an example for using result of `malloc()`? Because I have always wrote my code like this. I will be happy to learn something new. – Afshin Dec 01 '15 at 21:39
  • Please read the link @iharob provided. This has been answered here sooooooo many time already. For using the result of `malloc`: `int *p = malloc(sizeof *p); free(p);` So? Note this simply follows from not casting without need **and** exactly knowledege why **and** **all** implications. this is not different from C++ or any other language.. – too honest for this site Dec 01 '15 at 21:41
  • @Olaf Anyway, this was completely something new for me which differs from what is specified in cplusplus :http://www.cplusplus.com/reference/cstdlib/malloc/ And MSDN: https://msdn.microsoft.com/en-us/library/6ewkz86d.aspx?f=255&MSPPError=-2147217396 – Afshin Dec 01 '15 at 21:50
  • cplusplus is for c plus plus c++, that is different from c. And MSDN us for the windows API mostly, many of the functions at MSDN are not even standard. – Iharob Al Asimi Dec 01 '15 at 22:46
  • @Afshin In any language you use, typically if you can avoid an explicit cast, avoid it. Imagine if we wrote code like `double d = ...; d = (double)fabs(d);` or even `double x = (double)some_double * (double)2.0;` It can be a source of error and just unnecessary -- harmless at the very best, a source of error and confusion at worst. C++ is a different case since it requires a static cast to cast from void* to some other pointer type. There it's necessary. But if unnecessary, err on the side of leaving the cast out. –  Dec 01 '15 at 23:35
  • So i should not use afshin solution its dangerous? – rev2k9 Dec 02 '15 at 13:14