1

I'm a noob student trying to write a program that uses binary search tree to organize the workers of a company. My teacher told me if I want to be able to create a new instance of the Worker structure, i can use malloc with the structure, which will return pointer to a new struct every time it's used, then i can edit the details of that new struct from another function. But how can i do it? No matter what i do it gets so complicated and i can't do it. Here's the code i've been able to write this part of the code, just to test if i can create and edit a new structure. The main thing i ask is, how can i edit the newly created structure?

#include<stdlib.h>
#include<stdio.h>

struct btnode
{
    int value = 5;
    struct btnode *l;
    struct btnode *r;
};

int test(int *p)
{

    printf("%d", &p->value);
}

int main()
{
    int *asdf = (int *)malloc(sizeof(struct btnode));

    test(asdf);
}
  • Welcome to Stack Overflow! [Please see this discussion on why not to cast the return value of malloc() and family in C..](https://stackoverflow.com/q/605845/2173917) – Sourav Ghosh Jun 04 '17 at 18:39
  • 4
    Apart anything, `printf("%d", &p->value);` --> time to re-read the chapter for pointers. – Sourav Ghosh Jun 04 '17 at 18:40
  • `int *asdf = (int *)malloc(sizeof(struct btnode));` ==> `struct btnode *asdf = malloc(sizeof *asdf);` – Weather Vane Jun 04 '17 at 18:42
  • I was just testing, just trying different things from desperation. Oh and i admit, i'm having a hard time understanding pointers. – Artoghrul Gahramanli Jun 04 '17 at 18:44
  • Your teacher is confused about the difference between C and C++. You should not be using `malloc` in C++. You should be using `new`. – user1118321 Jun 04 '17 at 18:46
  • Note the `5` in the function definition will not find its way to the `printf` function. The memory allocated in my last comment will be *uninitialised*. But +1 for presenting a minimal example, before rushing blindly into a wall of code. Step by step is the right way to develop. – Weather Vane Jun 04 '17 at 18:46
  • `int*` has no structure member. – BLUEPIXY Jun 04 '17 at 18:48
  • `struct btnode { int value = 5;` : Writing like this can not be done in C. – BLUEPIXY Jun 04 '17 at 18:52
  • http://ideone.com/hZ8JZB – BLUEPIXY Jun 04 '17 at 20:30

2 Answers2

3

Here is a mod of your program which allocates memory for one struct, fills in values for its members, and calls test() to print one member.

#include <stdlib.h>
#include <stdio.h>

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
};

void test(struct btnode *p)
{
    printf("%d", p->value);
}

int main(void)
{
    struct btnode *asdf = malloc(sizeof *asdf);
    if(asdf != NULL) {
        asdf->value = 5;
        asdf->l = NULL;
        asdf->r = NULL;
        test(asdf);
        free(asdf);
    }
    return 0;
}

There are a number of small changes to detail too, I leave you to spot the differences.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56
0

First of all there are some mistakes in the code.
1) You can not assign values in the structure.
2) When you are making a pointer for the structure you need pointer of the structure not of the int (does not matter what you want from the inside of the structure)

This is the modified code which runs perfactly

#include<stdio.h>

struct btnode
{
    int value;
    struct btnode *l;
    struct btnode *r;
};

int test(struct btnode *p)
{

    printf("%d", p->value);
}

int main()
{
    struct btnode *asdf = (struct btnode*)malloc(sizeof(struct btnode));
    asdf->value = 5;
    test(asdf);
}
Pranam
  • 38
  • 1
  • 5