-1

I tried to create a tree.The structure definition is given.But when assigning values to id, gateType an error is coming-"Request for gateType not in structure or union".I have dynamically allocated memory for each node.A text file is being read which is not of much concern with this question.Please help.

void tree(FILE *file);
struct node
{
    char gateType[5];
    int id;
    struct node *input[20];
    struct node *output[10];
};

void main()
{
    FILE *f,*temp;

    f=fopen("c17.bench","r");


    tree(f);
    fclose ( f);

 }
void tree(FILE *file)
{
    while(fgets(line,sizeof(line),file) != NULL) 
   {
       if(line[0] != '#' || line[0] != ' ')
      {
        node *temp = (struct node *)malloc(sizeof(struct node));
        //temp->gateType="and";
        temp -> id = 5;
      }

   }

}
Akhil V
  • 53
  • 10
  • Mostly unrelated, but [you shouldn't cast the result of `malloc()`](https://stackoverflow.com/questions/605845/do-i-cast-the-result-of-malloc/605858#605858). – Tim Čas Oct 03 '17 at 14:23
  • `void main` is wrong. `main` should return `int`. – melpomene Oct 03 '17 at 14:24
  • 1
    Where's the definition of `node`? – melpomene Oct 03 '17 at 14:24
  • Anyhow, your problem is that you cannot assign a string to an arrays (you can *initialize* an array with a string, but you cannot assign it later). Try [`strncpy()`](https://www.freebsd.org/cgi/man.cgi?strncpy(3)), instead. – Tim Čas Oct 03 '17 at 14:24
  • `FILE` is undeclared. – melpomene Oct 03 '17 at 14:24
  • @TimČas Never use `strncpy`. – melpomene Oct 03 '17 at 14:24
  • @melpomene: That's not very constructive now. Why not? It's the appropriate tool for this task. – Tim Čas Oct 03 '17 at 14:25
  • @TimČas `strncpy` is never appropriate. Please don't recommend it. It does very crazy things. – melpomene Oct 03 '17 at 14:26
  • @melpomene: You've just repeated yourself. Good job. – Tim Čas Oct 03 '17 at 14:27
  • The array assigning is not of much concern......its the integer id that i am not able to asign. – Akhil V Oct 03 '17 at 14:27
  • 3
    In the code you posted, there is no type `node`, only `struct node`. So `node *temp = ...` should generate a compiler error. – Paul Ogilvie Oct 03 '17 at 14:29
  • @melpomene: I did. Array assignments don't work. That's an alternative that actually *does* work. But suit yourself. – Tim Čas Oct 03 '17 at 14:30
  • 1
    In the code you posted, though commented out, `temp->gateType="and";` is wrong. You must use `strcpy(temp->gateType, "and");`. – Paul Ogilvie Oct 03 '17 at 14:31
  • @TimČas But given that `"and"` is a string, it's reasonable to assume OP wants to use strings here. `strncpy` is not a string function. – melpomene Oct 03 '17 at 14:31
  • @TimČas: The trouble with `strncpy()` is that it doesn't guarantee that the result is a null-terminated string. In this context, it's other peculiar behaviour — null padding the target string to full length when the source is shorter than the size specified for the destination — is not much of a problem, but not null terminating the string could be. A decision has to be made whether truncating the input is appropriate, or whether an overlong string should be reported as erroneous. By the time you know enough to decide whether the string will be truncated, you don't need to use `strncpy()`. – Jonathan Leffler Oct 03 '17 at 14:35
  • `temp` is unused in `main`. – melpomene Oct 03 '17 at 14:38
  • `line` is undeclared. – melpomene Oct 03 '17 at 14:38
  • `line[0] != '#' || line[0] != ' '` is always true. – melpomene Oct 03 '17 at 14:38
  • How do I allocate memory for that structure? – Akhil V Oct 03 '17 at 14:39
  • @JonathanLeffler: Oh, *I* know that. It's just that his responses weren't exactly constructive, and he refused to articulate why. I just wanted to see if it was a case of "$X is evil!!!11one", or whether he had any legitimate complaints. – Tim Čas Oct 03 '17 at 14:50
  • sorry for all the silly mistakes from my side – Akhil V Oct 03 '17 at 14:55
  • 1
    @TimČas: It's interesting — since [Melpomene](https://en.wikipedia.org/wiki/Melpomene) was one of the nine (ancient) Greek muses, I associate the name with a female rather than male. However, this being on the Internet, there's no telling whether the name is an accurate reflection of gender. In context, I would not have recommended `strncpy()`. The target string is too short and if it's safe to assume the string won't overflow, then it's safe to use `strcpy()` and if it's not safe, `strncpy()` still isn't really appropriate. – Jonathan Leffler Oct 03 '17 at 14:59

1 Answers1

0

As Paul Ogilvie pointed out in the comments you are missing two things:

void tree(FILE *file);
struct node
{
    char gateType[5];
    int id;
    struct node *input[20];
    struct node *output[10];
};

void main()
{
    FILE *f,*temp;

    f=fopen("c17.bench","r");


    tree(f);
    fclose ( f);

 }
void tree(FILE *file)
{
    while(fgets(line,sizeof(line),file) != NULL) 
   {
       if(line[0] != '#' || line[0] != ' ')
      {

Problem is here:

        node *temp = (struct node *)malloc(sizeof(struct node));

Should be:

        struct node *temp = malloc(sizeof(*temp));

        //temp->gateType="and";

And now you can:

        snprintf(temp->gateType, sizeof(temp->gateType), "%s\n", "and");

etc...

        temp -> id = 5;
      }

   }

}
Ahmed Masud
  • 21,655
  • 3
  • 33
  • 58