2

I'm learning about dynamic memory allocation in C. I searched here for the same problem and found solutions that say that it's an error for C++ compiler. What I don't understand is why am I getting this error when I'm using .c extention and not .cpp. I'm using Visual Studio Express 2012.

Here is the code I wrote if anyone needs it. It's a simple program for entering a matrix (input) and writing it on screen (output):

#include <stdio.h>
#include <stdlib.h>
void main()
{
    int **a;
    int i,j,n,m;
    printf("Enter matrix dimensions:\n");
    scanf("%d%d", &n,&m);
    printf("Enter matrix:\n");
    a=malloc(n*sizeof(int*));
    for (i=0; i<n; i++)
    {
        a[i]=malloc(m*sizeof(int));
        for (j=0; j<m; j++)
        {
            scanf("%d",&a[i][j]);
        }
    }
    printf("Matrix is:\n");
    for (i=0; i<n; i++)
    {
        for (j=0; j<m; j++)
        {
            printf("%d ",a[i][j]);
        }
        putchar('\n');
    }
}

Note: I did other programs (without dynamic memory allocation) in C with the same compiler and everything was fine.

Edit and update(conclusion):
I combined two answers I've got from Vlad from Moscow and Shivansh Jagga and this solved the problem. Vlads' answer helped with the compiling and Shivanshs' got rid of the error, but I have to say, as suggested in comments, that malloc doesn't need typecast and the program can work with that error, it just skippes it.

I see that answer from Shivansh Jagga was deleted for some reason. Basically his answer said that to get rid of the error you need to go to:
Project > Properties > C/C++ > General > SDL checks > No.

Community
  • 1
  • 1
Plexus
  • 143
  • 2
  • 13
  • Try casting to whatever you want: `a = (int**) malloc(n*sizeof(int))` – tadman Sep 08 '17 at 17:53
  • 1
    What you read is correct... this the error that occurs when you compile C as C++, using a C++ compiler. – Dietrich Epp Sep 08 '17 at 17:53
  • @tadman: Let's not put bandaids on problems when we don't understand them. – Dietrich Epp Sep 08 '17 at 17:53
  • @DietrichEpp You're welcome to add a more complete answer. – tadman Sep 08 '17 at 17:54
  • @tadman: Yes, that's what I did. – Dietrich Epp Sep 08 '17 at 17:54
  • @DietrichEpp Why does it happen when I'm using .c extention? – Plexus Sep 08 '17 at 17:55
  • As I saw, the .c extention solves the problem which is not the case for me. – Plexus Sep 08 '17 at 17:55
  • It's often better to allocate a two-dimensional array in one shot rather than this double-layered mess with so many allocations. For an n x m matrix, allocate `n*m` entries. As a note, since you have access to C++, why not just use that? What's the motivation behind doing this in C? – tadman Sep 08 '17 at 17:55
  • @tadman It's not motivation, I'm learning C, not C++. I have to do it :) – Plexus Sep 08 '17 at 17:58
  • @tadman: C++ doesn't make 2D array allocation any easier. – Dietrich Epp Sep 08 '17 at 17:58
  • 1
    This is not relevant to your problem, but `void main()` should be `int main(void)`. (Some compilers let you get away with `void main()`; that doesn't make it correct.) – Keith Thompson Sep 08 '17 at 18:00
  • You tagged your question [C], but in reality you are trying to compile your code as C++. C and C++ are two different languages. Make up your mind about what language you want to use. – AnT stands with Russia Sep 08 '17 at 18:00
  • 2
    @AnT: In reality, OP is *trying* to compile as C but the code is actually being compiled as C++, that is the problem. The [c] tag is correct. – Dietrich Epp Sep 08 '17 at 18:01
  • 2
    @AnT: The OP has already made it clear, in previous comments, that he wants to compile the code as C. It seems there's some problem with his toolchain that's causing his code to be compiled as C++ instead. – Keith Thompson Sep 08 '17 at 18:02

2 Answers2

2

It is evident that you are compiling your program as a C++ program.

In C a pointer of the type void * may be assigned to an object pointer of any other type without explicit casting.

To compile it as a C program you should do the following.

As far as I remember you should select the item Project from the menu then select something like Project properties (J have no the English edition of the IDE) and then in the panel select C/C++ -> Additional and in the right side of the panel set the item Compile as to C (/TC)

Take into account that according to the C Standard the function main without parameters shall be declared like

int main( void )
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

Unfortunately, what you read is correct (this is caused by compiling as C++) but we cannot diagnose your problems here because they appear to be in the toolchain. To really make sure this is the case, temporarily add the following code to the top of your file:

#ifdef __cplusplus
#error "Can't compile as C++"
#endif

This will obliterate any doubt, and help you diagnose the incorrect settings in your Visual Studio project that are causing this to be compiled as C++.

Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • Adding a declaration `int class;` is another way to do this, though using **`__cplusplus`** is more direct. – Keith Thompson Sep 08 '17 at 18:01
  • I added this at top (before includes) and now there is a new error: #error directive: "Can't compile as C++". Note: The old one is still there as well. – Plexus Sep 08 '17 at 18:05
  • @Plexus: Then there is no doubt at all... the file is *definitely* being compiled as C++. If you compile as C, that error will go away. Try removing and adding the source file from your project, or double-checking the compiler settings. – Dietrich Epp Sep 08 '17 at 18:07