Jump to content

Structures.

- - - - -

  • Please log in to reply
2 replies to this topic

#1
ChilledOut

ChilledOut

    Newbie

  • Members
  • Pip
  • 6 posts
I just learned about this today. There are a couple of things I don't completely understand, but to avoid overwhelming myself and other people on this forum I'm going to take it one step at a time.

The first thing I'm having trouble with is making a structure into a function. I know the function call should look like this:

database fn();

Where database is the type of struct and fn is the function name. What I don't know is what return type to make the function. I have read that it should be the same return type as the structure itself but that hasn't worked for me yet.

After this is solved I will move on to the next, less serious thing.

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You've just declared that the return type of fn() is database.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
If I get what you are doing, you could always return i.e. type database* to use in your calling code.

i.e.
database* fn() {
  struct database* mydb = malloc(sizeof(struct database));
  if(mydb != NULL) {
    mydb->id = 12;
  }
  return mydb;
}

..
struct database *caller = NULL;
caller = fn();
You could as well manipulate caller through a pointer, i.e. defining
void fn(struct database* mydb) {
  if(mydb != NULL) {
    mydb->id = 21;
  }
}
..
struct database *caller = NULL;
fn(&caller);
It should be better to do this, if you could provide a bit more info feel free to ask as always.

Alexander.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users