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.

4
  • 1
    Cast the malloc result to (struct student *), not (struct student). Commented Jun 9, 2015 at 7:12
  • 2
    You don't need <malloc.h> unless you're using the fancy extra features. The <stdlib.h> header declares the basic memory allocation functions: malloc(), realloc(), calloc() and free(). Commented Jun 9, 2015 at 7: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. Commented Jun 9, 2015 at 7: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 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). Commented Jun 9, 2015 at 7:30

2 Answers 2

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).
Sign up to request clarification or add additional context in comments.

2 Comments

Or even the two lines to: struct student *c = malloc(sizeof(*c));.
@JonathanLeffler Absolutely right sir, but I thought maybe someone may then try doing the same in global scope and we have to handle another related question. :-)
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));

7 Comments

so, you're suggesting a cast?
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.
@CoolGuy yep, i'm happy now. Hope you did not mind a little fun, right? :-)
@SouravGhosh , Yeah. @ UzumakiIchigo, The cast is wrong in C, however. See this
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.