Thread: Passing structures to a function as pointer??

  1. #1
    Registered User
    Join Date
    Dec 2015
    Posts
    22

    Passing structures to a function as pointer??

    So, I have a structure containing two ints and want to pass the structure as a pointer (or a pointer to the structure) to a function.

    So I have a few questions:

    1. Do i need a pointer variable to do this? Or can i somehow just use the &-operator?

    2. What if I have a struct array? E.g.:
    Code:
    struct myStruct{
         int a;
         int b;
    }structArray[50];
    How can i then pass the array and modify it's elements and individual members?

    I would love some concrete examples and some explanation on what's going on. I might be asking for a lot but I'm really getting desperate.

    I hope these questions make sense; if not, please tell me and I'll try to explain it better.

    P.S I've googled and watched youtube-tutorials etc for days without luck on how to actually do this. I can only find examples that look sort of what I want to do but I really want to understand what I'm doing and not copy-paste.

    Thanks in advance

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    1. You can use a pointer variable if you have one. The result of an expression that uses & (address of) will be a pointer type.
    Code:
    struct custom { int something; } obj;
    
    void foo(struct custom *s);
    /* definition omitted*/
    
    foo(&obj);
    2. See Question 6.4 - the type of array is not really significant in the question.

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by Finoli View Post
    1. Do i need a pointer variable to do this? Or can i somehow just use the &-operator?
    You can just use the address-of operator. See examples 2 and 3 in post #14 here.

    Quote Originally Posted by Finoli View Post
    2. What if I have a struct array?
    The process is the same as passing a non-struct array to a function. A quick search for examples yielded many results, such as this one.

    Read the linked examples and let us know if there's anything you don't understand.

  4. #4
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Also posted here.

  5. #5
    Registered User
    Join Date
    Dec 2015
    Posts
    68
    If the struct only have two int's maybe pointers is not necessary , is the struct members modified by the function or data just read(passed) to it?
    I think it should work by just passing the index number to the functions.

    By using pointers the function call don't have to copy data to stack etc, so they're efficient.
    You have to declare a pointer of the specific struct type, and it's member offset can be used in function with -> (instead of .)

  6. #6
    Registered User
    Join Date
    Dec 2015
    Posts
    22
    Quote Originally Posted by tonyp12 View Post
    If the struct only have two int's maybe pointers is not necessary , is the struct members modified by the function or data just read(passed) to it?
    I think it should work by just passing the index number to the functions.

    By using pointers the function call don't have to copy data to stack etc, so they're efficient.
    You have to declare a pointer of the specific struct type, and it's member offset can be used in function with -> (instead of .)
    It's for an assignment. The ints represent the value and suit of a card, and using the struct im supposed to make a deck (somehow..).
    So yes, the members will be modified. How im going to make a deck out of it in not sure, trying to take it step by step.
    Im sure ill run into more trouble soon enough, stay tuned lol

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing structures to a function?
    By Meerul264 in forum C++ Programming
    Replies: 16
    Last Post: 12-26-2012, 06:15 AM
  2. Passing structures to a function
    By Magi in forum C Programming
    Replies: 7
    Last Post: 12-05-2012, 03:04 PM
  3. Passing array of structures by pointer
    By edharvey in forum C Programming
    Replies: 7
    Last Post: 04-13-2011, 02:58 AM
  4. Problems with passing an array of structures by pointer
    By raptor1770 in forum C Programming
    Replies: 9
    Last Post: 11-29-2008, 11:01 AM
  5. Structures, passing array of structures to function
    By saahmed in forum C Programming
    Replies: 10
    Last Post: 04-05-2006, 11:06 PM

Tags for this Thread