-2

I'm a beginner in C.
I want to assign each person's info to an array of pointer that is *arr[2]
but I get an error message that is

'malloc' is not declared in this scope.

How can I fix it?

#include <stdio.h>

int main()
{
    struct person {
        char *name;
        int number;
        char gender;
    };

    struct person *arr[2];

    arr[0] = (struct person *) malloc(sizeof(struct person));

    arr[0]->name = "john";
    arr[0]->number = 123;
    arr[0]->gender ='m';

    arr[1] = (struct person *) malloc(sizeof(struct person));

    arr[1]->name = "jessica";
    arr[1]->number = 456;
    arr[1]->gender ='w';

    printf("%s", arr[1]->name);

    return 0;
}
Gonen
  • 4,005
  • 1
  • 31
  • 39
choptxen
  • 57
  • 8
  • 6
  • Also related: [Do I cast the result of malloc?](http://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc) – Some programmer dude Mar 05 '18 at 12:02
  • When using a function such as `malloc` the manual page usually will tell you what include files you need to have to support it. So check the manual page always when using such functions. – lurker Mar 05 '18 at 12:04
  • You also have to alloc memory (or use MAX_LEN or something similar) for name. – honzakuzel1989 Mar 05 '18 at 12:05
  • @honzakuzel1989 When i use ... struct person{ char name[30] ..... , i get an error that is "incompatible types in assignment of 'const char [5]' to 'char [30]' " and also i want to reach 'arr[0] -> name ' program doesn't work – choptxen Mar 05 '18 at 12:15
  • @honzakuzel1989: name is a `char` pointer which can point to a string literal. So, `arr[0]->name = "john";` is perfectly fine. – H.S. Mar 05 '18 at 12:18
  • @choptxen: If using `char name[30] ....` in `struct person` than you need to copy the names to `name` variable, like this : `strcpy (arr[0]->name, "john")` instead of assigning string litereal "john" to `arr[0]->name`. – H.S. Mar 05 '18 at 12:20
  • @H.S. thank you so much!! – choptxen Mar 05 '18 at 12:37
  • @H.S. yes, that's right for string literal.. I thought about reading for external input (from file for example) in the future.. Sorry about misunderstanding. – honzakuzel1989 Mar 05 '18 at 19:10

3 Answers3

0

You should include a header which defines malloc(). Usually, this would be stdlib.h.

You can use online help from cppreference.com or similar sites to get this information, and full documentation for the c libraries.

Michaël Roy
  • 6,338
  • 1
  • 15
  • 19
0

A few inputs than just one.

Primarily: The man page for malloc says that you need to include the header file: stdlib.h

#include <stdlib.h>

The one habit which would save you a lot of pain going ahead is to check if malloc() was successful or not. You must check the value returned by malloc().

arr[0] = malloc(sizeof(struct person));
if(arr[0] == NULL)
{
    // Since malloc has returned NULL, requested memory is not allocated
    // Accessing it is out of question

    // Some error handling implementation
    return;
}

Additionally, we should always return what we borrow, unless we don't crash all of sudden. Allocated memory needs to be freed. There are several examples on internet on how to de-allocate the dynamically allocated memory. A good start would be reading concepts like Memory Leakage and Dangling pointers.

The other suggestion would be:
If you look at the same (or the other) man page for malloc, you'd find that malloc returns a void pointer. So, you do not have to cast the malloc() result. There is this legendary post and a legendary answer which explains why not to cast.

WedaPashi
  • 3,561
  • 26
  • 42
0

To actually use the function malloc,you should include the #include library which declares among other things the malloc(), calloc(), free() functions.

Robert
  • 5,278
  • 43
  • 65
  • 115