2

This program simply takes a file with ASCII lines, puts it into a linked-list stack, and then prints the reversed list to a new file in the same ASCII format.

My struct Code:

typedef struct Node{
    char info[15];
    struct Node *ptr;
};

I'm getting the following errors on Main. Most have to do where I declare the new Node Head... what's wrong with that syntax?:

Errors
    strrev.c:28: error: ‘Node’ undeclared (first use in this function)
    strrev.c:28: error: (Each undeclared identifier is reported only once
    strrev.c:28: error: for each function it appears in.)
    strrev.c:28: error: ‘head’ undeclared (first use in this function)
    strrev.c:34: warning: passing argument 1 of ‘strcpy’ from incompatible pointer type
   /usr/include/string.h:128: note: expected ‘char * __restrict__’ but argument is of         type ‘char **’

Main Code:

int main(int argc, char *argv[])
{
    if (argc != 3) {
        fprintf(stderr, "usage: intrev <input file> <output file>\n");
        exit(1);
    }

    FILE *fp = fopen(argv[1], "r");
    assert(fp != NULL);


    Node *head = malloc(sizeof(Node));
    head->ptr=NULL;

    char str[15];
    while (fgets(str, 15, fp) != NULL){
        struct Node *currNode = malloc(sizeof(Node));
        strcpy(currNode->info, str);
        currNode->ptr = head;
        head=currNode;
    }

    char *outfile = argv[2];
    FILE *outfilestr = fopen(outfile, "w");
    assert(fp != NULL);

    while (head->ptr != NULL){
        fprintf(outfilestr, "%s\n", head->info);
        head = head->ptr;
    }

    fclose(fp);
    fclose(outfilestr);
    return 0;
}
7
  • 1
    What is char *info[15] supposed to do in your mind? Can you please try and verify your assumptions of how C works on a small program first and move to a more complex one only after you're confident how it works? Commented Oct 2, 2013 at 7:48
  • It's a character array holding a line from a file. Commented Oct 2, 2013 at 7:49
  • 1
    But it isn't. It's an array of pointers. In fact, your compiler should have given you a warning about this. Are you sure you've enabled all warnings? Commented Oct 2, 2013 at 7:49
  • Or, it will when I add it. Once again, the problem seems to occur when I declare a new head node. Coming from Java, I guess I don't see where the problem is coming from. Commented Oct 2, 2013 at 7:51
  • gcc -o strrev strrev.c -Wall -m32 -O --> so yes Commented Oct 2, 2013 at 7:51

2 Answers 2

5

You have the wrong syntax for typedef of a structure. You need to place the typedef name after the structure definition:

typedef struct Node  /* <- structure name */
{
    /* ... */
} Node;  /* <- typedef name */

And it's okay to use the same name for both the structure and the type, as both live in different namespaces.

Sign up to request clarification or add additional context in comments.

8 Comments

wouldn't it also legal to omit the structure name?
Or use struct Node as in the while loop. But in that case there's no need for having the typedef at all.
Holy cow. I.. I'll be honest, maybe because I'm new, but I'm not entirely sure why that worked... Do you know where in K&R I can read up more on this?
@xmoex While it's legal, the OP uses a pointer to the structure inside the structure itself, so then need to give the structure a name.
@katiea It's how fgets works. It read and leaves the newline in the string, and you need to manually get rid of it (typically str[strlen(str) - 1] = '\0'; will work, be sure that the fgets call didn't fail first though).
|
2

You need to have a Node typedef first

typedef struct Node Node;
typedef struct Node{
char *info[15];
Node *ptr;
};

Or do it in one

typedef struct Node{
    char *info[15];
    struct Node *ptr;
} Node;

1 Comment

The declaration of the struct does not need typedef. It is not a type definition (the second code line should be only struct Node{).

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.