I have this piece of code wherein I want p to point to array index with value 10

struct_a {
int value;
...
}

There is an array of struct_a. finditem looks through the array to find one with value = 10;
main() {
struct_a *p;
fixup_p(p, 10);
}

void fixup_p (struct_a *p, int a) {
struct_a *check;
check = finditem(a);
p = check;
}
This doesnt work. What is the mistake here?

Recommended Answers

All 3 Replies

*sigh*

Post actual compilable code that demonstrates the problem.

[edit]If you want to change p in a called function, pass a pointer to it; i.e. a pointer to pointer to struct. Or else return a pointer from the function rather than returning void.

Hello
This is what I have.
Essentially I want to pass two pointers to find_name to make one of the pointers point to the other. In the real scenario, there is a lot more done in the routine.
I dont want to return the pointer as I have done in return_name but the outcome should be the same.
What is the mistake here?

Thanks

#include <stdio.h>
#include <string.h>

struct tag {                     /* the structure type */
    int number;
};

struct tag my_struct, my_struct1;        /* define the structure */
void find_name(struct tag *p, struct tag *p1);  /* function prototype */
struct tag *return_name(struct tag *p);
int main(void)
{
    struct tag *st_ptr, *st_ptr1;
    st_ptr = &my_struct;        /* point the pointer to my_struct */
   st_ptr->number = 10;
    st_ptr1= &my_struct1;
    st_ptr1->number = 20;
    find_name(st_ptr, st_ptr1);
    printf("%d\n", st_ptr1->number);
    st_ptr1 = return_name(st_ptr);
    printf("%d\n", st_ptr1->number);
    return 0;
}

void find_name(struct tag *p, struct tag *other)
{
    other = p;
}

struct tag *return_name(struct tag *p) {

  return(p);
    }

Previously:

If you want to change p in a called function, pass a pointer to it; i.e. a pointer to pointer to struct.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.