I am trying to assign a pointer to an array element inside the function get_by_id.
The function get_by_id finds the element correctly and assigns to element to lp, however when it comes back to the caller (set_attr), it reverts to NULL.
I'm not understanding why it's reverting to NULL in the caller, let alone how to fix it.
#include <stdio.h>
#include <stdlib.h>
typedef struct
{
int id;
char name[128];
int attr;
} Lookup;
static int num_sources = 0;
Lookup ltable[128];
void add(int id, const char *name)
{
sprintf(ltable[num_sources].name, name);
ltable[num_sources].id = id;
ltable[num_sources].attr = 999; // example default attr 999
num_sources++; // increment the number of sources
}
int get_by_id(int id, Lookup *lp)
{
int status = 1;
for (int i = 0; i < 128; i++)
{
lp = <able[i];
if (id == lp->id)
{
status = 0;
break;
}
}
return status;
}
int set_attr(const int id, const int attr)
{
Lookup *lp = NULL;
int status = get_by_id(id, lp);
if (status == 0)
{
lp->attr = attr;
}
return status;
}
int main(void) {
add(88, "main"); // example id 88
set_attr(88, 9); // example attr 9
return EXIT_SUCCESS;
}
lpinset_attr()to remain unchanged if a matchingidnot found?