points as function parameters C - c

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

Related

Error, linked lists using C programming

I'm trying to code simple push function and everything is fine until run time, executed code crashes. Anyone can clarify the reason, please ?
#include<stdio.h>
#include<stdlib.h>
typedef struct list{
int order;
struct list *next;
}list;
void push(struct list **arg,int i);
int main()
{
struct list **ptr=NULL;
for(int i=0;i<10;++i){
push(ptr,i);
}
return 0;
}
void push(struct list **arg,int i){
struct list *temp;
temp= malloc(sizeof(list));
temp->order=i;
temp->next=*arg;
*arg=temp;
}
Write
list *ptr=NULL;
^^^
for(int i=0;i<10;++i){
push( &ptr,i);
^^^^
}
Otherwise if to declare like this
struct list **ptr=NULL;
then this dereferencing
*ptr
results in undefined behavior.

linked list && structures

I would like to know if design of my program is correct, as well as to understand if my commented area is doing what it supposed to be doing. I get these compile errors that are associated probably with commented segments of my code, and I would lie to receive some help. THANKS!
part1.c:15:6: error: expected '=', ',', ';', 'asm' or '__attribute__' before 'insert'
part1.c: In function 'main':
part1.c:43:14: error: incompatible types when assigning to type 'struct point' from type 'int'
part1.c:49:44: error: invalid type argument of '->' (have 'struct point')
part1.c:49:59: error: invalid type argument of '->' (have 'struct point')
part1.c:55:5: error: incompatible type for argument 1 of 'free'
/usr/include/stdlib.h:488:13: note: expected 'void *' but argument is of type 'struct point'
char *chars[3]= {"a","b","c"};
int nums[3]= {5,8,9};
struct point {char *letter;
int number;
struct point *next;};
struct point* insert(struct point list[],char *rqdLetters, int rqdNums)
{
struct point *new;
new = (struct point*)malloc(sizeof(struct point));
if(new == NULL)
fprintf(stderr,"error!");
new->letter = rqdLetters;
new->number = rqdNums;
new->next = head;
head = new;
//not sure if i'm returning the a pointer to the start of new list
return head;
}
int main(int argc, char **argv)
{
//not sure if i need to declare these here or in the insert
struct point list[3];
struct point *head = NULL;
struct point *next;
struct point *new;
int i;
for (i = 0; i < 3; i++)
{
//return result put back into the pointer to the start of the list
head[i] = insert(list[i], chars[i], nums[i]);
}
int j;
for(j = 0; j < 3; j++)
{
printf("letter %s and number %d\n", list[j]->letter, list[j]->number);
}
int z;
for(z = 0; z < 3; z++)
{
free(list[z]);
}
return 0;
}
At a glance, there are several issues with your code. Firstly, you're not declaring your variables correctly.
new = list;
should be:
struct point* new;
Your function signature also looks a little suspect. If you're returning a pointer to your data structure, it should be something like:
struct point* insert(...) { ... }
At a more general level, I does seem like your idea of a linked list may be a little off. To represent a list, you should only need to hold on to the head and tail of the list, instead of keep an array of your points.
It usually helps if you create a data structure to hold these pointers. You can then pass this structure around to functions that operate on the list e.g. the insert() function.
As a quick example (untested):
struct node {
struct node *next;
char letter;
int number;
}
struct list {
struct node *head;
struct node *tail;
}
/* create a new list */
struct list* list_new(void) {
struct list *L = malloc(sizeof(struct list));
L->head = NULL;
L->tail = NULL;
}
/* add a new node to the list */
void list_insert(struct list *list, char in_letter, int in_number) {
struct node *node = malloc(sizeof(struct node));
node->letter = in_letter;
node->number = in_number;
node->next = NULL;
if (list->head == NULL) { /* empty list */
list->head = node;
list->tail = node;
} else { /* append to list */
list->tail->next = node;
list->tail = node;
}
}
You can then use it as such:
int i;
char chars[3]= {"a","b","c"};
int nums[3]= {5,8,9};
struct list *mylist = list_new();
for (i = 0; i < 3; i++)
{
list_insert(mylist, chars[i], nums[i]);
}
In response to:
... and i am not sure if i am supposed to declare it inside insert or main, i did in the main however
This depends on where you intend to use the variables and the intended lifespan of these variables. As stated in the comments above, you might want to polish up on your understanding of scoping rules.

initializing error in while loop

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 */

'list' undeclared (first use in this function)

I have below code.
I am getting error as 'list' undeclared (first use in this function).
Please help me
#include <stdio.h>
#include <stdlib.h>
struct list{
int data;
struct list *next;
};
typedef struct list *head;
int main()
{
struct list *start;
int i;
start = (list *) malloc(sizeof(struct list));
printf("\nEnter the data : \n");
scanf("%d", &i);
start->data = i;
start->next = NULL;
while(list->next != NULL)
{
printf("%d ", list->data);
list = list->next;
}
return 0;
}
You're using the type list instead of variable name start. Proper code:
while (start->next != NULL)
{
start = start->next;
// etc.
}
Don't cast the return type of malloc - there's no benefit of it and in this case you did it wrong!
start = (list *) malloc(sizeof(struct list));
should be
start = malloc(sizeof(struct list));
the type list * doesn't exist; you meant struct list *.
You can make it even more safe by writing
start = malloc(sizeof(*start));
this way you automatically malloc enough bytes for the (pointer) type of start, which is useful when you later change the type of start - the malloc call doesn't change a bit.
The
start = (list *) malloc(sizeof(struct list));
includes an unnecessary typecast. Just do
start = malloc(sizeof(struct list));
However, your code has more trouble than this. I can answer your question best by asking a question of my own: in your mind, is list a type or an object?
If you answer this question, one suspects that you can fix your code. Good luck.
#include <stdio.h>
#include <stdlib.h>
typedef struct list{
int data;
struct list *next;
} list;
typedef struct list *head;
int main()
{
struct list *start;
int i;
start = (list *) malloc(sizeof(struct list));
printf("\nEnter the data : \n");
scanf("%d", &i);
start->data = i;
start->next = NULL;
while(start->next != NULL)
{
start = start->next;
}
return 0;
}
you can define the type (list *)
Your variable is named « start » and you called it « list ».
start = (struct list *) malloc ...
You've missed struct in casting.
Which is not neccessary at all as Anthales pointed out.
start = malloc ...
One problem you had is you were re-using struct in declaring your struct pointer after you had created a typedef, struct list *start;. Also the struct and typedef cannot have the same name. You get this:
cc -Wall test.c -o test
test.c: In function ‘main’:
test.c:13: error: ‘list_t’ undeclared (first use in this function)
test.c:13: error: (Each undeclared identifier is reported only once
test.c:13: error: for each function it appears in.)
test.c:13: error: ‘start’ undeclared (first use in this function)
test.c:13: error: ‘cur’ undeclared (first use in this function)
test.c:13: warning: left-hand operand of comma expression has no effect
test.c:16: error: expected expression before ‘)’ token
You can choose to use struct list everywhere and skip making using typedef. Use of typedef simplifies how your code reads as noted here: http://en.wikipedia.org/wiki/Struct_%28C_programming_language%29#typedef
I've rewritten what you have just so I could compile it and understand it a little better, and so I could put some data into one node. I remember the whole struct typedef concept taking a little time to sink in, when I was learning C. So, don't give up.
#include <stdio.h>
#include <stdlib.h>
struct list {
int data;
struct list *next;
};
typedef struct list list_t;
int main()
{
list_t *start, *cur;
int i;
start = (list_t *) malloc(sizeof(list_t));
if (NULL != start)
{
cur = start; /* Preserve list head, and assign to cur for list trarversal. */
printf("\nEnter the data : ");
scanf("%d", &i);
cur->data = i;
cur->next = NULL;
cur = start;
while(cur != NULL)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
else
{
printf("Malloc failed. Program ending.");
}
return 0;
}

C struct and malloc problem (C)

It's amazing how even the littlest program can cause so much trouble in C.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
tree* tree = (tree*) malloc(sizeof(tree));
node *node = (node*) malloc(sizeof(node));
tree->nodes[0] = node;
return tree;
}
int main() {
return 0;
}
The compiler says:
main.c: In function 'initTree':
main.c:17: error: expected expression before ')' token
main.c:18: error: expected expression before ')' token
Can you please help?
You're using two variables named tree and node, but you also have structs typedefed as tree and node.
Change your variable names:
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
/* in C code (not C++), don't have to cast malloc's return pointer, it's implicitly converted from void* */
tree* atree = malloc(sizeof(tree)); /* different names for variables */
node* anode = malloc(sizeof(node));
atree->nodes[0] = anode;
return atree;
}
int main() {
return 0;
}
tree and node is your case are type names and should not be used as variable names later on.
tree *initTree() {
tree *myTree = (tree*) malloc(sizeof(tree));
node *myNode = (node*) malloc(sizeof(node));
myTree->nodes[0] = myNode;
return myTree;
}
Change (tree*) and (node*) to (struct tree*) and (struct node*). You can't just say tree because that's also a variable.
Change the body of initTree as follows:
tree* myTree = (tree *)malloc(sizeof(tree));
node *myNode = (node *)malloc(sizeof(node));
myTree->nodes[0] = myNode;
return myTree;
Don't use typedef'ed names as variable names, and there is not need to cast malloc(); in C.
#include <stdio.h>
#include <stdlib.h>
typedef struct node {
int value;
struct node *leftChild;
struct node *rightChild;
} node;
typedef struct tree {
int numNodes;
struct node** nodes;
} tree;
tree *initTree() {
tree->nodes[0] = malloc(sizeof(node));
return malloc(sizeof(tree));
}
int main() {
return 0;
}
I second that Mehrdad's explanation is to the point.
It's not uncommon that in C code you define a variable with the same name as the struct name for instance "node node;". Maybe it is not a good style; it is common in, e.g. linux kernel, code.
The real problem in the original code is that the compiler doesn't know how to interpret "tree" in "(tree*) malloc". According to the compiling error, it is obviously interpreted as a variable.
Apart from the original question, this code, even in it's correct forms will not work, simply due to the fact that tree::nodes (sorry for the C++ notation) as a pointer to a pointer will not point to anything usefull right after a tree as been malloced. So tree->nodes[0] which in the case of ordinary pointers is essentially the same like *(tree->nodes), can't be dereferenced. This is a very strange head for a tree anyway, but you should at least allocate a single node* to initialize that pointer to pointer:
tree *initTree() {
/* in C code (not C++), don't have to cast malloc's return pointer, it's implicitly converted from void* */
tree* atree = malloc(sizeof(struct tree)); /* different names for variables */
/* ... */
/* allocate space for numNodes node*, yet numNodes needs to be set to something beforehand */
atree->nodes = malloc(sizeof(struct node*) * atree->numNodes);
node* anode = malloc(sizeof(struct node));
atree->nodes[0] = anode;
return atree;
}
Interestingly, it does compile cleanly if you simply write the allocations as:
tree *tree = malloc( sizeof *tree );
It is often considered better style to use "sizeof variable"
rather than "sizeof( type )", and in this case the stylistic
convention resolves the syntax error. Personally, I think
this example is a good case demonstrating why typecasts are
generally a bad idea, as the code is much less obfuscated if
written:
struct tree *tree = malloc( sizeof *tree );

Resources