Bill Cunningham said:
This is what I've been trying to accomplish. It compiles into an object
file with no complaints so there must be something right.
This is a potentially dangerous attitude.
It's not nearly sufficient to have "something right". The goal is
to have *everything* right. A program that compiles without error
but doesn't execute correctly isn't significantly better than a
program that fails to compile.
I wanted a
function to load up a pointer to a struct.
Why? What is your goal?
#include "c.h"
struct cat *load(char *name, char *color)
{
struct cat *temp;
temp->color = color;
temp->name = name;
return temp;
}
Obtaining a pointer to an object that doesn't already exist can
be tricky. There are multiple approaches, each with its own
advantages and disadvantages.
You can return a pointer to a static object, which is straightforward
but means you only have one copy of the object in your entire
program.
You can require the caller to create the object (by whatever means)
and pass a pointer as an argument. This places an additional burden
on the caller.
You can have your function allocate the object with malloc.
This means the caller has to be responsible for deallocating it,
and both the caller and the function have to deal with allocation
failures.
You've chosen none of these alternatives. Your code will not work,
though it might accidentally appear to do so.
Note also that structs (unlike arrays) can be passed, returned, and
assigned by value:
struct cat new_cat(char *name, char *color)
{
struct cat result;
result.color = color;
result.name = name;
return result;
}
This has its own disadvantages. It doesn't give you a pointer, so
you'll have to modify the caller to handle a returned struct value.
It (probably) copies the entire structure, which could be inefficient
if the structure is very large.