Thread: passing vectors to functions

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    passing vectors to functions

    Hello, i have tried to read how I am supposed to pass a vector to a function.

    When dealing with a single float number we pass the number by it's variable name. And if we want to change a floating number in a function I pass the adress &floatname, and in the function I change it by saying *floatname=blabla.

    But this does not apply to vectors? No matter if I want to change the vector or just use it in a function I give it in to the function by "vectorname[]".?

    regards

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    There's no such thing as a 'vector' in C. Assuming you mean arrays...
    Code:
    void foo( type name[], size_t num )
    {
        ...
    }
    
    type name[ ARRAYSIZE ];
    
    foo( name, ARRAYSIZE );
    Also consider reading this.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Perhaps you mean to post in the C++ forum?
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  4. #4
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by iMalc View Post
    Perhaps you mean to post in the C++ forum?
    I did not mean to do that no. I am programming in C.

    And yes I meant arrays not vectors.

    But can anyone confirm what I wrote in my first post. I tried to test it with some code, and it seemed to work:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <math.h>
    void fun(float arr[]);
    
    
    int main(){
    
    float ar[10];
    
    fun(ar);
    int i;
    for (i=0;i<=10;i++)
    printf("Element %i=%f\n",i, ar[i]);
    
    }
    
    void fun(float table[]){
    
    int i;
    
    for (i=0;i<=9;i++)
       table[i]=i+1;
    
    
    
    }
    So can confirm that this allways work? That is: We pass the array to a function the same way, no matter if we want to change it contents or just use its contents?

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yes, you pass them the same regardless if you intend to change its contents or not.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Quote Originally Posted by quzah View Post
    Yes, you pass them the same regardless if you intend to change its contents or not.


    Quzah.
    Ok, thanks for your reply! it is interesting that arrays is so much more easy to handle than ordinary floats, but I guess the answers lie in the link you provided me with. I should read through all that when I have time.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Passing Argument from incompatible pointer type
    By AmritxD in forum C Programming
    Replies: 3
    Last Post: 08-15-2010, 03:23 PM
  2. Passing pointers to functions in seperate source files
    By derekeverett in forum C Programming
    Replies: 12
    Last Post: 10-08-2009, 07:15 AM
  3. Passing 2D arrays between functions
    By taurus in forum C Programming
    Replies: 10
    Last Post: 09-28-2009, 05:05 AM
  4. passing structures to functions
    By AmazingRando in forum C++ Programming
    Replies: 5
    Last Post: 09-05-2003, 11:22 AM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM