0

I wrote a code and it compiles perfectly in CodeBlocks 13.12 with no errors ! i copied the same code to VS2010 it shows 1 error:

IntelliSense: identifier "malloc" is undefined  

CodeBlocks Code:

#include <stdio.h>
#define maxLength 4
typedef short int *set;

void func(set *a)
{
    *a=malloc(maxLength*sizeof(set));
    (*a)[0]=10;
    (*a)[1]=13;
    (*a)[2]=15;
}

void main()
{
    set a;
    func(&a);
    printf("%d %d %d",a[0],a[1],a[2]);

}

VS2010 Code:

#include "stdafx.h"
#include <stdio.h>

#define maxLength 4
typedef short int *set;

void func(set *a){
         *a=malloc(maxLength*sizeof(set));
               (*a)[0]=10;
               (*a)[1]=13;
               (*a)[2]=15;
}

int _tmain(int argc, _TCHAR* argv[])
{
    set a;
    func(&a);
    printf("%d %d %d",a[0],a[1],a[2]);
    return 0;
}

I don't know what is the problem .. and if i add in the pre-compile code : #include <iostream> the error goes , but another error appears:

IntelliSense: a value of type "void *" cannot be assigned to an entity of type "set"    

NEW CODE

void func(set *a){
    func(a);
}

void func1(set *a){
        *a=reinterpret_cast<set>(malloc(maxLength*sizeof(set)));
               (*a)[0]=10;
               (*a)[1]=13;
               (*a)[2]=15;
}

How do i create an array in a function that called by a function ?

2 Answers2

1

There are two problems you seem to have. The first and most serious is that you're missing a header file. See e.g. this malloc reference. Not including this header file will cause your memory allocations to now work as you expect.

The other error is that you actually seem to be using C++ and not C. In C you should not cast the return of malloc, but in C++ you must do it.

Community
  • 1
  • 1
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • Still has errors , actually 2 errors: `IntelliSense: a value of type "void *" cannot be assigned to an entity of type "set"` And second error: `error C2440: '=' : cannot convert from 'void *' to 'set'` – user3225284 Feb 02 '14 at 10:31
  • @user3225284 You heard of type-casting? In C a `void *` can be implicitly casted to any other pointer type, and the opposite. However C++ is a much stringer typed language, so you must cast incompatible types. Try e.g. `*a = reinterpret_cast(malloc(...));` – Some programmer dude Feb 02 '14 at 10:37
  • @user3225284 In C++ you need to cast the returned value of `malloc` – rullof Feb 02 '14 at 10:39
  • what is **reinterpret_cast** ? – user3225284 Feb 02 '14 at 10:41
  • @user3225284 It's exactly what it sounds like, it *reinterprets* one type as another. Read e.g. [this reference](http://en.cppreference.com/w/cpp/language/reinterpret_cast). You might also want to check out [The Definitive C++ Book Guide and List](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) – Some programmer dude Feb 02 '14 at 10:55
  • @JoachimPileborg It worked , but only one thing left , I updated the code .. look at it – user3225284 Feb 02 '14 at 10:59
0

You should add a header file defining the malloc function: stdlib.h (C) or cstdlib (C++).

Peter Petrik
  • 9,701
  • 5
  • 41
  • 65