Like Tree1Likes
  • 1 Post By CommonTater

send one part of struct array to function

This is a discussion on send one part of struct array to function within the C Programming forums, part of the General Programming Boards category; lets say i have this Code: typedef struct {int x}stru; void fun(stru *array){ array->x=5; } int main() { stru arra[5]; ...

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    65

    send one part of struct array to function

    lets say i have this
    Code:
    typedef struct {int x}stru;
    void fun(stru *array){
     array->x=5;
    }
    int main()
    { 
     stru arra[5];
      fun(array[0])
    }
    the problem is that i want to send only one place of the array not all of it
    can you tell me whats the wrong code?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    You don't return an int from main like you're supposed to. You also have a typo, you declare it as arra, but pass it as array. Also, your function expects a stru *, but you pass array[0] which is just a stru. You either need to change the funciton definition to take a stru, or change the function call to pass the address of array[0].

  3. #3
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    hello
    how can i change the function call to send the arra position 4 for example
    i need to leave function declare as it is

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,484
    fun(&array[4])
    ought to do it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User sagar474's Avatar
    Join Date
    Jun 2011
    Location
    Kakinada
    Posts
    56
    why don't you just pass a pointer to the structure. ?
    what do you mean that "one place"? do you mean that only one struct in a array of structs?

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by cable View Post
    hello
    how can i change the function call to send the arra position 4 for example
    i need to leave function declare as it is
    Code:
    fun(&arry[3]);
    You really should pop open your textbooks and re-read the sections on arrays and pointers.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by Salem View Post
    fun(&array[4])
    ought to do it.
    Ummm... isn't that position 5?

  8. #8
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    guys please give me an code example for the next question
    Code:
    typedef struct {int x}stru;
    void fun(stru *array){
     array->x=5;
    }
    int main()
    { 
     stru arra[5];
      fun(&arra[4])
    return 0;
    }
    what change i need to do for this
    Code:
    fun(&arra[4])
    (which is not working) to make it send the fifth struct of the array of struct arra
    edit: or if you know a better way write it to me (always without changing the function declare)

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Ok we're not about to babysit you through replacing a 4 with a 5...

    Really... turn that big brain of yours back on...

    You need to read up on loops and arrays...
    You can start.... here

    And it's not working because your struct definition is wrong...
    Code:
    typedef struct {int x}stru;
    
    // should be
    
    typedef struct t_stru
      { int x;
       } stru;
    If you would set your code up properly you would have spotted that on your own.
    Last edited by CommonTater; 09-22-2011 at 12:51 PM.
    Salem likes this.

  10. #10
    Registered User
    Join Date
    Nov 2010
    Posts
    65
    edit:
    anyone else? about my question
    Last edited by cable; 09-22-2011 at 12:44 PM.

  11. #11
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,451
    Quote Originally Posted by cable View Post
    edit:
    anyone else? about my question
    Quote Originally Posted by cable View Post
    guys please give me an code example for the next question
    We're not going to hand you a code sample. We actually have a policy about this. 10 months and 30 posts on this board, you should know that by know. You should also know we expect some effort from you towards solving your problems. We've told you what you need to know and where to read up on it. You have to put in a little effort yourself. Note, arrays in C start at 0 and go to size-1, so your array only has array[0] to array[4]. There is no array[5].

    what change i need to do for this
    Code:
    fun(&arra[4])
    (which is not working) to make it send the fifth struct of the array of struct arra
    What's not working about it? You need to be specific. It looks like it's correctly sending the 5th element to me.

  12. #12
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    edit:
    anyone else? about my question

    Don't you just love it when they completely blow you off like that?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with last part of struct
    By victory1 in forum C Programming
    Replies: 8
    Last Post: 11-27-2009, 05:51 PM
  2. Replies: 6
    Last Post: 08-15-2009, 10:17 AM
  3. How to send Struct ?
    By sunit in forum C Programming
    Replies: 3
    Last Post: 06-11-2009, 07:50 AM
  4. Pass struct array to function
    By aditya_t90 in forum C Programming
    Replies: 4
    Last Post: 03-30-2009, 11:54 AM
  5. How Do I Send a Two-Dimensional Char Array to a Function?
    By DonFord81 in forum C Programming
    Replies: 7
    Last Post: 02-26-2009, 02:01 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21