Reputation: 223
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:
Errors:
strrev.c:14: warning: useless storage class specifier in empty declaration
strrev.c: In function ‘main’:
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 **’
My main program:
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;
}
Upvotes: 0
Views: 323
Reputation: 3275
If not typedef-ed to anything a structure type name in C has to be preceded by the struct keyword. So instead of
Node *ptr;
use
struct Node *ptr;
Your strcpy function seems undeclared. For that just : #include <string.h
>
Upvotes: 0
Reputation:
The correct way to reference your struct within your struct:
struct Node {
char info[15];
struct Node *ptr;
};
When you make a struct you have to typeout struct Node
in order to use it. If you want to avoid that you can make a typedef
typedef struct Node {
char info[15];
struct Node *ptr;
} Node;
then you can use just Node
when defining variables, like this
Node myNode;
(but it is probably better to avoid using the same name for the struct and the typedef to avoid confusion).
Note however that you still have to write struct Node
within your struct when it references itself, because at that point the typedef hasn't been created yet.
Upvotes: 5