1

I am getting compilation error for the following code. I have tried my best, but cannot figure it out. Any help will be appreciated.

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

#define N 100

int counter=0;
struct node {
    int value; };
struct node *p = (struct node  *) malloc (N*sizeof (node));

void main()
{

    int a = 5, b=6;
        struct node * c = 0;
    c = add(a,b); 
}

void add(int m, int n)
{
    struct node * pin_1;
        struct node * pin_2;
        struct node * pin_0;
    pin_0->value = m;
    pin_1->value = n;
    pin_2->value = m + n;
    counter++;
    printf("value of out is %d /n", pin_2->value);       
}

I am getting the error in GCC:

struct_check.c:9: error: ‘node’ undeclared here (not in a function)

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261

1 Answers1

4

First of all, syntactically, you need to change

  struct node *p = (struct node  *) malloc (N*sizeof (node));

to

  struct node *p = malloc (N*sizeof ( struct node));

because, node itself is not a type, unless you use a typedef to create one.

That said,

  • You cannot have a statement to be executed in global scope, move that inside some function.
  • You never seem to make use of p, anywhere.
  • You're using pin_2 and pin_0 uninitialized, that invokes undefined behavior. You need to make those pointers point to some valid memory before you can dereference them.
  • void main() is obsolete as per the latest standard, and for a hosted environment, the conforming signature will be int main(void), at aleast.
  • You can write a more robust statement by using the style struct node *p = malloc ( N *sizeof(*p));
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
  • but then how do I typecast it to type node, if I dont use (struct node *) before malloc, without using typedef – Shankhadeep Mukerji Oct 03 '16 at 10:06
  • 4
    @ShankhadeepMukerji You don;t, it's not needed, atleast in C. – Sourav Ghosh Oct 03 '16 at 10:07
  • 1
    Yes, the casting is mandatory in C++, but in C `void *` is implicitly casted to any `sometypename *`. – u354356007 Oct 03 '16 at 10:10
  • 1
    In C++, use of `malloc()` is discouraged. By responding to such discouragement, i.e. not using `malloc()` in cases like this, the need for casting is avoided anyway. – Peter Oct 03 '16 at 10:13
  • My intention is to add two new array elements of type struct node(pin_0 and pin_1) and initialize them inside the add function. I am not able to figure out how to do that i.e my first element of array p should be pin_0 and should have a value of a and so on. I dont want to keep track of indices. – Shankhadeep Mukerji Oct 03 '16 at 10:20
  • Thanks everyone! I got my expected results. – Shankhadeep Mukerji Oct 03 '16 at 10:29
  • @Sourav Ghosh My only question is that I am using struct node *p = malloc ( N *sizeof(*p)); which has restricted my use of node *p values inside the functions and only. I cannot get the values in my main function. Also, when I use another function say multiply, do I again write struct node *p = malloc ( N *sizeof(*p)); sinde that function and assign struct node * pin_0 = &p[index++] ?? I want my memory allocation to be accessed by main and all other functions, maybe keep it global and yet use it to allocate memory inside add, multiply functions. – Shankhadeep Mukerji Oct 03 '16 at 10:40
  • 1
    @ShankhadeepMukerji allocate `p` in `main()` and pass it to function as argumnets. – Sourav Ghosh Oct 03 '16 at 10:53
  • @SouravGhosh I hate to ask this, but can you please provide a code script of the function , how to pass the p in function argument. I am not getting. – Shankhadeep Mukerji Oct 03 '16 at 11:11
  • @ShankhadeepMukerji No offense, if that is the case, you need to read a C book. no SO question (or answer, for that matter) will be able to help you, that's simply too broad. – Sourav Ghosh Oct 03 '16 at 11:23
  • I got the answer. I was missing the &p[0] and instead giving p[0] as function arg – Shankhadeep Mukerji Oct 03 '16 at 11:34