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

struct student
{
    char name[25];
    int age;
};

int main()
{
    struct student *c;

    *c =(struct student)malloc(sizeof(struct student));
    return 0;
}

What is the wrong with this code? I tried times by alternating this code to allocate memory to struct pointer. But this error comes when compiling:

testp.c:15:43: error: conversion to non-scalar type requested
  *c =(struct student)malloc(sizeof(struct student));
                                           ^

I'm using mingw32 gcc compiler.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • 1
    Cast the ``malloc`` result to ``(struct student *)``, not ``(struct student)``. – Ami Tavory Jun 09 '15 at 07:12
  • 2
    You don't need `` unless you're using the fancy extra features. The `` header declares the basic memory allocation functions: `malloc()`, `realloc()`, `calloc()` and `free()`. – Jonathan Leffler Jun 09 '15 at 07:14
  • 3
    Tag your question properly. Is it C or C++? In C don't cast `malloc`. In C++ don't use it at all, use `new` instead. – CiaPan Jun 09 '15 at 07:15
  • 1
    Don't forget to add `free(c)` once you've fixed the other problems. Otherwise, you leak memory. It is as well to get into the habit of tracking memory reliably from the start of your career using `malloc()` et al. And be aware that [`valgrind`](http://www.valgrind.org/) is your friend if it runs on the system you use (and you can always host a VM to be able to run it in the VM if it won't run in your native system). – Jonathan Leffler Jun 09 '15 at 07:30

2 Answers2

6

What is the wrong with this code?

Ans: First of all you change that "is" to "are", there are two major problems, atleast. Let me elaborate.

  • Point 1. You allocate memory to the pointer, not to the value of pointer. FWIW, using *c (i.e., de-referencing the pointer without memory allocation) is invalid and will result in undefined behaviour.

  • Point 2. Please do not cast the return value of malloc() and family in C. The cast you used is absolutely wrong and proves the authenticity of the first sentence.

To solve the issues, change

*c =(struct student)malloc(sizeof(struct student));

to

c = malloc(sizeof(struct student));

or, for better,

c = malloc(sizeof*c);   //yes, this is not a multiplication
                        //and sizeof is not a function, it's an operator

Also, please note, to make use of malloc() and family , you don't need the malloc.h header file. These functions are prototyped in stdlib.h.


EDIT:

Suggestions:

  1. Check for success of malloc() before using the returned pointer.
  2. Always free() the memory once the usage is over.
  3. The recommended signature of main() is int main(void).
Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
1

This worked (on C and C++).
Since you originally included both tags.

change

*c =(struct student)malloc(sizeof(struct student));

to

c =(struct student*) malloc(sizeof(struct student));
Varun Garg
  • 2,464
  • 23
  • 37
  • so, you're suggesting a cast? – Sourav Ghosh Jun 09 '15 at 07:16
  • That's because @Sourav answered about C, not C++. I've removed the C++ tag as OP's code is compatible in C (once the problem has been fixed) and also has a `.c` extension. – Spikatrix Jun 09 '15 at 07:16
  • @CoolGuy yep, i'm _happy_ now. Hope you did not mind a little fun, right? :-) – Sourav Ghosh Jun 09 '15 at 07:25
  • 1
    @SouravGhosh , Yeah. @ UzumakiIchigo, The cast is wrong in C, however. See [this](http://stackoverflow.com/q/605845/2173917) – Spikatrix Jun 09 '15 at 07:27
  • @CoolGuy The second point in my answer, also. I think I've already linked _that_ particular post more than 1000 times now. Anybody givin' me a badge for this? – Sourav Ghosh Jun 09 '15 at 07:30
  • @CoolGuy, I read your link, it does not say it is wrong it says its unnecessary if you are using C. 2)By putting it it becomes compatible with C++ too. which was suggested by tag C++ where I found this question. Come on, what can be wrong if code is running fine in both? – Varun Garg Jun 09 '15 at 07:36
  • 1
    There is a cadre of people adamantly opposed to the casts. I'm not a member of that cadre. However, the primary objection is that if you don't declare the memory allocation functions, the results can be misinterpreted. Since C99 and later requires declarations of all functions, if you compile using options that force you to declare functions before use and you never write the declarations for the memory allocation functions yourself (you always use a standard or system-supplied header), then the direst predictions of the cadre won't affect you. – Jonathan Leffler Jun 09 '15 at 07:50