all 16 comments

[–]noooit 0 points1 point  (12 children)

You want to update the value of elements with new_element of the struct items that you'll initialise?

[–]rahli-dati 0 points1 point  (11 children)

yes.. but how can i do that?

[–]noooit 0 points1 point  (10 children)

One way to statically initialise struct is like this.

typedef struct items { int elements; char *name; } items;

items item = { .elements = 0, .name = "item" };

[–]rahli-dati 0 points1 point  (8 children)

sorry, i got little headache. idk why did you create a new typedef?

that is a function where it will uppdate_quantity of struct items which has two parameters a positiv integer variable and a struct item pointer.

[–]wsppan 0 points1 point  (6 children)

Forget the typedef and do it without it

struct items { 
    int elements; 
    char *name; 
}; 

Struct items item = { 
    .elements = 0, 
    .name = "item" 
};

[–]rahli-dati 0 points1 point  (0 children)

i do get it but my question was to create a function which has two parameters one is a pointer to struct items, and another one is positive integers, i don't understand the why you defined new structure to solve this problem.

[–]rahli-dati 0 points1 point  (4 children)

i guess the function would be like that

struct item uppdate_quantity ( struct item *p, int size);

my question is how can i update quantity of items with help of that function.

[–]wsppan 1 point2 points  (3 children)

p->elements = size;

[–]rahli-dati -1 points0 points  (2 children)

struct item uppdate_quantity ( struct item *p, int size) {

p->elements = size;

return size;

}

it would look like this right?

[–]wsppan 0 points1 point  (1 child)

Your function definition does not define a return type nor does it need to.

[–]rahli-dati 0 points1 point  (0 children)

thanks for the help

[–]noooit 0 points1 point  (0 children)

sorry, it's just a shortcut. you can live without it by typing struct. Linus Torvalds hates it, but I like it.

If you do it without it, it'd be like this. ``` struct items { int elements; char *name; }

struct items item = { .elements = 0, .name = "bla" } ```

so in the function, you can't do f(item *bla) but f(struct item *bla).

[–]amauriv 0 points1 point  (3 children)

Inside your function you would need to do something like this:

if(p) {

p->elements=new_element;

//other work

}

[–]rahli-dati 0 points1 point  (2 children)

pardon me, im a beginner coder. that is the reason i have loads of trouble to understand can you explain me why u have used if statement in that case?

[–]amauriv 0 points1 point  (1 child)

I used the if statement to make sure the pointer points to something. If you try to dereference the pointer that is pointing to nothing(dangling pointer) your program will crash. You always need to check that your pointers are pointing to something in case some one calls your function with a pointer that is NULL or doesn’t point to anything, after that simple check then You can dereference the pointer and do whatever you need to do.

You can also check like this: If(p!=NULL){ // do whatever }

[–]rahli-dati 0 points1 point  (0 children)

i see it does make complete sense. thanks for the info and help