I have the following code:
struct TreeNode {
int val;
struct TreeNode *left;
struct TreeNode *right;
};
#define MAXSIZE 1024
typedef struct stack{
struct TreeNode volum[MAXSIZE];
int top;
}STACK;
STACK st;
st.top = -1;
/* other function definitions such as pop() and push() */
But when I compile it, it gives me error Line 18: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘.’ token. Where Line 18 is st.top=-1;
So here I need to initialize the stack, i.e setting the top to -1. I also tried to do it inside the struct: int top=-1; But got the same error. I wonder what the correct way of doing it is. Thanks in advance.
You could try
typedef struct stack {
int top;
struct TreeNode volum[MAXSIZE];
} STACK;
STACK st = { -1 }; // top has to be the first member of the struct
Put st.top = -1; in some function (e.g. main), because you can't do assigning globally.
Related
This question already has answers here:
C : typedef struct name {...}; VS typedef struct{...} name;
(6 answers)
Closed 5 years ago.
I am creating a program that uses a basic stack in C. In this I have two structures defined in the heading:
A structure named Node with a string and a pointer to a previous Node as members.
A structure named Stack with a pointer to the last Node as member.
Here are the definitions of these structures in my header file:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
const char* string;
struct Node *prev;
};
typedef struct Stack {
size_t sizeOfStack;
size_t sizeOfElem;
struct Node *last;
};
One method giving me errors is CreateStack():
CreateStack: This function creates a stack (equivalent to a constructor).
(a) Name: CreateStack
(b) Return Type: A pointer to a stack allocated in the heap.
Here is my implementation
Stack* CreateStack() {
Stack* stack = malloc(sizeof(*stack));
if (stack == NULL) {
return NULL;
}//end of if
stack->sizeOfElem = 0;
stack->sizeOfStack = 0;
stack->last = NULL;
return stack;
}//end of CreateStack
But the compiler is spitting this out:
error: 'Stack {aka struct Stack}' has no member named 'last'
stack->last = node;
error: 'Stack {aka struct Stack}' has no member named 'last'
node->prev = stack->last;
error: 'Stack {aka struct Stack}' has no member named 'last'
Node *node = stack->last;
If someone could point out the issue here I would greatly appreciate it. I am confused as to why it is saying last is not a thing, yet prev defined in the same way in the other structure does not raise a flag. Thanks.
Fix the typedefs and it'll compile:
#include <stdio.h>
#include <stdlib.h>
typedef struct Node {
const char* string;
struct Node *prev;
} Node ;
typedef struct Stack {
size_t sizeOfStack;
size_t sizeOfElem;
struct Node *last;
} Stack;
Stack* CreateStack() {
Stack* stack = malloc(sizeof(*stack));
if (stack == NULL) {
return NULL;
}//end of if
stack->sizeOfElem = 0;
stack->sizeOfStack = 0;
stack->last = NULL;
return stack;
}//end of CreateStack
Your typedef statement is incomplete, as you do not define a name for the type. Write the following.
typedef struct Stack {
size_t sizeOfStack;
size_t sizeOfElem;
struct Node *last;
}Stack;
Note the Stack at the end, which defines now type Stack being equivalent to struct Stack.
I am trying to write code to build a stack, but I'm getting compilation errors which don't make sense to me. Here is my stack.h:
struct StackNode {
void* previous;
int value;
};
struct Stack {
StackNode* top;
};
Stack* new_stack () {
StackNode stn = { NULL, 0 };
Stack* st = (Stack*) malloc(sizeof(Stack));
st->top = NULL;
return st;
}
and my main.c:
#include <stdio.h>
#include <stdlib.h>
#include "stack.h"
int main () {
struct Stack* st = new_stack();
return 0;
}
gcc throws these errors:
make (in directory: /home/diego/temp/stack) gcc -g -O2 -std=c99 -c
main.c In file included from main.c:4: Compilation failed. stack.h:10:
error: expected specifier-qualifier-list before ‘StackNode’
stack.h:14: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘attribute’
before ‘*’ token main.c: In function ‘main’: main.c:8: warning:
implicit declaration of function ‘new_stack’ main.c:8: warning:
initialization makes pointer from integer without a cast make: *
[main.o] Error 1
EDIT: I found the error. I forgot to put struct before Stack and StackNode in some lines. Always having struct on those lines solves the issue.
Change:
struct Stack {
StackNode* top;
};
to:
struct Stack {
struct StackNode* top;
};
and anywhere else StackNode or Stack is used and not preceded by struct. If you wish to not specify struct you could use a typedef.
Here is a small program I wrote, (I am still writing it), however till this point the program on compilation should not be giving any error as per my understanding.
#include <stdio.h>
#include <stdlib.h>
struct node t1 {
int data;
struct node *next, *prev;
};
struct node *root;
root = NULL;
int main()
{
int i, j, choice, count;
printf("enter choice\n");
scanf("%d", &choice);
count = 0;
while (choice == 1) {
printf("enter a data element");
scanf("%d", &j);
count++;
}
}
void push()
{
}
void pop()
{
}
The error I get is
cc linklist.c
linklist.c:3:16: error: expected ‘=’, ‘,’, ‘;’, ‘asm’ or ‘__attribute__’ before ‘{’ token
linklist.c:8:1: warning: data definition has no type or storage class [enabled by default]
linklist.c:8:1: error: conflicting types for ‘root’
linklist.c:7:14: note: previous declaration of ‘root’ was here
linklist.c:8:8: warning: initialization makes integer from pointer without a cast [enabled by default]
I use gcc and Ubuntu 11.04.
What is the reason that upon compiling the code I get above warning.
struct node *root;
root = NULL;
You can't assign like that outside a function. Drop the root = NULL since it's implicit for objects with static storage (such as global variables).
EDIT
As spotted by Tom Dignan the struct declaration is also wrong:
struct node t1 { ... };
^^
You can't put a statement like root = NULL; at top-level (outside of any function). Do
struct node *root = NULL;
(The = NULL part is actually optional; a global or static pointer is automatically null.)
For one, you have an assignment statement outside of main or a function.
root = NULL;
I have not tried anything else.
struct node t1 {
int data;
struct node *next, *prev;
};
You want to create alias for struct node. It shoud be:
typedef struct node { /* typedef! */
int data;
struct node *next, *prev;
}t1; /* alternative name go here */
This question already has answers here:
Why can't we initialize members inside a structure?
(6 answers)
Closed 7 years ago.
Can you have a structure in C that has elements of that same structure? My first attempt at implementing a binary search tree in C is the following:
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left = null;
struct binary_tree_node *right = null;
};
main() {
struct binary_tree_node t;
t.value = 12;
struct binary_tree_node y;
y.value = 44;
t.left = &y;
}
I can't figure out what's wrong with this code, any help would be appreciated. I realize there are other questions on binary search implementations in C, but I'm trying to figure this out from scratch with my own code (and some guidance of course). Thanks!
Remove the = null in your struct declaration. You can declare the self-reference, but you cannot set it.
This is the error message on gcc 4:
test.c:6: error: expected ‘:’, ‘,’, ‘;’, ‘}’ or ‘__attribute__’ before ‘=’ token
test.c: In function ‘main’:
test.c:18: error: ‘struct binary_tree_node’ has no member named ‘left’
Firstly, you null is NULL in C.
Secondly, you cannot set a value to an element in a struct inside the struct definition.
So, it would look something like this:
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left;
struct binary_tree_node *right;
};
main() {
struct binary_tree_node t;
t.left = NULL;
t.right = NULL;
t.value = 12;
struct binary_tree_node y;
y.left = NULL;
t.right = NULL;
y.value = 44;
t.left = &y;
}
Or, you can create a function to make left and right NULL,
#include <stdio.h>
struct binary_tree_node {
int value;
struct binary_tree_node *left;
struct binary_tree_node *right;
};
void make_null(struct binary_tree_node *x) {
x->left = NULL;
x->right = NULL;
}
main() {
struct binary_tree_node t;
make_null(&t)
t.value = 12;
struct binary_tree_node y;
make_null(&y);
y.value = 44;
t.left = &y;
}
You cannot define the values inside the struct when defining the struct. This code snippet may benefit your project:
typedef struct binary_tree_node
{
int value;
binary_tree left;
binary_tree right;
} binary_tree_node, *binary_tree;
#define DATA(T) ((T)->value)
#define LEFT(T) ((T)->left)
#define RIGHT(T) ((T)->right)
this is probably a simple solution I am not that familiar with C just trying to port my java data structure assignments to C.
this is the error i am getting:
test.c:4: error: expected ‘)’ before ‘*’ token
test.c:11: error: expected ‘)’ before ‘*’ token
#include <stdio.h>
#include <stdlib.h>
void to_screen(NODE *cur){
while(cur->next != NULL){
printf("%d\n", cur->data);
cur = cur->next;
}
}
void add_first(NODE *head, int data){
NODE *cur;
int i;
for(i=0; i<10; i++){
cur = malloc(sizeof(NODE));
cur->data = data;
cur->next = (*head).next;
head->next = cur;
}
}
typedef struct node{
int data;
struct element *next;
}NODE;
int main(){
int i;
NODE *head;
for(i=0; i<10; i++){
add_first(head, i);
}
to_screen(head);
}
You need to move the definition of your struct above the to_screen function. The compiler is saying that it doesn't know what NODE is.
You need to define NODE before it is used. Move the definition to the top.
You need to move this block to the top as 2 other answers recommend.
typedef struct node{
int data;
struct element *next;
}NODE;
You may ask for the reason. The reason is that C language specification is not like Java. So it kinds of compiling from the top to the bottom. So, if it see something undefined, it will look for definition above that point, and if it sees the definition it gets it. It doesn't look below the line of code