a doubt about sending an array to a function

This is a discussion on a doubt about sending an array to a function within the C Programming forums, part of the General Programming Boards category; Hi, I have a porblem sending an array to a function, it sounds quie simple, however, it won´t let me ...

  1. #1
    Registered User
    Join Date
    Aug 2004
    Posts
    66

    Unhappy a doubt about sending an array to a function

    Hi, I have a porblem sending an array to a function, it sounds quie simple, however, it won´t let me do it, I believe there should be a way to do it, however, i cannot find it, if someone knows how, please help me, i want to do it without having to send a single element each time.

    It is an array of pointers, it has 4 elements, a simple list is attached to each of the elements, i can send the pointers one on one, but for a function I require the structure to go complete, but it won't, please help!!!!!!!!!


    [CODE]
    nodo *arr[4]; //this is the array of pointers to a structure named nodo
    /*now each pointer is sent to a function and each one gets a list, therés no problem on that/*
    /*
    now for sending it to a function i´ve tried all this:

    function(arr);
    function(&arr);
    function(arr[]);
    function (&arr[]);

    none of them work, I don´t know if the way to recieve it is correct

    the function declaration is:
    int function(nodo **arr);
    however that is forr recieving a pointer, if I try to recieve it as an array it won´t do it either, like this:

    int function(nodo arr[])

    pleae, i will be really thankful to any suggestions

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    I think you want:
    Code:
    int function(nodo *arr[])
    Then the call would be:
    Code:
    function(arr);

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,220
    Code:
    void function(char **strs)
    {
       printf("%c", *strs[0]);
    }
    
    int main(void)
    {
       char *strs[4];
       strs[0] = malloc(sizeof(char));
       *strs[0] = 'h';
       function(strs);
    }
    for example

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You're just trying to send the array, not a pointer to the array, right?
    Code:
    #include <stdio.h>
    void foo( int array[] )
    {
        int x;
    
        for( x = 0; array[ x ] != -1; x++ )
            printf( "%d\n", array[ x ] );
    }
    
    
    int main( void )
    {
        int array[] = { 1, 2, 3, 4, 5, 6, -1 };
    
        foo( array );
    
        return 0;
    }
    An array of pointers works the same way.

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

  5. #5
    UT2004 Addict Kleid-0's Avatar
    Join Date
    Dec 2004
    Posts
    656
    I think you're doing fine, you just pass it in...though I'm not totally sure if I'm answering this correctly in the code, ask more specifically if needed.
    Code:
    #include <stdio.h>
    #include <al/etl.h>
    #include <sfl.h>
    
    struct nodo
    {
    	int n;
    	double f;
    };
    
    void initializePointerArray( struct nodo **p )
    {
    	p[0] = malloc( sizeof( struct nodo ) );
    	p[1] = malloc( sizeof( struct nodo ) );
    	p[2] = malloc( sizeof( struct nodo ) );
    	p[3] = malloc( sizeof( struct nodo ) );
    	
    	p[0]->n = 4;
    	p[0]->f = 342.3F;
    	p[1]->n = 5;
    	p[1]->f = 252.3F;
    	p[2]->n = 6;
    	p[2]->f = 112.3F;
    	p[3]->n = 7;
    	p[3]->f = 555.3F;
    }
    
    int main( void )
    {
    	int i;
    	struct nodo *pointerArray[4];
    	initializePointerArray( pointerArray );
    	
    	for( i=0; i<4; i++ )
    	{
    		printf( "%d: %d %5.1f\n", i, pointerArray[i]->n, pointerArray[i]->f );
    		free( pointerArray[i] );
    	}
    	
    	return EXIT_SUCCESS;
    }

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <string.h>
    
    typedef struct tag_xp
    {
    	int age;
    	char *name;
    
    }typexp;
    
    void xcall(typexp**);
    
    int main(int argc, char **argv)
    {
    	typexp *arr[4];
    
    	arr[3] = malloc(sizeof(typexp));
    	arr[3]->age = 15;
    	arr[3]->name = "string";
    
    	xcall(arr);
    	free(arr[3]);
    }
    
    void xcall(typexp  **p)
    {
    	printf("age: %d\n", p[3]->age); // OR: (**(p + 3)).age OR: (*(p + 3))->age
    	
    	printf("name: %s\n", p[3]->name); // OR: (**(p + 3)).name OR (*(p + 3))->name
    }

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You guys sure like to obfuscate things, which can be fun at times, but is rather pointless here. Just use the method swoopy shows, and it looks inside the function exactly the same as it does outside the function.

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

  8. #8
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    Let me clear things up for you guys.

    nodo *arr[4];

    what you have is a pointer to an array.

    with the function declaration of:

    function( nodo ** arr)

    it wants a pointer to a pointer to an array. This is not what you have at all. This in theory should never work. Some compilers will adjust it for you and accept this.
    With the function declaration of:

    function(nodo arr[]);

    it simply wants an array. you have a pointer to an array. you would have to dereference the array like this:

    function( *arr);

    the * dereferences the pointer. if you wanted to pass a call like this:

    function(&arr);

    you are giving the function a pointer to a pointer to an array. Your function would have to look like this:

    function(nodo ** arr[]);

    I don't think the problem is in the function though. It is the array. You shoud have an array like this:

    nodo arr[4];

    then you can have a function of:

    function(nodo * arr[]);

    and pass:

    function(&arr);

    or a function of

    function(nodo arr[]);

    and pass:

    function(arr);

    Hope this clears everything up.
    Don't quote me on that... ...seriously

  9. #9
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    Pointers and arrays are complicated little crappers.
    nodo arr[4];

    a function of

    function(nodo arr[]);

    and pass:

    function(arr);
    This is correct. Unfortunately, the rest of your post is not quite so correct.

  10. #10
    Captain - Lover of the C
    Join Date
    May 2005
    Posts
    341
    I have a little problem with the reply above mine. If you do it that way then you have to return an array. If you don't want to jack around with returning an array since getting one has been so hard, I recommend this:

    Code:
    nodo arr[4];
    
    void function(nodo * arr[])
    {
        *arr[0] = "Hi";
        *arr[1] = "Hello";
        *arr[2] = "Hola";
    }
    
    function(&arr);
    you notice that you have to have a * in front of the array in the function. Just Do It. Have fun.
    Don't quote me on that... ...seriously

  11. #11
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    >> I have a little problem with the reply above mine. If you do it that way then you have to return an array. <<

    Which reply are you referring to?

    I'd suggest you test your code by trying to compile it. Just Do It. Have fun.

  12. #12
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Brad0407, I think you need to read the original post.

  13. #13
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You never have to return an array. Arrays are passed by "reference" (so to speak). Any changes to any array passed to a function effect the array outside of the function.

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

  14. #14
    Registered User
    Join Date
    Mar 2005
    Posts
    135
    Quote Originally Posted by Brad0407
    Let me clear things up for you guys.

    nodo *arr[4];

    what you have is a pointer to an array.
    To be more precise: 'arr' is an array of 4 pointers of type nodo.

    with the function declaration of:

    function( nodo ** arr)

    it wants a pointer to a pointer to an array. This is not what you have at all. This in theory should never work. Some compilers will adjust it for you and accept this.
    No, it's just a pointer to a pointer. It's up to the programmer if he/she wants to treat it as an pointer to a pointer to an array. If for example, in the call function it was made as an array of pointers (as in this case), it's fine to use it as one. If it was'nt and in the called function it is used as one - you would most likely get an access violation'.

    With the function declaration of:

    function(nodo arr[]);

    it simply wants an array. you have a pointer to an array. you would have to dereference the array like this:

    function( *arr);
    the * dereferences the pointer
    It simply wants a pointer, which yeah, is a pointer to an array.


    if you wanted to pass a call like this:

    function(&arr);

    you are giving the function a pointer to a pointer to an array. Your function would have to look like this:

    function(nodo ** arr[]);
    If the declaration in the call function is still an array of pointers of type nodo, then, &arr is meaningless because arr is just an array (of pointers) and an array automatically exposes its address, therefore; function(&arr) and function(arr) is the same thing. If 'arr' were a pointer variable, then yes, you would retrieve it's address with &arr.

    I don't think the problem is in the function though. It is the array. You shoud have an array like this:

    nodo arr[4];

    then you can have a function of:

    function(nodo * arr[]);
    No, just: function(nodo *arr);

    and pass:

    function(&arr);
    Like I said, arrays automatically expose their address with simply, it's name (arr).

    or a function of

    function(nodo arr[]);
    Something helpful to remember is that, [] and '*' are alot of the times interchangable in function paramters.
    Last edited by xeddiex; 05-15-2005 at 12:53 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. Dynamic Array Allocation function
    By P4R4N01D in forum C++ Programming
    Replies: 6
    Last Post: 05-15-2009, 03:04 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 07:39 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. I need help with passing pointers in function calls
    By vien_mti in forum C Programming
    Replies: 3
    Last Post: 04-24-2002, 11:00 AM

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